Jump to content

Displaying a loop of generated images


ExpertAlmost

Recommended Posts

Good morning!

 

I have some simple test code (below) that generates a random image of 100x100 pixels. I have a loop to generate a new image 25 times. I want to see each of those 25 runs. But this code only shows what may be the last image. What did I do wrong?

 

How can I automatically update newly generated images in PHP?

 

Thank you for your expertise!

 

<?php
header ("Content-type: image/png");
$ImageSize = 100;
$Img = imagecreatetruecolor($ImageSize, $ImageSize);
for ($Runs = 0; $Runs < 25; $Runs++) {
   for ($XVal = 0; $XVal < $ImageSize; $XVal++) {
      for ($YVal = 0; $YVal < $ImageSize; $YVal++) {
         $RedVal = rand(0, 255); $GreenVal = rand(0, 255); $BlueVal = rand(0, 255); 
         $PixelClr = imagecolorallocate($Img, $RedVal, $GreenVal, $BlueVal);
         imagesetpixel($Img, $XVal, $YVal, $PixelClr);
         imagecolordeallocate($Img, $PixelClr);
         }
      }
      imagepng($Img);
      imagedestroy($Img);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/169879-displaying-a-loop-of-generated-images/
Share on other sites

You're only displaying the last image. You need to either store them and then display them or  display each one as it is generated. Something like this:

<?php
if (isset($_GET['d'])) {
   header ("Content-type: image/png");
   $ImageSize = 100;
   $Img = imagecreatetruecolor($ImageSize, $ImageSize);
   for ($XVal = 0; $XVal < $ImageSize; $XVal++) {
      for ($YVal = 0; $YVal < $ImageSize; $YVal++) {
         $RedVal = rand(0, 255); $GreenVal = rand(0, 255); $BlueVal = rand(0, 255); 
         $PixelClr = imagecolorallocate($Img, $RedVal, $GreenVal, $BlueVal);
         imagesetpixel($Img, $XVal, $YVal, $PixelClr);
         imagecolordeallocate($Img, $PixelClr);
         }
      }
      imagepng($Img);
      imagedestroy($Img);
}
?>
<html>
<body>
<?php
for ($i=0;$i<25;$i++) {
   echo '<img src="?d=1" h="100" w="100"><br><br>';
}
?>
</body>
</html>

 

Ken

Thank you for the kindness of your reply kenrbnsn! That works great. But...

 

Perhaps a more detailed description of what I am trying to do is in order. Rather than show 25 images (in a row or column formation), or one big composite image containing the 25 images -- I want to see in one location on one page, a graphic changing 25 times, once for each iteration of generation. Perhaps thinking of it as an animation makes more sense? If the loop is too fast, I can always put in a delay.

 

Perhaps it is impossible to do this in PHP and all I can do is 25 images in a line?

 

Thanks again to all of you ExpertsAlready for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.