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中的Cannot modify header information 问题

完美解决PHP中的Cannot modify header information 问题

我就遇到这种问题,网上找到这个解决的方案,就收藏下写PHP的朋友们肯定遇到过这样一个问题:通过header函数改变http协议头的时候,会出现一个类似下面格式的warning:复制代码...

用PHP实现小写金额转换大写金额的代码(精确到分)

复制代码 代码如下: /** *数字金额转换成中文大写金额的函数 *String Int $num 要转换的小写数字或小写字符串 *return 大写字母 *小数位为两位 **/ fun...

PHP4和PHP5版本下解析XML文档的操作方法实例分析

本文实例讲述了PHP4和PHP5版本下解析XML文档的操作方法。分享给大家供大家参考,具体如下: 在PHP网站开发与建设过程中,时常会碰到需要对XML文档进行解析,PHP4版本自带了XM...

让PHP支持页面回退的两种方法[转]

在开发过程中,往往因为表单出错而返回页面的时候填写的信息都不见了,为了支持页面回跳,可以通过两种方法实现。 第一,使用Header方法设置消息头Cache-control header(...

合格的PHP程序员必备技能

作为PHP的爱好者,如果你想加入PHP程序的世界,一定要做好充分的准备。 如果想进入大的企业进行底层开发的话必须对互联网各方面的技术原理了解的很清楚,例如apache实现原理。语言方面既...