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

伊戈尔·彼得夫(Igor Petev)

我有以下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,并抛出此错误?

伊戈尔·彼得夫(Igor Petev)

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

/* 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分钟,脚本正在运行,并且没有出现错误消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章