PHP Image Resize Fatal error: Out of memory
PHP Image Resize Fatal error: Out of memory
我有以下 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');
但我收到相同的错误消息..那么如何修复此错误以便脚本重命名所有 5500 张图片而不仅仅是 50 张并抛出此错误?
在成员的帮助下重播答案后,我得到了最终的工作代码:
/* 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 分钟,脚本可以正常工作,不会出现错误消息。
我有以下 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');
但我收到相同的错误消息..那么如何修复此错误以便脚本重命名所有 5500 张图片而不仅仅是 50 张并抛出此错误?
在成员的帮助下重播答案后,我得到了最终的工作代码:
/* 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 分钟,脚本可以正常工作,不会出现错误消息。