本篇文章主要介绍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免费学习笔记(深入)”;
功能介绍:后台功能介绍1.系统管理:(1)基本信息管理包括网站名称,域名,管理员昵称,联系电话,邮箱和网站关键字等等的设置。(2) 密码修改 系统管理员密码修改(3)后台登陆记录查看2.新闻管理:(1)新闻管理包括新闻的修改删除(2)添加新闻可添加文字新闻和图片新闻,采用EWEB编辑器操作简便(3)新闻分类管理可自由的添加和删除新闻分类3.产品管理:(1)管理产品可对数据库现有进行修改和删除(2)
0
以上就是php基于Imagick实现添加水印、文字的图片功能的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号