[英]PHP Fatal error: Allowed memory size
给定一大堆格式为jpg或png的无序图像,我想创建一个PHP脚本,该脚本将首先过滤所有文件夹内容以允许的格式,然后将它们复制到按数字顺序重命名的新文件夹中(1.jpg,2 .jpg,3.jpg ..),在子文件夹“ thumbs”中为每个图像创建50x50的缩略图,然后创建一个名为“ gallery”的.html文件,其中包含每个缩略图的“ img”标签的转储。
它可以正常工作直到十几张图像,然后超过最大可分配内存。 这突然发生了,并在调用imagecopyresize函数时出现。
任何建议表示赞赏。
资源:
<?php
# Prepare vars
$dir = "O:/zip/";
$newDir = "C:/Users/user/Desktop/zip/";
$thumbs = $newDir."thumbs/";
$gallery = $newDir."gallery.html";
$types = array(".jpg", ".png");
$files = array();
$tempFiles = scandir($dir);
$i = 0;
# Copy and rename images
foreach($tempFiles as $file)
{
$thisType = substr($file,-4);
if(in_array($thisType, $types))
{
$dest = fopen($newDir.$i.$thisType, 'w');
fwrite($dest, file_get_contents($dir.$file));
fclose($dest);
list($width, $height) = getimagesize($newDir.$i.$thisType);
$im = imagecreatetruecolor(50, 50);
if($thisType == '.jpg')
{
imagecopyresized($im, imagecreatefromjpeg($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
imagejpeg($im, $thumbs.$i.$thisType);
}
else
if($thisType == '.png')
{
imagecopyresized($im, imagecreatefrompng($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
imagepng($im, $thumbs.$i.$thisType);
}
imagedestroy($im);
$html .= "<a href='$newDir$i$thisType'><img src='$thumbs$i$thisType' alt='$i$thisType' width='50' height='50'></a>";
print "Successfully processed $i$thisType<br>";
$i++;
}
}
print "Done.<br>";
# Create html gallery for new imgs
$dest = fopen($gallery, 'w');
fwrite($dest, $html);
fclose($dest);
print "There are ".number_format($i)." image files.\n\r";
?>
您的代码看起来还可以。 第十二张图片有多大,比其他图片大? 查阅有关imagecopyresize ()@ php.net的文档,有人编写了setMemoryForImage()
函数,该函数确定'memory_limit'的当前php.ini设置是否允许调整给定图像的大小。
这使我相信,如果原始图像太大,则在尝试调整大小时会耗尽内存。
任何建议表示赞赏。
好吧,乍一看我看不到泄漏,但是您可以使用memory_get_usage()
打印出调试信息以发现不会释放内存的谎言。
或者它可能只是一个单独导致此错误的异常大的图像。 对于这种情况,yopu必须增加内存限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.