首頁  >  文章  >  後端開發  >  php基於Imagick實作添加浮水印、文字的圖片功能

php基於Imagick實作添加浮水印、文字的圖片功能

墨辰丷
墨辰丷原創
2018-06-07 17:43:223362瀏覽

本篇文章主要介紹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實作fpm開啟狀態統計的方法

PHP實作多種序列化/反序列化的方法

PHP物件導向之工作單元實例講解

以上是php基於Imagick實作添加浮水印、文字的圖片功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn