使用imagick在动态GIF上打文字水印

因为最近一段时间发生了一点事所以没有更新BLOG。不太好意思。
前几天有一个朋友找到我,说在imagewand上生成的动态GIF一直有问题。

本想研究一下,但后来经过沟通imagick也可以,就答应他搞一个出来。

PHP代码
01.02.$imagedraw = new Imagick();
03.$pixel = new ImagickPixel( 'gray' );
04.$pixel->setColor('black');
05.$imagedraw->newImage(100, 75, $pixel);
06.
07.$draw = new ImagickDraw();
08.$draw->setFont('Bookman-DemiItalic');
09.$draw->setFontSize( 12 );
10.
11.$image=new Imagick();
12.$animation = new Imagick();
13.$animation->setFormat( "gif" );
14.$image->readImage("old.gif");
15.$unitl = $image->getImageIndex();
16.$image->writeImages('animation.gif',false);
17.$delay = $image->getImageDelay();
18.$filename = 'animation-';
19.
20.for ($i=0; $i<$unitl; $i++) {
21. $thisimage = new Imagick();
22. $thisimage->readImage($filename.$i.'.gif');
23. $thisimage->annotateImage($draw, 0, 12, 0, 'copyright by mpeg');
24. $animation->addImage($thisimage);
25. $animation->setImageDelay( $delay );
26.}
27.
28.header( "Content-Type: image/gif" );
29.echo $animation->getImagesBlob();
30.
31.?>
特贴上Helven兄改后的代码,可以减少IO操作

PHP代码
01.02.
03.$draw = new ImagickDraw();
04.$draw->setFont('simsun.ttc');
05.$draw->setFontSize( 12 );
06.
07.$text = iconv('GB2312', 'UTF-8', '网易');
08.
09.$image=new Imagick();
10.$animation = new Imagick();
11.$animation->setFormat( "gif" );
12.$image->readImage("52924.gif");
13.$unitl = $image->getNumberImages();
14.
15.for ($i=0; $i<$unitl; $i++) {
16. $image->setImageIndex($i);
17. $thisimage = new Imagick();
18. $thisimage->readImageBlob($image);
19. $delay = $thisimage->getImageDelay();
20. $thisimage->annotateImage($draw, 0, 12, 0, $text);
21. $animation->addImage($thisimage);
22. $animation->setImageDelay( $delay );
23.}
24.
25.header( "Content-Type: image/gif" );
26.echo $animation->getImagesBlob();
27.
28.?>

相关文档
最新文档