php给图片加水印函数

作者:袖梨 2022-07-02

function Watermark_IM($preview = 0) {
  global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
  $watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
  switch($watermarkstatus) {
   case 1:
    $gravity = 'NorthWest';
    break;
   case 2:
    $gravity = 'North';
    break;
   case 3:
    $gravity = 'NorthEast';
    break;
   case 4:
    $gravity = 'West';
    break;
   case 5:
    $gravity = 'Center';
    break;
   case 6:
    $gravity = 'East';
    break;
   case 7:
    $gravity = 'SouthWest';
    break;
   case 8:
    $gravity = 'South';
    break;
   case 9:
    $gravity = 'SouthEast';
    break;
  }
  $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  if($watermarktype < 2) {
   $watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
   $exec_str = $imageimpath.'/composite'.
    ($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
    ' -quality '.$watermarkquality.
    ' -gravity '.$gravity.
    ' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
  } else {
   $watermarktextcvt = str_replace(array("n", "r", "'"), array('', '', '''), pack("H*", $watermarktext['text']));
   $watermarktext['angle'] = -$watermarktext['angle'];
   $translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
   $skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
   $skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
   $exec_str = $imageimpath.'/convert'.
    ' -quality '.$watermarkquality.
    ' -font "'.$watermarktext['fontpath'].'"'.
    ' -pointsize '.$watermarktext['size'].
    (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
     ' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
     ' -draw "'.
      ' gravity '.$gravity.$translate.$skewX.$skewY.
      ' rotate '.$watermarktext['angle'].
      ' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' ''.$watermarktextcvt.''"' : '').
    ' -fill "rgb('.$watermarktext['color'].')"'.
    ' -draw "'.
     ' gravity '.$gravity.$translate.$skewX.$skewY.
     ' rotate '.$watermarktext['angle'].
     ' text 0,0 ''.$watermarktextcvt.''"'.
    ' '.$this->targetfile.' '.$targetfile;
  }
  @exec($exec_str, $output, $return);
  if(empty($return) && empty($output)) {
   $this->attach['size'] = filesize($this->targetfile);
  }
 }

相关文章

精彩推荐