Create multiple dynamic image with php

Am trying to write a script that will create an image and write randomly generated numbers on each of the images in a loop. Everything is almost working except that it is only the last generated numbers will be written in all the images… Please can anyone help me with this so that as the numbers are being generated in a loop, it will be written on each of the image seperately, so that i will have images with different values written on them.
Below is my codes: the first one ‘gen.php’ is the one that generates the numbers, and outputs it with an image that will be created with the second scripts ‘img.php’.
$num = 5; // Assume we want to generate just 5 differnt set of numbers
for ($j = 1; $j <= $num; $j++) {
$md5 = md5(rand(0, 999));
$pin = substr($md5, 1, 10);
$pin = strtoupper($pin);
$_SESSION[‘pin’] = $pin;
echo “<img src=img.php /><br><br>”;


$pin = $_SESSION[‘pin’];

$img = imagecreate(200,50); //Create the image resource

//We are making three colors, white, black and gray
$white = ImageColorAllocate($img, 255, 255, 255);
$black = ImageColorAllocate($img, 0, 0, 0);
$grey = ImageColorAllocate($img, 204, 204, 204);

ImageFill($img, 0, 0, $grey);

imagestring($img, 10, 30, 20, $pin, $white); //Add randomly generated string in white to the image

header(“Content-Type: image/png”); //Tell the browser what kind of file is come in

imagepng($img); //Output the newly created image in png format

ImageDestroy($img); //Free up resources


The reason only the last one would be showing up is because your using a session which is only been passed once to the img.php file since that file only gets called once the image exists in the DOM.

A solution would be to do something like


which would eliminate the session all together there for giving an accurate result. Then in your PHP file simply instead of $_SESSION call $_GET

:slight_smile: It worked as perfect as I wanted, thanks very much, I appreciate!! :slight_smile:

No problem