使用imagemagick创建图片太慢了.如何提高?

Joh*_*ith 3 php imagemagick

我正在编写一个小CMS,你可以上传几个图像.这些图像通过imagemagick 转换为3个版本(大,中和缩略图大小).

问题是imagemagick需要5分钟才能创建4张图片的3个版本(已上传).

这是imagemagick命令的部分:

foreach($upIMGS as $key => $filename){
    list($width, $height) = getimagesize($path.$filename);
    if ($width > $height) $size = "x96";
    else $size = "96x";

    exec(P_IMAGEMAGICK." ".$path.$filename." -resize $size -gravity center -crop 96x96+0+0 +repage ".$path."th-".$filename);
    exec(P_IMAGEMAGICK." ".$path.$filename." -resize 320x320 ".$path."hl-".$filename);
    exec(P_IMAGEMAGICK." ".$path.$filename." -resize 514x ".$path."fl-".$filename);

    unlink($path.$filename);
}
Run Code Online (Sandbox Code Playgroud)

[$ upIMGS是一个包含最近上传图像的所有文件名的数组]

我的意思是..它确实有效,但太慢了,5分钟后服务器给我一个错误.一些文件是生成的,有些不是......

如果你能给我一个提示,那将是非常好的.

jnt*_*jns 7

我最近遇到了同样的问题,但我只是通过图像一次将它们从原来的2592x1944调整为300xbestFit或bestFitx300

我使用的是PHP类imagick而不是命令行,但是在我的情况下,通过更改为-scale或scaleImage,我将时间缩短了一半.这是我的测试代码的片段.

while ($images = readdir($handle)) {
    // check to see if the first or second character is a '.' or '..', 
    // if so then remove from list
    if (substr($images,0,1) != '.') {
        //Check files to see if there extensions match any of the following image extensions. 
        // GLOB_BRACE looks for all glob criteria within the {braces}
        $images = glob($dir."{*.gif,*.jpg,*.png,*.jpeg}", GLOB_BRACE);
        // the glob function gives us an array of images
        $i = 0;
        foreach ($images as $image) {
            // parse the data given and remove the images/ $dir, 
            // imagemagick will not take paths, only image names.
            $i++;
            list ($dir, $image) = split('[/]', $image);
            echo $i, " ", $image, "<br />";

            $magick = new Imagick($dir."/".$image);
            $imageprops = $magick->getImageGeometry();

            if ($imageprops['width'] <= 300 && $imageprops['height'] <= 300) {
                // don't upscale
            } else {

                // 29 Images at 2592x1944 takes 11.555036068 seconds -> 
                // output size = 300 x 255
                $magick->scaleImage(300,300, true);

                // 29 Images at 2592x1944 takes 23.3927891254 seconds -> 
                // output size = 300 x 255
                //$magick->resizeImage(300,300, imagick::FILTER_LANCZOS, 0.9, true);
                $magick->writeImage("thumb_".$image);

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在2592x1944处理29张图像,从23.3927891254秒到11.555036068秒.我希望这有帮助.

编辑:

除了我上面所说的,我刚刚在ImageMagick v6示例 - API和脚本中遇到了以下内容,这可能会有所帮助:

  • "Shell脚本本质上很慢.它被解释,需要多个步骤和额外的文件处理到磁盘.这当然更好,这要归功于新的IM v6选项处理,允许您在一个命令中执行大量的图像处理操作即便如此,你很少能在一个convert命令中完成所有事情,因此你经常需要使用多个命令来实现你想要的目标."

  • "当读取大量或甚至大量图像时,最好使用" 读取修改器"来调整大小或裁剪它们,因为IM不会在完整图像中读取,因此会降低其内存需求."

  • "如果你将ImageMagick称为Apache模块,它也将减少启动时间,因为部件将被加载一次并且可以多次使用,而不是需要反复重载.这可能会在将来变得更加实用运行'守护进程'IM进程."