温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php怎么使用Imagick生成图片

发布时间:2021-08-04 10:00:35 来源:亿速云 阅读:181 作者:chen 栏目:开发技术

这篇文章主要介绍“php怎么使用Imagick生成图片”,在日常操作中,相信很多人在php怎么使用Imagick生成图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php怎么使用Imagick生成图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

本文实例讲述了php使用Imagick生成图片的方法。分享给大家供大家参考。具体如下:

这里使用Imagick 生成图片
解决了图片写中文文字乱码问题,添加支持的字体

public function getPic(){
  header('Content-Type: text/html; charset=utf-8');
  $text = '中粮屯河(sh700737)';//中粮屯河(sh700737)
  $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怎么使用Imagick生成图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI