0

Given a large set of unordered images in the format either jpg or png, I wanted to create a PHP script that would firstly filter all folder contents for the allowed formats, copy them to a new folder renamed in numerical order (1.jpg, 2.jpg, 3.jpg ..), create a 50x50 thumbnail of each image in a child folder "thumbs" and then create an .html file called "gallery" which contains a dump of "img" tags of each thumbnail.

It works fine up until a dozen or so images and then exceeds the maximum allocatable memory. This has suddenly happened and appears when the function imagecopyresized is called.

Any advice is appreciated.

Source:

<?php

# Prepare vars
$dir = "O:/zip/";
$newDir = "C:/Users/user/Desktop/zip/";
$thumbs = $newDir."thumbs/";
$gallery = $newDir."gallery.html";
$types = array(".jpg", ".png");
$files = array();
$tempFiles = scandir($dir);
$i = 0;

# Copy and rename images
foreach($tempFiles as $file)
{
    $thisType = substr($file,-4);
    if(in_array($thisType, $types))
    {
        $dest = fopen($newDir.$i.$thisType, 'w');
        fwrite($dest, file_get_contents($dir.$file));
        fclose($dest);

        list($width, $height) = getimagesize($newDir.$i.$thisType);
        $im = imagecreatetruecolor(50, 50);
        if($thisType == '.jpg')
        {
            imagecopyresized($im, imagecreatefromjpeg($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
            imagejpeg($im, $thumbs.$i.$thisType);
        }
        else
        if($thisType == '.png')
        {
            imagecopyresized($im, imagecreatefrompng($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
            imagepng($im, $thumbs.$i.$thisType);
        }
        imagedestroy($im);

        $html .= "<a href='$newDir$i$thisType'><img src='$thumbs$i$thisType' alt='$i$thisType' width='50' height='50'></a>";
        print "Successfully processed $i$thisType<br>";
        $i++;
    }
}
print "Done.<br>";

# Create html gallery for new imgs
$dest = fopen($gallery, 'w');
fwrite($dest, $html);
fclose($dest);

print "There are ".number_format($i)." image files.\n\r";

?>
2
  • Have you tried upping the memory available? Commented Dec 14, 2011 at 0:32
  • 1
    I've contemplated that, but I'd rather solve the memory issue otherwise I'd have to keep increasing the limit the more images are in the initial folder. Commented Dec 14, 2011 at 0:33

2 Answers 2

3

Your code looks ok. How large is the 12th image, is it larger than the others? Check out the docs for imagecopyresized() @ php.net, someone wrote a setMemoryForImage() function which determines if the current php.ini setting for 'memory_limit' will allow for a given image to be resized.

This leads me to believe that if the original image is too large, it will exhaust the memory when attempting to resize.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm baffled as the images are only ever a few hundred K. I increased the limit to 1024 and all ~500 images process perfectly.
@Lee: What did you increase the limit from?
The default which is 16, which is what leads me to believe there must be something wrong with my method, considering the massive needed change.
Ya, obviously low. In this case I think it's ok to up the limit size to accomplish what you need, but don't overdo it. Try 512, 256, 128 and see what you can get away with.
1

Any advice is appreciated.

Well, at first glance I don't see the leak, but you can use memory_get_usage() to print out debugging info to spot the lie which doesn't release the memory.

Or it can be just one exceptionally big image causing this error alone. For this case yopu have to increase memory limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.