我自己整理 DZ 的GD类~ 作者:DZ-monkeye 整理加注:haierspi
IMAGE类变量含义 Haierspi原创
PHP代码
<? php
//DZ IMAGE类包含变量含义 >Haierspi
$imagelib = 0;
//水印模式 0为使用GD库 1为外置IM处理
$imageimpath = ”;
//IM执行路径
$thumbstatus = 0;
//缩略图功能状态 0功能不启用 1自动缩略图 2指定大小的缩略图
$thumbwidth = ”;;
//缩略图宽度
$thumbheight = ”;;
//缩略图高度
$watermarkstatus = ”;;
//水印状态
/*
0 不启用缩略图
以下是各个位置对应的值:
1 2 3
4 5 6
7 8 9
比如值为1 则在图片左上角添加水印
*/
$watermarkminwidth = 300;
$watermarkminheight = 300;
// 水印功能开启的最小宽度和最小高度 当低于这个值时 不启用水印
$watermarktrans = 50;
//水印效果融合度 这个对GIF图片有效
$watermarkquality = 100;
//水印图片压缩质量
$watermarktype = 1;
//水印类型 0 gif ;1 PNG ;2 文本水印
$watermarktext = array();
//文字水印的一些参数 这个变量是ARRAY 值对应以下:
$watermarktext[text] = ”;
//文字水印内容
$watermarktext[fontpath] = ”;
//文字水印字体路径
$watermarktext[size] = ”;
//文字水印字体大小
$watermarktext[angle] = ”;
//文字水印字体角度
$watermarktext[color] = ”;
//文字水印字体颜色
$watermarktext[shadowx] = ”;
//文字水印投影X偏移
$watermarktext[shadowy] = ”;
//文字水印投影Y偏移
$watermarktext[shadowcolor] = ”;
//文字水印投影颜色
//IM 变量含义 略… 自己想吧 呵呵
class Image {
var $attachinfo = '';
var $srcfile = '';
var $targetfile = '';
var $imagecreatefromfunc = '';
var $imagefunc = '';
var $attach = array();
var $animatedgif = 0;
function Image($srcfile, $targetfile, $attach = array()) {
global $imagelib, $watermarktext;
$this - > srcfile = $srcfile;
$this - > targetfile = $targetfile;
$this - > attach = $attach;
$this - > attachinfo = @getimagesize($targetfile); //返回图片信息
if (!$imagelib) {
switch ($this - > attachinfo['mime']) {
case 'image/jpeg':
$this - > imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
$this - > imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
break;
case 'image/gif':
$this - > imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
$this - > imagefunc = function_exists('imagegif') ? 'imagegif' : '';
break;
case 'image/png':
$this - > imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
$this - > imagefunc = function_exists('imagepng') ? 'imagepng' : '';
break;
}
} else {
$this - > imagecreatefromfunc = $this - > imagefunc = TRUE;
}
$this - > attach['size'] = emptyempty($this - > attach['size']) ? @filesize($targetfile) : $this - > attach['size'];
//取得图像大小
if ($this - > attachinfo['mime'] == 'image/gif') {
$fp = fopen($targetfile, 'rb');
$targetfilecontent = fread($fp, $this - > attach['size']);
fclose($fp);
$this - > animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === FALSE ? 0 : 1;
//动画的GIF 里确实有 “NETSCAPE2.0” 这个字符
//静态GIF 里没有 “NETSCAPE2.0” 字符,哈哈 郁闷了我好长时间
}
}
//缩略图函数
function Thumb($thumbwidth, $thumbheight, $preview = 0) {
global $imagelib, $imageimpath, $thumbstatus, $watermarkstatus;
//是否执行水印功能 $imageimpath 这个是ImageMagick的安装路径
$imagelib && $imageimpath ? $this - > Thumb_IM($thumbwidth, $thumbheight, $preview) : $this - > Thumb_GD($thumbwidth, $thumbheight, $preview);
if ($thumbstatus == 2 && $watermarkstatus) {
$this - > Image($this - > srcfile, $this - > targetfile, $this - > attach);
$this - > attach['size'] = filesize($this - > targetfile);
}
}
//水印函数
function Watermark($preview = 0) {
global $imagelib, $imageimpath, $watermarktype, $watermarktext, $watermarkminwidth, $watermarkminheight;
if (($watermarkminwidth && $this - > attachinfo[0] <= $watermarkminwidth && $watermarkminheight && $this - > attachinfo[1] <= $watermarkminheight) || ($watermarktype == 2 && (!file_exists($watermarktext['fontpath']) || !is_file($watermarktext['fontpath'])))) {
return;
}
$imagelib && $imageimpath ? $this - > Watermark_IM($preview) : $this - > Watermark_GD($preview);
}
//GD库函数
function Thumb_GD($thumbwidth, $thumbheight, $preview = 0) {
global $thumbstatus;
if ($thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
$imagecreatefromfunc = $this - > imagecreatefromfunc;
$imagefunc = $thumbstatus == 1 ? 'imagejpeg' : $this - > imagefunc;
list($img_w, $img_h) = $this - > attachinfo;
if (!$this - > animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
$attach_photo = $imagecreatefromfunc($this - > targetfile);
$x_ratio = $thumbwidth / $img_w;
$y_ratio = $thumbheight / $img_h;
if (($x_ratio * $img_h) < $thumbheight) {
$thumb['height'] = ceil($x_ratio * $img_h);
$thumb['width'] = $thumbwidth;
} else {
$thumb['width'] = ceil($y_ratio * $img_w);
$thumb['height'] = $thumbheight;
}
$targetfile = !$preview ? ($thumbstatus == 1 ? $this - > targetfile.
'.thumb.jpg' : $this - > targetfile) : DISCUZ_ROOT.
'./forumdata/watermark_temp.jpg';
$thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
imageCopyreSampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $img_w, $img_h);
if ($this - > attachinfo['mime'] == 'image/jpeg') {
$imagefunc($thumb_photo, $targetfile, 100);
} else {
$imagefunc($thumb_photo, $targetfile);
}
$this - > attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
}
}
}
function Watermark_GD($preview = 0) {
global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext;
$watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
if ($watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) {
$imagecreatefromfunc = $this - > imagecreatefromfunc;
$imagefunc = $this - > imagefunc;
list($img_w, $img_h) = $this - > attachinfo;
if ($watermarktype < 2) {
$watermark_file = $watermarktype == 1 ? './images/common/watermark.png' : './images/common/watermark.gif';
$watermarkinfo = @getimagesize($watermark_file);
$watermark_logo = $watermarktype == 1 ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file);
if (!$watermark_logo) {
return;
}
list($logo_w, $logo_h) = $watermarkinfo;
} else {
$box = imagettfbbox($watermarktext['size'], $watermarktext['angle'], $watermarktext['fontpath'], $watermarktext['text']);
$logo_h = max($box[1], $box[3])– min($box[5], $box[7]);
$logo_w = max($box[2], $box[4])– min($box[0], $box[6]);
$ax = min($box[0], $box[6]) * -1;
$ay = min($box[5], $box[7]) * -1;
}
$wmwidth = $img_w– $logo_w;
$wmheight = $img_h– $logo_h;
if (($watermarktype < 2 && is_readable($watermark_file) || $watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this - > animatedgif) {
switch ($watermarkstatus) {
case 1:
$x = +5;
$y = +5;
break;
case 2:
$x = ($img_w– $logo_w) / 2;
$y = +5;
break;
case 3:
$x = $img_w– $logo_w– 5;
$y = +5;
break;
case 4:
$x = +5;
$y = ($img_h– $logo_h) / 2;
break;
case 5:
$x = ($img_w– $logo_w) / 2;
$y = ($img_h– $logo_h) / 2;
break;
case 6:
$x = $img_w– $logo_w;
$y = ($img_h– $logo_h) / 2;
break;
case 7:
$x = +5;
$y = $img_h– $logo_h– 5;
break;
case 8:
$x = ($img_w– $logo_w) / 2;
$y = $img_h– $logo_h– 5;
break;
case 9:
$x = $img_w– $logo_w– 5;
$y = $img_h– $logo_h– 5;
break;
}
$dst_photo = imagecreatetruecolor($img_w, $img_h);
$target_photo = @$imagecreatefromfunc($this - > targetfile);
imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h);
if ($watermarktype == 1) {
imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
}
elseif($watermarktype == 2) {
if (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor']) {
$shadowcolorrgb = explode(',', $watermarktext['shadowcolor']);
$shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax + $watermarktext['shadowx'], $y + $ay + $watermarktext['shadowy'], $shadowcolor, $watermarktext['fontpath'], $watermarktext['text']);
}
$colorrgb = explode(',', $watermarktext['color']);
$color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax, $y + $ay, $color, $watermarktext['fontpath'], $watermarktext['text']);
} else {
imageAlphaBlending($watermark_logo, true);
imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans);
}
$targetfile = !$preview ? $this - > targetfile : DISCUZ_ROOT.
'./forumdata/watermark_temp.jpg';
if ($this - > attachinfo['mime'] == 'image/jpeg') {
$imagefunc($dst_photo, $targetfile, $watermarkquality);
} else {
$imagefunc($dst_photo, $targetfile);
}
$this - > attach['size'] = filesize($this - > targetfile);
}
}
}
//ImageMagick的函数定义 Magickwand
function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
global $thumbstatus, $imageimpath;
if ($thumbstatus) {
list($img_w, $img_h) = $this - > attachinfo;
$targetfile = !$preview ? ($thumbstatus == 1 ? $this - > targetfile.
'.thumb.jpg' : $this - > targetfile) : DISCUZ_ROOT.
'./forumdata/watermark_temp.jpg';
if (!$this - > animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
$exec_str = $imageimpath.
'/convert -geometry '.$thumbwidth.
'x'.$thumbheight.
' '.$this - > targetfile.
" ".$targetfile;@
exec($exec_str, $output, $return);
if (emptyempty($return) && emptyempty($output)) {
$this - > attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
}
}
}
}
function Watermark_IM($preview = 0) {
global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
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.
'%' : '').
' -gravity '.$gravity.
' '.$watermark_file.
' '.$this - > targetfile.
' '.$targetfile;
} else {
$watermarktext['text'] = str_replace(array("\n", "\r", "'"), array('', '', '\''), $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'.
' -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'].
' \''.$watermarktext['text'].
'\'"' : '').
' -fill "rgb('.$watermarktext['color'].
')"'.
' -draw "'.
' gravity '.$gravity.$translate.$skewX.$skewY.
' rotate '.$watermarktext['angle'].
' text 0,0 \''.$watermarktext['text'].
'\'"'.
' '.$this - > targetfile.
' '.$targetfile;
}@
exec($exec_str, $output, $return);
if (emptyempty($return) && emptyempty($output)) {
$this - > attach['size'] = filesize($this - > targetfile);
}
}
}