本文实例讲述了php使用Imagick生成图片的方法。分享给大家供大家参考。具体如下:
这里使用Imagick 生成图片
解决了图片写中文文字乱码问题,添加支持的字体
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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程序设计有所帮助。
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 38
-
2025-05-26 25
-
2025-05-29 53
-
2025-05-29 43
-
2025-05-27 76
热门评论

