ImageMagick是功能强大的服务器端图片处理工具,比GD强大多了。很多PHP的网站随着应用的广泛,开始用到ImageMagick,虽然其配置并不复杂,但如果几个需要注意得地方没有注意,恐怕就要浪费很多时间和精力了。本文意在帮这些人,尽量一次就安装配置成功。
本文参考了国内外大量的资料。包括ImageMagick官方文档。
第一步,下载编译ImageMagick.
注意要点,1、版本,版本要和下面的Magickwand兼容,否则编译中会出错。2,加入–enable-shared –with-modules 选项,否则可能最后MagickWand for PHP无法编译成功。
tar -jxvf ImageMagick-6.3.3-10.tar.bz2
cd ImageMagick-6.3.3
./configure –enable-shared –enable-lzw –without-perl –with-modules
make
make install
编译完成
第二步,安装 Magickwand for PHP
MagickWandForPHP-1.0.8.tar.gz
/usr/local/php/bin/phpize
./configure --enable-shared --with-magickwand=shared,/usr/local/ImageMagick --enable-module=shared,magickwand --with-php-config=/usr/local/php/bin/php-config
(请注意–enable-shared 参数,别漏掉,否则可能编译出的.so无法加载)
make
make install
第三步,安装 imagick
imagick-2.3.0.tgz
/usr/local/php/bin/phpize
./configure --prefix=/usr/local/imagick --enable-shared --with-php-config=/usr/local/php-5.2.8/bin/php-config --with-imagick=/usr/local/ImageMagick --with-php-config=/usr/local/php/bin/php-config
make
make install
其他问题:
如果MagickWand 执行./configure 时,出现checking for MagickSetImageTicksPerSecond in -lWand… NO的错误,可能是您现在安装了两个版本的ImageMagick,且默认的版本低于ImageMagick-6.3.3
可以用Magick-config –version命令查看其版本。
因为最近一段时间发生了一点事所以没有更新BLOG。不太好意思。
前几天有一个朋友找到我,说在imagewand上生成的动态GIF一直有问题。
本想研究一下,但后来经过沟通imagick也可以,就答应他搞一个出来。
PHP代码
<?PHP
$imagedraw = new Imagick();
$pixel = new ImagickPixel( 'gray' );
$pixel->setColor('black');
$imagedraw->newImage(100, 75, $pixel);
$draw = new ImagickDraw();
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 12 );
$image=new Imagick();
$animation = new Imagick();
$animation->setFormat( "gif" );
$image->readImage("old.gif");
$unitl = $image->getImageIndex();
$image->writeImages('animation.gif',false);
$delay = $image->getImageDelay();
$filename = 'animation-';
for ($i=0; $i<$unitl; $i++) {
$thisimage = new Imagick();
$thisimage->readImage($filename.$i.'.gif');
$thisimage->annotateImage($draw, 0, 12, 0, 'copyright by mpeg');
$animation->addImage($thisimage);
$animation->setImageDelay( $delay );
}
header( "Content-Type: image/gif" );
echo $animation->getImagesBlob();
?>
特贴上Helven兄改后的代码,可以减少IO操作
PHP代码
<?PHP
$draw = new ImagickDraw();
$draw->setFont('simsun.ttc');
$draw->setFontSize( 12 );
$text = iconv('GB2312', 'UTF-8', '网易');
$image=new Imagick();
$animation = new Imagick();
$animation->setFormat( "gif" );
$image->readImage("52924.gif");
$unitl = $image->getNumberImages();
for ($i=0; $i<$unitl; $i++) {
$image->setImageIndex($i);
$thisimage = new Imagick();
$thisimage->readImageBlob($image);
$delay = $thisimage->getImageDelay();
$thisimage->annotateImage($draw, 0, 12, 0, $text);
$animation->addImage($thisimage);
$animation->setImageDelay( $delay );
}
header( "Content-Type: image/gif" );
echo $animation->getImagesBlob();
?>
鄂ICP备07011897号