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 preg_replace替换实例讲解

php preg_replace替换实例讲解

复制代码 代码如下:mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $l...

PHP调试函数和日志记录函数分享

网站程序开发过程经常需要调试,发布阶段也需要记录运行日志,方便发现问题和还原事件。这就要求有调试和日志记录功能。 下面分别写了用于调试的函数和用于记录错误的函数。 使用方法很简单,且自动...

highchart数据源纵轴json内的值必须是int(详解)

var users = ["0","0","0","0","0","2","0"]; 这样的纵轴数据是不显示的,json里面的值必须是int类型。 必须是这样: var...

PHP获取指定函数定义在哪个文件中以及其所在的行号实例

当调试开源的代码时,希望查看某个函数的定义,那么就需要定位其位置。在 zend studio 这样的 IDE 中自是可以自动提示到,但当没有安装这样的开发工具时,我们可以怎么办呢?参考如...

php中array_multisort对多维数组排序的方法

本文实例讲述了php中array_multisort对多维数组排序的方法。分享给大家供大家参考。具体实现方法如下: function sort_array($array, $keyi...