您好,欢迎访问一九零五行业门户网

PHP给图片生成缩略图和加版权的类

最近几天看了一下php的图片处理方面的功能,以前这方面的需求比较少,也就没怎么看,最近有空看了一下。感觉图片处理一些简单的功能还可以,复杂的就算了,gd库都2.0.1了,还是不支持中文,看了几篇文章,想使用中文只能先将gb2312转换成unicode再写入图片,太麻烦了,索性只使用英文算了。
在图像生成部分可以定义图片的最大高,宽,比较适用于新闻及相册等系统。
gd2.0.1在图片处理上有很大提高,我试了下imagecopyresized和imagecopyresampled,后者处理的图片明显好于前者,据手册上讲后者对改变大小后的图片重新采样基本保持不失真,生成缩略图的效果还真不错。
-------------------------------------------------------------
下面是类
-----------------------
//====================================================
// 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 . 04b_08__.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, 4.9, 0, 20, $height-14, $black, $this->fontname, $text[0]);
imagettftext($new, 4.9, 0, 20, $height-6, $black, $this->fontname, $text[1]);
//*/
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 = ;
echo $html;
}
//==========================================
// 函数: displaymark($file)
// 功能: 显示指定图片的水印图
// 参数: $file 文件名
// 返回: 0 图片不存在
//==========================================
function displaymark($file)
{
$markname = substr($file, 0, strrpos($file, .)) . _mark.jpg;
$file = $this->gallerypath . $markname;
if (!file_exists($file))
return 0;
$html = ;
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(ib_upload_path, ib_gallery, ib_temp);
$text = array(ice-berg.org,all rights reserved);
$img->maxwidth = $img->maxheight = 300;
$img->tofile = true;
$img->watermark(mm.jpg, $text);
$img->makethumb(mm.jpg);
$img->displaythumb(mm.jpg);
$img->displaymark(mm.jpg);
其它类似信息

推荐信息