脚本宝典收集整理的这篇文章主要介绍了php – 用imagemagick创建图片太慢了.如何提高?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
问题是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); }
我使用的是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,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,imagick::FILTER_LANCZOS,0.9,true); $magick->writeImage("thumb_".$image); } } } }
我正在2592×1944处理29张图像,从23.3927891254秒到11.555036068秒.我希望这有帮助.
编辑:
除了我上面所说的,我刚刚在ImageMagick v6 Examples — API & Scripting遇到了以下内容,这可能会有所帮助:
>“Shell脚本本质上很慢.它被解释,需要
多个步骤和对磁盘的额外文件处理.这当然是
更好的归功于新的IM v6选项处理,让您可以做到
单个命令中的大量图像处理操作.甚至
所以你很少能在一个转换命令中做任何事情,所以
你经常需要使用多个命令来实现你想要的目标.“
>“当阅读大量甚至大量图像时,最好是
使用Read Modifier来调整大小,或裁剪它们,因为IM不会在完整图像中读取,从而减少了对内存的需求.“>“如果你将ImageMagick称为Apache模块,它也会减少启动时间,因为部件将被加载一次并保持可用多次使用,而不是一遍又一遍地重新加载.这可能通过永久运行,在未来变得更加实用‘守护进程’IM进程.“
以上是脚本宝典为你收集整理的php – 用imagemagick创建图片太慢了.如何提高?全部内容,希望文章能够帮你解决php – 用imagemagick创建图片太慢了.如何提高?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。