PHP给图片生成缩略图和加版权的类,测试通过

发布时间:2007年11月23日      浏览次数:1813 次
GDImage.inc.php
========================================================
<?
// FileName:GDImage.inc.php
// Summary: 图片处理程序
// Author: ice_berg16(寻梦的稻草人)
// CreateTime: 2004-10-12
// LastModifed:2004-10-12
// copyright (c)2004 ice_berg16@163.com
//====================================================
class GDImage
{
var $sourcePath; //图片存储路径
var $galleryPath; //图片缩略图存储路径
var $toFile = false; //是否生成文件
var $fontName; //使用的TTF字体名称
var $maxWidth = 500; //图片最大宽度
var $maxHeight = 600; //图片最大高度
//==========================================
// 函数: GDImage($sourcePath ,$galleryPath, $fontPath)
// 功能: constructor
// 参数: $sourcePath 图片源路径(包括最后一个"/")
// 参数: $galleryPath 生成图片的路径
// 参数: $fontPath 字体路径
//==========================================
function GDImage($sourcePath, $galleryPath, $fontPath)
{
$this->sourcePath = $sourcePath;
$this->galleryPath = $galleryPath;
$this->fontName = $fontPath . "SIMLI.TTF";
}
//==========================================
// 函数: makeThumb($sourFile,$width=128,$height=128)
// 功能: 生成缩略图(输出到浏览器)
// 参数: $sourFile 图片源文件
// 参数: $width 生成缩略图的宽度
// 参数: $height 生成缩略图的高度
// 返回: 0 失败 成功时返回生成的图片路径
//==========================================
function makeThumb($sourFile,$width=128,$height=128)
{
$imageInfo = $this->getInfo($sourFile);
$sourFile = $this->sourcePath . $sourFile;
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.jpg";
switch ($imageInfo["type"])
{
case 1: //gif
$img = imagecreatefromgif($sourFile);
break;
case 2: //jpg
$img = imagecreatefromjpeg($sourFile);
break;
case 3: //png
$img = imagecreatefrompng($sourFile);
break;
default:
return 0;
break;
}
if (!$img)
return 0;
$width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
$height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
$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")) //GD2.0.1
{
$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"]);
}
//*/
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);
}
ImageDestroy($new);
ImageDestroy($img);
}
//==========================================
// 函数: waterMark($sourFile, $text)
// 功能: 给图片加水印
// 参数: $sourFile 图片文件名
// 参数: $text 文本数组(包含二个字符串)
// 返回: 1 成功 成功时返回生成的图片路径
//==========================================
function waterMark($sourFile, $text)
{
$imageInfo = $this->getInfo($sourFile);
$sourFile = $this->sourcePath . $sourFile;
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_mark.jpg";
switch ($imageInfo["type"])
{
case 1: //gif
$img = imagecreatefromgif($sourFile);
break;
case 2: //jpg
$img = imagecreatefromjpeg($sourFile);
break;
case 3: //png
$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")) //GD2.0.1
{
$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);
$alpha = imageColorAllocateAlpha($new, 230, 230, 230, 40);
//$rectW = max(strlen($text[0]),strlen($text[1]))*7;
ImageFilledRectangle($new, 0, $height-26, $width, $height, $alpha);
ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black);
ImageTTFText($new, 9, 0, 21, $height-7, $white, $this->fontName, $text[0]);
ImageTTFText($new, 9, 0, 20, $height-8, $black, $this->fontName, $text[0]);
//*/
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);
}
ImageDestroy($new);
ImageDestroy($img);
}
//==========================================
// 函数: displayThumb($file)
// 功能: 显示指定图片的缩略图
// 参数: $file 文件名
// 返回: 0 图片不存在
//==========================================
function displayThumb($file)
{
$thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg";
$file = $this->galleryPath . $thumbName;
if (!file_exists($file))
return 0;
$html = "<img style='BORDER-RIGHT: #000 1px solid; BORDER-TOP: #000 1px solid; BORDER-LEFT: #000 1px solid; BORDER-BOTTOM: #000 1px solid' alt='' src='$file' />";
echo $html;
}
//==========================================
// 函数: displayMark($file)
// 功能: 显示指定图片的水印图
// 参数: $file 文件名
// 返回: 0 图片不存在
//========================================== www.bitsCN.net网管博客等你来搏
function displayMark($file)
{
$markName = substr($file, 0, strrpos($file, ".")) . "_mark.jpg";
$file = $this->galleryPath . $markName;
if (!file_exists($file))
return 0;
$html = "<img style='BORDER-RIGHT: #000 1px solid; BORDER-TOP: #000 1px solid; BORDER-LEFT: #000 1px solid; BORDER-BOTTOM: #000 1px solid' alt='' src='$file' />";
echo $html;
}
//==========================================
// 函数: getInfo($file)
// 功能: 返回图像信息
// 参数: $file 文件路径
// 返回: 图片信息数组
//==========================================
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;
}
}
?>
----------------------------------
下面是使用方法
这个类使用了一个04B_08__.TTF字体
使用类的时候指定该字体路径即可
-----------------------------------
<?
require_once("GDImage.inc.php");
//header("Content-type: image/jpeg");//输出到浏览器的话别忘了打开这个
$img = new GDImage("./", "./1/", "./"); //这三个参数分别为:源图片路径,目标图片路径,水印字体路径
$text = array("WEB SITE"); //设置水印文字
$img->maxWidth = $img->maxHeight = 500;//指定图片被生成的最大宽与高度
$img->toFile = true; //设定图片是否被显示
$img_name="1.jpg";//原图片名称
$img->makeThumb($img_name); //生成缩略图
$img->waterMark($img_name, $text); //给图片加水印
$img->displayThumb($img_name); //显示缩略图
$img->displayMark($img_name);//显示加水印后的原图
?>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!