我正在编写一个小小的CMS,在那里你可以上传几张图片。这些图像被转换成3个版本(大,中和缩略图大小)与图像。
问题是,imagemagick需要5分钟来创建这3个版本的4张图片(上传)。
下面是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);
}
$upIMGS是一个数组,包含最近上传的图像的所有文件名。
我是说..。它确实能工作,但是太慢了,5分钟后服务器给了我一个错误。有些文件是生成的,有些文件不是.
如果你能给我个提示就太好了。
发布于 2012-04-18 13:28:18
最近,我遇到了同样的问题,但我只浏览了一次图像,以便将它们从原来的2592x1944调整到300xdexFit或bestFitx300。
我使用的是PHP类,而不是命令行,但在我的情况下,我将时间改为-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);
}
}
}
}
我正在处理2592x1944的29幅图像,从23.3927891254秒上升到11.555036068秒。我希望这能帮到你。
编辑:
除了上面说的话,我还在ImageMagick v6 Examples -- API & Scripting上遇到了以下可能有帮助的内容
convert
命令中完成所有事情,因此您通常需要使用多个命令来实现您想要的结果。“发布于 2012-04-21 07:57:01
在将主映像加载到内存中并对其进行处理以生成其他映像时,此示例可能会有所帮助:
$cmd = " input.jpg \( -clone 0 -thumbnail x480 -write 480_wide.jpg +delete \)".
" \( -clone 0 -thumbnail x250 -write 250_wide.jpg +delete \) ".
" \( -clone 0 -thumbnail x100 -write 100_wide.jpg +delete \) -thumbnail 64x64! null: ";
exec("convert $cmd 64_square.jpg ");
这是创建4种不同大小的图像。
发布于 2012-04-17 11:03:03
等等什么?从三张上传的图片中生成12张图片不需要5分钟。
我看不到您的其余代码,但是为什么您的getimagesize路径是$path.$filename,而不是您的unlink P_UPLOADS.$value呢?他们有什么不同的原因吗?无论如何,$value从何而来,它没有在foreach()循环中定义。也许您只是有一个导致脚本挂起的bug。我使用了ImageMagick (虽然不是针对exec(),而是针对实际的类),而且速度非常快。
在foreach循环()上运行诊断信息了吗?就像打印出毫秒一样,执行每个exec()命令需要几毫秒时间。
https://stackoverflow.com/questions/10189881
复制相似问题