给图片加水印类

set_time_limit(3000);
class GDMark {
var $sourcePath; //图片存储路径
var $galleryPath; //图片缩略图存储路径
var $toFile = false; //是否生成文件
var $WaterPos;
var $MarkSign = null;
var $fontName; //使用的TTF字体名称
var $maxWidth = 600; //图片最大宽度
var $maxHeight = 700; //图片最大高度

function GDMark($sourcePath, $galleryPath, $WaterPos, $fontPath, $MarkSign=”) {
$this->sourcePath = $sourcePath;
$this->galleryPath = $galleryPath;
$this->WaterPos=$WaterPos;
$this->fontName = $fontPath . “allegro0.TTF”;
$this->MarkSign = $MarkSign;
}

function waterMark($sourFile, $text=’Mark’) {
$imageInfo = $this->getInfo($sourFile);
$sourFile = $this->sourcePath . $sourFile;
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], “.”)) . “_mark.jpg”;
switch ($imageInfo["type"]) {
case 1: $img = imagecreatefromgif($sourFile); break;
case 2: $img = imagecreatefromjpeg($sourFile); break;
case 3: $img = imagecreatefrompng($sourFile); break;
default: return 0; break;
}
if (!$img) return 0;
$width = ($this->maxWidth > $imageInfo["width"]) ? $imageInfo["width"] : $this->maxWidth;
$height = ($this->maxHeight > $imageInfo["height"]) ? $imageInfo["height"] : $this->maxHeight;
$srcW = $imageInfo["width"];
$srcH = $imageInfo["height"];
if ($srcW * $width > $srcH * $height) $height = round($srcH * $width / $srcW);
else $width = round($srcW * $height / $srcH);
if (function_exists(“imagecreatetruecolor”)) {
$new = imagecreatetruecolor($width, $height);
ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
} else {
$new = imagecreate($width, $height);
ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
}
$white = imageColorAllocate($new, 255, 255, 255);
$black = imageColorAllocate($new, 0, 0, 0);
$red = imageColorAllocate($new, 255, 0, 0);
$gray = imageColorAllocate($new, 161, 161, 161);

if($this->MarkSign<>”){
$MarkInfo = $this->getInfo($this->MarkSign);
$widthm = $MarkInfo["width"];
$heightm = $MarkInfo["height"];
switch ($MarkInfo["type"]) {
case 1: $mark = imagecreatefromgif($this->MarkSign); break;
case 2: $mark = imagecreatefromjpeg($this->MarkSign); break;
case 3: $mark = imagecreatefrompng($this->MarkSign); break;
default: return 0; break;
}
$w = $widthm;
$h = $heightm;
}else{
$fontsize = $width>300?20:10;
$temp = imagettfbbox($fontsize, 0, $this->fontName, $text);
$w = $temp[2] – $temp[6];
$h = $temp[3] – $temp[7];
unset($temp);
}

switch($this->WaterPos) {
case 0://随机
$posX = rand(0,($width – $w));
$posY = rand($h,($height – $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ($width – $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $width – $w;
$posY = $h;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($height – $h) / 2;
break;
case 5://5为中部居中
$posX = ($width – $w) / 2;
$posY = ($height – $h) / 2;
break;
case 6://6为中部居右
$posX = $width – $w;
$posY = ($height – $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $height – $h;
break;
case 8://8为底端居中
$posX = ($width – $w) / 2;
$posY = $height – $h;
break;
case 9://9为底端居右
$posX = $width – $w-5;
$posY = $height – $h-5;
break;
default://随机
$posX = rand(0, ($width – $w));
$posY = rand($h, ($height – $h));
break;
}

if($this->MarkSign<>”){
imagecopy($new, $mark, $posX, $posY, 0, 0, $widthm, $heightm);
imagedestroy($mark);
}else{
$alpha = imageColorAllocateAlpha($new, 200, 200, 200, 70);
ImageFilledRectangle($new, 0, $posY-$h, $width, $posY+5, $alpha);
// ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black);
ImageTTFText($new, $fontsize, 0, $posX, $posY, $black, $this->fontName, $text);
}
if ($this->toFile) {
if (file_exists($this->galleryPath . $newName)) unlink($this->galleryPath . $newName);
ImageJPEG($new, $this->galleryPath . $newName);
return $this->galleryPath . $newName;
} else {
ImageJPEG($new,$sourFile);
}
ImageDestroy($new);
ImageDestroy($img);
}

function getInfo($file) {
$file = $this->sourcePath . $file;
$data = getimagesize($file);
$imageInfo["width"] = $data[0];
$imageInfo["height"]= $data[1];
$imageInfo["type"] = $data[2];
$imageInfo["name"] = basename($file);
return $imageInfo;
}

}

$mark=new GDMark(”, ”, 9, ”, ‘./watermark.png’);

$mark->waterMark(“2.jpg”);

echo ““;

?>

Popularity: 6%

No Responses to “给图片加水印类”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>