2 Replies Latest reply: Oct 2, 2013 12:45 PM by Fumph RSS

    Finding the error - PHP

    Fumph Community Member

      Hi there, as part of a bigger project, I need to copy and resize some pictures. It'd be great if someone could tell me where I'm going wrong! I've got 100+ pictures in the folder "photoframe" and they need to be scaled, compressed, and then copied to the folder "cache." The foreach loop gets to 24 pictures and then stops! Why is this?

       

      function cache_old() {

                $imgs = glob("photoframe/*.{jpg,png,gif,JPG,PNG,GIF}",GLOB_BRACE);

                foreach ($imgs as $image) {

                          // Set a maximum height and width

                          $width = 200;

                          $height = 1000;

       

                          // Get new dimensions

                          list($width_orig, $height_orig) = getimagesize($image);

       

                          $ratio_orig = $width_orig/$height_orig;

       

                          if ($width/$height > $ratio_orig) {

                             $width = $height*$ratio_orig;

                          } else {

                             $height = $width/$ratio_orig;

                          }

       

                          // Resample

                          $image_p = imagecreatetruecolor($width, $height);

                          switch (strtolower(pathinfo($image,PATHINFO_EXTENSION))) {

                                    case "jpg":

                                    case "jpeg":

                                              $image_c  = imagecreatefromjpeg ($image);

                                              break;

                                    case "png":

                                              $image_c = imagecreatefrompng($image);

                                              break;

                                    case "gif":

                                              $image_c = imagecreatefromgif($image);

                                              break;

                          }

                          imagecopyresampled($image_p, $image_c, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

       

                          // Output

                          $saveAs = "cache/thumb_".basename($image);

                          switch (strtolower(pathinfo($image,PATHINFO_EXTENSION))) {

                                    case "jpg":

                                    case "jpeg":

                                              imagejpeg($image_p, $saveAs,50);

                                              break;

                                    case "png":

                                              imagepng($image_p, $saveAs,5);

                                              break;

                                    case "gif":

                                              imagegif($image_p, $saveAs);

                                              break;

                          }

                          echo "Resampled ".$saveAs." as a ".strtolower(pathinfo($image,PATHINFO_EXTENSION)).". Dimensions: ".$width."x".$height."<br>";

                }

      }

      cache_old();

       

      In an attempt to get things woking, I've copied other code, twisted things up, and still, only 24 pictures copied. The 24 that do get copied are perfect! I'm open to anything,  I probably didn't read the fine print in the functions or something simple like that. Renaming files and moving files doesn't affect anything, and resampling individual files past the 24th image work fine.

       

      Each picture is roughly 500kB-3mB large, and I'm looking to get each picture 200 pixels wide, keeping the aspect ratio, but slimming down the size a lot! Quality isn't a objection here, the copied versions will be thumbnail-like.

       

      Thank you all!

        • 1. Re: Finding the error - PHP
          David_Powers CommunityMVP

          The fact that you get 24 images and no more tells the story in itself. You've run out of memory.

           

          You need to destroy the image after saving it. I haven't studied your script in detail, but the last few lines of your function should be changed like this:

           

                              echo "Resampled ".$saveAs." as a ".strtolower(pathinfo($image,PATHINFO_EXTENSION)).". Dimensions: ".$width."x".$height."<br>";

                    }

                     imagedestroy($image_p);

          }

          • 2. Re: Finding the error - PHP
            Fumph Community Member

            I assume you meant to add destroyimage inside the loop? I tried on the inside, the outside, and even both.

             

            I used destroyimage on $image_p and $image_c and still nothing. I looked into it a bit more, and I'd agree with you completely it's a memory error. Any other ideas to erase the tracks of the looped images so I don't run out of memory like this?