Welcome Guest, Not a member yet? Register   Sign In
Exceeding memory_limit - Image Manipulation Class
#1

[eluser]Unknown[/eluser]
Hello Everyone!

I've got a problem whilst trying to use the image_lib.

As you can see below im using it in a foreach loop so that every file in the specified folder is turned into a thumbnail in a different folder.

The actual creation of the thumbnail works fine but I would have thought that after each pass through the loop it should release the memory once it's done the processing and then start a fresh, but it seems to just keep adding to the memory and causing this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 5120 bytes) in D:\durham\work files\websites\DesignJunkie\system\libraries\Image_lib.php on line 1140

I know that i could increase the size in the php.ini file but with the amount of files that need to be put through this function i would have to put it stupidly high.

Is there any problem in my codeing or any other way around this problem?

Cheers

Durham

CODE:


$photofile = get_filenames('./web/photoshoots/'.$data['shootid']);

foreach ($photofile as $value)
{
$clientphotos['image'] = $value;

//CREATE THUMBNAILS
$this->load->library('image_lib');

$config['image_library'] = 'GD2';
$config['source_image'] = './web/photoshoots/'.$clientphotos['shootid'].'/'.$clientphotos['image'];
$config['new_image'] = './web/photoshoots/'.$clientphotos['shootid'].'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 170;
$config['height'] = 120;

$this->image_lib->initialize($config);

$this->image_lib->resize();

//echo $this->image_lib->display_errors();
//END THUMBNAILS
}
#2

[eluser]Michael Wales[/eluser]
Image processing is notoriously server intensive. Your best bet would be to run the thumbnailing in batches of X files at a time.

PHP doesn't release the memory until script execution is complete.
#3

[eluser]Derek Allard[/eluser]
You may be able to locally bump your memory limit. We wrote about it in the ExpressionEngine knowledge base if that's of interest to you.
#4

[eluser]Drarok[/eluser]
[quote author="walesmd" date="1192473371"]PHP doesn't release the memory until script execution is complete.[/quote]
Sounds like a bug in the image lib to me!
PHP does release memory before script end. Look at this example:
Code:
<?php

    class ABigClass {
        function __construct() {
            $this->bigString = str_repeat(' ', 1024 * 1024); // 1MB
        }
    }

    $classes = array();
    for ($x = 1; $x < 15; $x++) {
        $classes[] = new ABigClass();
    }

    echo "I'm using loads of RAM!";
?&gt;
This will die (assuming your memory limit is low enough, anyway).

This, however, will work fine, because PHP will release unreferenced memory:
Code:
&lt;?php

    class ABigClass {
        function __construct() {
            $this->bigString = str_repeat(' ', 1024 * 1024); // 1MB
        }
    }

    $classes = array();
    for ($x = 1; $x < 15; $x++) {
        $classes[] = new ABigClass();
        $classes = array(); // THIS WILL RELEASE ABigClass's MEMORY!
    }

    echo "I'm not using loads of RAM!";
?&gt;

So it seems to me that either the image library is keeping references that it doesn't need, or this gentleman's code is.
#5

[eluser]salbertson[/eluser]
I am getting the same error at the same line of Image_lib.php with a jpg of size 1.5 MB. I bumped my memory_limit in php.ini from 16 to 64 MB and the resize worked. Is there a memory leak in the library functions or does it really take that much memory to resize a 2 MB image?
#6

[eluser]Jordan Harper[/eluser]
I understand what's going on here but I'm interested in hearing a response to salberson's question...

I'm trying to process a single 1.4MB image, and PHP has 16MB set as the memory limit - sure enough it's throwing the 'Allowed memory size ... exhausted' fatal error message. This seems like a particularly low threshold.

Alternatively, can anyone think of any way to make this fail gracefully rather than white-out the screen?
#7

[eluser]Randy Casburn[/eluser]
[quote author="salbertson" date="1200446007"]I am getting the same error at the same line of Image_lib.php with a jpg of size 1.5 MB. I bumped my memory_limit in php.ini from 16 to 64 MB and the resize worked. Is there a memory leak in the library functions or does it really take that much memory to resize a 2 MB image?[/quote]

Hi Salbertson, I'm not sure anyone specifically stated this yet or not so I will.

JPEGs are (very) compressed file structures on disk. In order to "resize" the image that is represented within the structure of the file, it must be uncompressed into memory, manipulated into the second "resized" copy in memory, then finally the "resized" image is re-compressed and loaded back into your PHP variable.

That's where your memory consumption is coming from. Your only option is to try other graphics processors to see if they are more graceful with your memory. ImageMagic is the best choice other than GD.

By the way, I seriously doubt this is a memory "leak" as the memory is being relinquished upon the termination of the program. If it were, GD wouldn't be running on many of our servers as it would be crashing most of them and the hosts wouldn't allow that.


@jordan Harper - Reference the memory error, if you're using PHP 5, the best way might be to use your "try/catch" exception processing found here...http://us3.php.net/manual/en/language.exceptions.php

Hope this is helpful,

Randy




Theme © iAndrew 2016 - Forum software by © MyBB