php使用Imagick生成图片的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

PHP 抓取网页图片并且另存为的实现代码

下面是源代码,及其相关解释 复制代码 代码如下: <?php //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字 //默认把图片放在以此脚本相同的目...

php类的自动加载操作实例详解

本文实例讲述了php类的自动加载操作。分享给大家供大家参考,具体如下: 类的自动加载 在外面的页面中,并不需要去引入类文件,但程序会在需要一个类的时候自动去“动态加载”该类。 ① 创建一...

PHPMailer的主要功能特点和简单使用说明

支持邮件 s/mime加密的数字签名 支持邮件多个 TOs, CCs, BCCs and REPLY-TOs 可以工作在任何服务器平台,所以不用担心WIN平台无法发送邮件的问题的 支持文...

PHP设计模式之模板模式定义与用法详解

PHP设计模式之模板模式定义与用法详解

本文实例讲述了PHP设计模式之模板模式定义与用法。分享给大家供大家参考,具体如下: 模板模式准备一个抽象类,将部分逻辑以具体方法以及具体构造形式实现,然后声明一些抽象方法来迫使子类实现剩...

PHP实现的蚂蚁爬杆路径算法代码

本文实例讲述了PHP实现的蚂蚁爬杆路径算法代码。分享给大家供大家参考,具体如下: <?php /** * 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘...