加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP编程:php使用Imagick生成图片的方法

发布时间:2020-12-13 02:48:21 所属栏目:PHP教程 来源:网络整理
导读:《php使用Imagick生成图片的方法》要点: 本文介绍了php使用Imagick生成图片的方法,希望对您有用。如果有疑问,可以联系我们。 PHP实例 本篇章节讲解php使用Imagick生成图片的办法.供大家参考研究.具体如下: PHP实例 这里使用Imagick 生成图片 办

《php使用Imagick生成图片的方法》要点:
本文介绍了php使用Imagick生成图片的方法,希望对您有用。如果有疑问,可以联系我们。

PHP实例本篇章节讲解php使用Imagick生成图片的办法.分享给大家供大家参考.具体如下:

PHP实例这里使用Imagick 生成图片
办理了图片写中文文字乱码问题,添加支持的字体

PHP实例
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,$style);
   $this->add_text($imagick,5,$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,$style);
  header ( 'Content-type: ' . strtolower ($imagick->getImageFormat ()) );
  echo $imagick->getImagesBlob ();
}
// 添加水印文字
public function add_text(& $imagick,$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,$text );
  }
}

PHP实例希望本文所述对大家的php程序设计有所帮助.

编程之家培训学院每天发布《php使用Imagick生成图片的方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读