PHP图像调整大小致命错误:内存不足

时间:2018-10-13 17:55:36

标签: php memory out

我有以下PHP代码将照片调整为所需大小:

/* 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);

我大约需要调整5500张图片的大小,因此我将这段代码运行到while循环中,并从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

然后我在PHP脚本中添加以下代码:

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

但是我收到相同的错误消息..so如何解决此错误,以便脚本重命名所有5500张图片而不仅仅是50张图片并抛出此错误?

1 个答案:

答案 0 :(得分:0)

在上述成员的帮助下,重播答案后,我得到了最终的工作代码:

/* 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;

使用:

$image_p = null;
$image = null;

现在可以工作约20分钟,脚本正在运行,并且没有出现错误消息。