您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php基于Imagick实现添加水印、文字的图片功能

2025/4/28 20:09:26发布36次查看
本篇文章主要介绍php基于imagick实现添加水印、文字的图片功能,感兴趣的朋友参考下,希望对大家有所帮助。
这里使用imagick 生成图片
解决了图片写中文文字乱码问题,添加支持的字体
public function getpic(){ header('content-type: text/html; charset=utf-8'); $text = '中粮屯河(sh600737)';//中粮屯河(sh600737) $watermark = '305988103123zczcxzas'; $len = strlen($text); $width = 10.5*(($len-8)/3*2+8); $height = 26; $imagick = new imagick(); $color_transparent = new imagickpixel('#ffffff'); //transparent 透明色 $imagick->newimage($width, $height, $color_transparent, 'jpg'); //$imagick->borderimage('#000000', 1, 1); $style['font_size'] = 12; $style['fill_color'] = '#000000'; for($num= strlen($watermark); $num>=0; $num--){ $this->add_text($imagick,substr($watermark, $num,1), 2+($num*8), 30, 1,$style); $this->add_text($imagick,substr($watermark, $num,1), 2+($num*8), 5, 1,$style); } //return; $style['font_size'] = 20; $style['fill_color'] = '#ff0000'; $style['font'] = './msyh.ttf'; ///微软雅黑字体 解决中文乱码 //$text=mb_convert_encoding($text,'utf-8'); //iconv("gbk","utf-8//ignore",$text); $this->add_text($imagick,$text, 2, 20, 0,$style); header ( 'content-type: ' . strtolower ($imagick->getimageformat ()) ); echo $imagick->getimagesblob ();}// 添加水印文字public function add_text(& $imagick, $text, $x = 0, $y = 0, $angle = 0, $style = array()) { $draw = new imagickdraw (); if (isset ( $style ['font'] )) $draw->setfont ( $style ['font'] ); if (isset ( $style ['font_size'] )) $draw->setfontsize ( $style ['font_size'] ); if (isset ( $style ['fill_color'] )) $draw->setfillcolor ( $style ['fill_color'] ); if (isset ( $style ['under_color'] )) $draw->settextundercolor ( $style ['under_color'] ); if (isset ( $style ['font_family'] )) $draw->setfontfamily( $style ['font_family'] ); if (isset ( $style ['font'] )) $draw->setfont($style ['font'] ); $draw->settextencoding('utf-8'); if (strtolower ($imagick->getimageformat ()) == 'gif') { foreach ( $imagick as $frame ) { $frame->annotateimage ( $draw, $x, $y, $angle, $text ); } } else { $imagick->annotateimage ( $draw, $x, $y, $angle, $text ); }}
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php实现fpm开启状态统计的方法
php实现多种序列化/反序列化的方法
php面向对象之工作单元实例讲解
以上就是php基于imagick实现添加水印、文字的图片功能的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product