tags:

views:

27

answers:

2

I have a php code which generate an gif-image defined by a set of parameters. I wrote this code as a function and now I want to insert an image into a html page.

My problem is that whenever I use an img tag I need to specify a name of the file containing an image but I do not have an image in a file. The file containing a function for generation of an image do not contain the image it contain the function generating an image.

I could use a file which would contain an image but then I do not know how to pass parameters to this file. Usage of $_GET does not seem to be a good solution since one of the arguments of the function is a big array.

Is there an elegant solution of the described problem?

A: 

You can create a PHP file that generates the image you need from the specified $_GET arguments. In the <img ... /> tag, you can set the image source to the PHP file path that is passed the proper arguments. In the PHP script, be sure to set the content-type header information correctly.

Marius Schulz
His problem is that he has more parameters than fit into `$_GET`.
Pekka
+1  A: 

Usage of $_GET does not seem to be a good solution since one of the arguments of the function is a big array.

You could serialize() the array with your parameters and store it in a temporary file with a randomly generated ID. You would then pass that ID to the image file:

<img src="script.php?data=1230491039202439">

if you do the creating operation in the script that creates the HTML containing the image reference before outputting the img tag, this will be safe to do.

The other approach would be generating all images in a separate process, and writing them out into a file (the file name consisting e.g. of the md5() value of the serialized data array). This would effectively be a caching solution, where no calculations are made during the time of the request. This may or may not be feasible for you, depending on your project's architecture.

Pekka