Search code examples
phpmemoryout

PHP Image Resize Fatal error: Out of memory


I have following PHP code that resizes my pictures to desired size:

/* POSTER - resize */
                $remote_file = $castImages[$name];
                $new_width  = 296;
                $new_height = 436;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
                $new_width  = 74;
                $new_height = 109;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
                imagedestroy($image_p);
                imagedestroy($image);

I have around 5500 pictures to be resizes, so i run this code into the while loop and get this error from PHP:

Fatal error: Out of memory (allocated 473956352) (tried to allocate 27263000 bytes) in D:\portal_ONLINE\UwAmp\www\inc\test.php on line 54

I then add in PHP script this code:

ini_set('memory_limit', '-1');

But i receive same error message..so how to fix this error so that script rename all 5500pictures not just 50 and throw this error?


Solution

  • After help from members from above replay answers i got finnaly working code:

    /* POSTER - resize */
                $remote_file = $castImages[$name];
                $new_width  = 296;
                $new_height = 436;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
                $image_p = null;
                $image = null;
    
                $new_width  = 74;
                $new_height = 109;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
                imagedestroy($image_p);
                imagedestroy($image);
                $image_p = null;
                $image = null;
    

    Using:

    $image_p = null;
    $image = null;
    

    It works now about 20minutes and script is working and not trowing error message.