简体   繁体   English

PHP致命错误:允许的内存大小

[英]PHP Fatal error: Allowed memory size

Given a large set of unordered images in the format either jpg or png, I wanted to create a PHP script that would firstly filter all folder contents for the allowed formats, copy them to a new folder renamed in numerical order (1.jpg, 2.jpg, 3.jpg ..), create a 50x50 thumbnail of each image in a child folder "thumbs" and then create an .html file called "gallery" which contains a dump of "img" tags of each thumbnail. 给定一大堆格式为jpg或png的无序图像,我想创建一个PHP脚本,该脚本将首先过滤所有文件夹内容以允许的格式,然后将它们复制到按数字顺序重命名的新文件夹中(1.jpg,2 .jpg,3.jpg ..),在子文件夹“ thumbs”中为每个图像创建50x50的缩略图,然后创建一个名为“ gallery”的.html文件,其中包含每个缩略图的“ img”标签的转储。

It works fine up until a dozen or so images and then exceeds the maximum allocatable memory. 它可以正常工作直到十几张图像,然后超过最大可分配内存。 This has suddenly happened and appears when the function imagecopyresized is called. 这突然发生了,并在调用imagecopyresize函数时出现。

Any advice is appreciated. 任何建议表示赞赏。

Source: 资源:

<?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";

?>

Your code looks ok. 您的代码看起来还可以。 How large is the 12th image, is it larger than the others? 第十二张图片有多大,比其他图片大? Check out the docs for imagecopyresized () @ php.net, someone wrote a setMemoryForImage() function which determines if the current php.ini setting for 'memory_limit' will allow for a given image to be resized. 查阅有关imagecopyresize ()@ php.net的文档,有人编写了setMemoryForImage()函数,该函数确定'memory_limit'的当前php.ini设置是否允许调整给定图像的大小。

This leads me to believe that if the original image is too large, it will exhaust the memory when attempting to resize. 这使我相信,如果原始图像太大,则在尝试调整大小时会耗尽内存。

Any advice is appreciated. 任何建议表示赞赏。

Well, at first glance I don't see the leak, but you can use memory_get_usage() to print out debugging info to spot the lie which doesn't release the memory. 好吧,乍一看我看不到泄漏,但是您可以使用memory_get_usage()打印出调试信息以发现不会释放内存的谎言。

Or it can be just one exceptionally big image causing this error alone. 或者它可能只是一个单独导致此错误的异常大的图像。 For this case yopu have to increase memory limit. 对于这种情况,yopu必须增加内存限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM