本文实例讲述了摘自织梦CMS中的图片处理类。分享给大家供大家参考。具体如下:
if($this->thumbstatus == 2 && $this->watermarkstatus)
{
$this->image($this->targetfile,$this->attach);
$this->attach['size'] = filesize($this->targetfile);
}
}
/**
-
图片水印
-
@access public
-
@param int $preview 是否预览
-
@return void
*/
function watermark($preview = 0)
{
if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)
{
return ;
}
$this->watermark_gd($preview);
}
/** -
@access public
-
@param int $thumbwidth 图片宽度
-
@param int $thumbheight 图片高度
-
@param int $preview 是否预览
-
@return void
/
function thumb_gd($thumbwidth,$preview = 0)
{
if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))
{
$imagecreatefromfunc = $this->imagecreatefromfunc;
$imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
list($imagewidth,$imageheight) = $this->attachinfo;
if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))
{
$attach_photo = $imagecreatefromfunc($this->targetfile);
$x_ratio = $thumbwidth / $imagewidth;
$y_ratio = $thumbheight / $imageheight;
if(($x_ratio $imageheight) < $thumbheight)
{
$thumb['height'] = ceil($x_ratio $imageheight);
$thumb['width'] = $thumbwidth;
}
else
{
$thumb['width'] = ceil($y_ratio $imagewidth);
$thumb['height'] = $thumbheight;
}
$targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
$thumb_photo = imagecreatetruecolor($thumb['width'],$thumb['height']);
imagecopyresampled($thumb_photo,$attach_photo,$thumb['width'],$thumb['height'],$imagewidth,$imageheight);
if($this->attachinfo['mime'] == 'image/jpeg')
{
$imagefunc($thumb_photo,$targetfile,100);
}
else
{
$imagefunc($thumb_photo,$targetfile);
}
$this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
}
}
}
/** -
使用gd进行水印
-
@access public
-
@param int $preview 是否预览
-
@return void
/
function watermark_gd($preview = 0)
{
if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge'))
{
$imagecreatefunc = $this->imagecreatefromfunc;
$imagefunc = $this->imagefunc;
list($imagewidth,$imageheight) = $this->attachinfo;
if($this->watermarktype < 2)
{
$watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
$watermarkinfo = @getimagesize($watermarkfile);
$watermarklogo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermarkfile);
if(!$watermarklogo)
{
return ;
}
list($logowidth,$logoheight) = $watermarkinfo;
}
else
{
$Box = @imagettfbBox($this->watermarktext['size'],$this->watermarktext['angle'],$this->watermarktext['fontpath'],$this->watermarktext['text']);
$logowidth = max($Box[2],$Box[4]) - min($Box[0],$Box[6]);
$logoheight = max($Box[1],$Box[3]) - min($Box[5],$Box[7]);
$ax = min($Box[0],$Box[6]) -1;
$ay = min($Box[5],$Box[7]) * -1;
}
$wmwidth = $imagewidth - $logowidth;
$wmheight = $imageheight - $logoheight;
if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)
{
switch($this->watermarkstatus)
{
case 1:$x = +5;
$y = +5;
break;
case 2:
$x = ($imagewidth - $logowidth) / 2;
$y = +5;
break;
case 3:
$x = $imagewidth - $logowidth - 5;
$y = +5;
break;
case 4:
$x = +5;
$y = ($imageheight - $logoheight) / 2;
break;
case 5:
$x = ($imagewidth - $logowidth) / 2;
$y = ($imageheight - $logoheight) / 2;
break;
case 6:
$x = $imagewidth - $logowidth - 5;
$y = ($imageheight - $logoheight) / 2;
break;
case 7:
$x = +5;
$y = $imageheight - $logoheight - 5;
break;
case 8:
$x = ($imagewidth - $logowidth) / 2;
$y = $imageheight - $logoheight - 5;
break;
case 9:
$x = $imagewidth - $logowidth - 5;
$y = $imageheight - $logoheight -5;
break;
}
$dst_photo = @imagecreatetruecolor($imagewidth,$imageheight);
$target_photo = $imagecreatefunc($this->targetfile);
imagecopy($dst_photo,$target_photo,$imageheight);
if($this->watermarktype == 1)
{
imagecopy($dstphoto,$watermarklogo,$x,$y,$logowidth,$logoheight);
}
elseif($this->watermarktype == 2)
{
if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'])
{
$shadowcolorrgb = explode(',',$this->watermarktext['shadowcolor']);
$shadowcolor = imagecolorallocate($dst_photo,$shadowcolorrgb[0],$shadowcolorrgb[1],$shadowcolorrgb[2]);
imagettftext($dst_photo,$this->watermarktext['size'],$x + $ax + $this->watermarktext['shadowx'],$y + $ay + $this->watermarktext['shadowy'],$shadowcolor,$this->watermarktext['text']);
}
$colorrgb = explode(',$this->watermarktext['color']);
$color = imagecolorallocate($dst_photo,$colorrgb[0],$colorrgb[1],$colorrgb[2]);
imagettftext($dstphoto,$x + $ax,$y + $ay,$color,$this->watermarktext['text']);
}
else
{
imagealphablending($watermarklogo,true);
imagecopymerge($dst_photo,$logoheight,$this->watermarktrans);
}
$targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
if($this->attachinfo['mime'] == 'image/jpeg')
{
$imagefunc($dst_photo,$this->watermarkquality);
}
else
{
$imagefunc($dst_photo,$targetfile);
}
$this->attach['size'] = filesize($this->targetfile);
}
}
}
}//End Class
希望本文所述对大家的PHP程序设计有所帮助。