• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php使用Imagick生成图片的方法_php技巧

php 搞代码 3年前 (2022-01-26) 38次浏览 已收录 0个评论

本文实例讲述了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<div>……本2文来源gaodai.ma#com搞##代!^码@网3</div><code>搞代gaodaima码</code>_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程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php使用Imagick生成图片的方法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址