Best practice: Displaying image generated by C program in PHP website

83 views Asked by At

I would like some advice about what would be the best practice from a design perspective when tackling this problem:

  • I have a website that is currently displaying an "image" generated by google maps.
  • This website runs on an Apache web server in Linux.
  • I would like to generate my own "image" using my own program (my program.c), not the google maps library.
  • My program (my_program.c) is written in in C. I have the option of installing it into the same machine as the Apache web server, or a separate machine.

I need to: 1) somehow call this C program, or preferably a function inside this program (my_program.c) from the PHP code, 2) Access the MySQL database via the C program, and perform some calculations that would ultimately render the image desired 3) Return the results of this process (the image that was generated) to the PHP program so that it can be displayed in the browser.

Any tips would be appreciated.. Thanks.

1

There are 1 answers

0
r3mainer On BEST ANSWER

1) Call this C program from the PHP code:

Put the compiled program in your /cgi-bin folder and pass parameters from PHP via the query string. Make sure that your code sends out at least a Content-Type HTTP header before any image data (e.g., printf("Content-Type: image/png\r\n\r\n");)

2) Access the MySQL database via the C program:

Use the MySQL C API.

3) Return the image generated by this process to the PHP program so it can be displayed in the browser.

You probably don't need to return anything to PHP. Just embed a link to the CGI application in the HTML code, e.g.,

<img src="http://example.com/cgi-bin/my-map-tool?lat=+42.5;long=-0.5" />