与GD相比,ImageMagic for php缓慢

时间:2016-08-19 12:28:38

标签: php linux imagemagick gd

我运行一个小基准。使用GD和ImageMagick进行相同的简单文本绘制操作请参阅下面的代码。

在开发服务器上,GD版本在1.9秒内运行,ImageMagic在7.3秒内运行。这在多个测试中是一致的。

在Linux上,GD版本在2.5秒内运行,IM在4秒内运行(包括圆形提示,但测试在同一服务器/客户端上多次完成并且一致)

是什么让IM慢于GD?有没有办法优化它我能做些什么来获得相同的性能?

基准代码GD:

$base_image=imagecreatetruecolor ( 350, 700 );

$white  = imagecolorallocatealpha($base_image, 255, 255, 255, 100);
imagefill($base_image , 0, 0, $white);

imagealphablending($base_image, true);
imagesavealpha($base_image, true);


$black = imagecolorallocatealpha ($base_image, 0,0,0, 1);

for ($i =0; $i<700; $i ++) {

imagettftext($base_image, 13, 0, 20,  $i, $black, './arial.ttf', "Hello World!"); 

imagettftext($base_image, 13, 0, 20,  $i, $black, './arib____.ttf', "Hello World!");

imagettftext($base_image, 13, 0, 20,  $i, $black, './verdana.ttf', "Hello World!");

imagettftext($base_image, 13, 0, 20,  $i, $black, './trebuc.ttf', "Hello World!");

imagettftext($base_image, 13, 0, 20,  $i, $black, './georgia.ttf', "Hello World!");

}


header("Content-Type: image/png");
imagepng($base_image);

基准代码IM:

$canvas = new Imagick();

$canvas->newImage(350, 700, "white");

$draw = new ImagickDraw();



$draw->setFontSize(13*96/72);

for ($i =0; $i<700; $i ++) {    

$draw->setFont('./arial.ttf');
$draw->annotation(20, $i, "Hello World!");

$draw->setFont('./arib____.ttf');
$draw->annotation(20, $i, "Hello World!");

$draw->setFont('./verdana.ttf');
$draw->annotation(20, $i, "Hello World!");

$draw->setFont('./trebuc.ttf');
$draw->annotation(20, $i, "Hello World!");

$draw->setFont('./georgia.ttf');
$draw->annotation(20, $i, "Hello World!");

}

$canvas->drawImage($draw);

$canvas->setImageFormat('png');
header("Content-Type: image/png");
echo $canvas;

GD信息

GD Support  enabled
GD Version  2.1.1-dev
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.5.2
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version 8
PNG Support enabled
libPNG Version  1.2.50
WBMP Support    enabled
XPM Support enabled
libXpm Version  30411
XBM Support enabled
WebP Support    enabled

Imagick Info

imagick module  enabled imagick module version  3.1.2 imagick classes   Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator ImageMagick version    ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org ImageMagick copyright    Copyright (C) 1999-2012 ImageMagick Studio LLC ImageMagick release date 2016-06-01 ImageMagick number of supported formats: 204 ImageMagick supported formats   3FR, A, AAI, AI, ART, ARW, AVI, AVS, B, BGR, BGRA, BIE, BMP, BMP2, BMP3, BRF, C, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DFONT, DNG, DPX, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, FAX, FITS, FRACTAL, FTS, G, G3, GIF, GIF87, GRADIENT, GRAY, GROUP4, HALD, HDR, HISTOGRAM, HRZ, HTM, HTML, ICB, ICO, ICON, INFO, INLINE, IPL, ISOBRL, J2C, J2K, JBG, JBIG, JNG, JP2, JPC, JPEG, JPG, JPX, K, K25, KDC, LABEL, M, M2V, M4V, MAC, MAP, MAT, MATTE, MEF, MIFF, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MTV, MVG, NEF, NRW, NULL, O, ORF, OTB, OTF, PAL, PALM, PAM, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PGX, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG24, PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSB, PSD, PTIF, PWP, R, RADIAL-GRADIENT, RAF, RAS, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, SR2, SRF, STEGANO, SUN, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UBRL, UIL, UYVY, VDA, VICAR, VID, VIFF, VST, WBMP, WMV, WPG, X, X3F, XBM, XC, XCF, XPM, XPS, XV, XWD, Y, YCbCr, YCbCrA, YUV

Directive   Local Value Master Value imagick.locale_fix 0   0 imagick.progress_monitor  0   0

1 个答案:

答案 0 :(得分:0)

这可能是由于GD使用比特深度= 16对比特深度= 8而不是IM。尝试

$draw -> setImageDepth ( 8 );

或者,如果您可以控制服务器,请尝试安装&#34; Q8&#34;构建ImageMagick代替&#34; Q16&#34;你现在拥有的构建(当你正在构建它时,获得当前版本的ImageMagick; 6.7.7已经很老了。)