php Imagick生成图片的实例
时间:2016-03-26
本文章向码农们介绍php使用Imagick扩展来生成图片,并在图片上添加水印、文字等,注意一定的阅读价值,需要的码农可以参考一下。
本文实例讲述了php使用Imagick生成图片的方法。分享给大家供大家参考。具体如下:
这里使用Imagick 生成图片
解决了图片写中文文字乱码问题,添加支持的字体
php 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概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Python PyQt5整理介绍
- django API 中接口的互相调用实例
- Python Numpy中数据的常用保存与读取方法
- python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
- Django Admin设置应用程序及模型顺序方法详解
- 在 Pycharm 安装使用black的方法详解
- Android开发手机无线调试的方法
- Android Studio Gradle插件版本与Gradle版本之间的对应关系
- Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔
- Android自定义View实现简单文字描边功能
- 在脚本中单独使用django的ORM模型详解
- Android5.0之Activity的转场动画的示例
- Django-rest-framework中过滤器的定制实例
- Python greenlet和gevent使用代码示例解析
- 完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误