找回密码
 立即注册
搜索
查看: 418|回复: 0

[基础] [imagick]利用php的imagick给gif图片加水印[转载]

  [复制链接]
发表于 2023-2-23 10:31 | 显示全部楼层 |阅读模式

尝试多次直接用GD库给gif图片加水印均未达到理想效果后,决定用imagick扩展给gif图片加水印,虽然帧数太多的情况下速度不太给力外,效果蛮理想。

开启imagick扩展,可参考http://www.vpser.net/manage/lnmp-lnmpa-imagemagick-install-tutorial.html

果断先上图:

再上代码:

 

$image_name = '01351346.gif';
//$image_name = 'Left_spinning_dancer.gif';
//$image_name = 'gifmerge.gif';

function createTextImagickDraw($fontSize=12, $fillColor='', $underColor='', $font='msyh.ttf'){
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($fontSize);
//$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);//设置水印位置
if(!empty($underColor)) $draw->setTextUnderColor(new ImagickPixel($underColor));
if(!empty($fillColor)) $draw->setFillColor(new ImagickPixel($fillColor));

return $draw;
}

function createWaterImagickDraw($waterImg='water.png',$x=10,$y=85,$width=16,$height=16){
$water = new Imagick($waterImg);
//$second->setImageOpacity (0.4);//设置透明度
$draw = new ImagickDraw();
//$draw->setGravity(Imagick::GRAVITY_CENTER);//设置位置
$draw->composite($water->getImageCompose(), $x, $y, $width, $height,$water);

return $draw;
}

$image = new Imagick($image_name);

$animation = new Imagick();
$animation->setFormat( "gif" );
$image = $image->coalesceImages();
$unitl = $image->getNumberImages();

for ($i=0; $i<$unitl; $i++) {
$image->setImageIndex($i);
$thisimage = new Imagick();
$thisimage->readImageBlob($image);
$delay = $thisimage->getImageDelay();
$thisimage->annotateImage(createTextImagickDraw(12, 'red'), 30, 100, 0, '阿维卡');
$thisimage->annotateImage(createTextImagickDraw(12, 'green'), 10, 120, 0, 'http://kller.cn');
$thisimage->annotateImage(createTextImagickDraw(12, 'blue'), 10, 140, 0, 'http://www.aweika.com');
$thisimage->drawImage(createWaterImagickDraw('f.jpg'));
$animation->addImage($thisimage);
$animation->setImageDelay( $delay );
}

$animation->writeImages('new/'.$image_name, true);

header( "Content-Type: image/gif" );

echo $animation->getImagesBlob();
 

 

附件:(你们懂的)

imagick.zip

原文地址:http://type.so/php/gif-add-watermark-with-imagick.html

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|学习笔记

GMT+8, 2024-12-22 15:16 , Processed in 0.036172 second(s), 13 queries , APCu On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表