最近几天看了一下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 [email protected]
//====================================================
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 = "";%20
echo%20$html;%20
}%20
//==========================================%20
//%20函数:%20displaymark($file)%20
//%20功能:%20显示指定图片的水印图%20
//%20参数:%20$file%20文件名%20
//%20返回:%200%20图片不存在%20
//==========================================%20
function%20displaymark($file)%20
{%20
$markname%20=%20substr($file,%200,%20strrpos($file,%20"."))%20.%20"_mark.jpg";
$file = $this->gallerypath . $markname;
if (!file_exists($file))
return 0;
$html = "";%20
echo%20$html;%20
}%20
//==========================================%20
//%20函数:%20getinfo($file)%20
//%20功能:%20返回图像信息%20
//%20参数:%20$file%20文件路径%20
//%20返回:%20图片信息数组%20
//==========================================%20
function%20getinfo($file)%20
{%20
$file%20=%20$this->sourcepath%20.%20$file;%20
$data%20=%20getimagesize($file);%20
$imageinfo["width"]%20=%20$data[0];%20
$imageinfo["height"]=%20$data[1];%20
$imageinfo["type"]%20=%20$data[2];%20
$imageinfo["name"]%20=%20basename($file);%20
return%20$imageinfo;%20
}%20
}%20
?>%20
----------------------------------%20
下面是使用方法%20
这个类使用了一个04b_08__.ttf字体%20
使用类的时候指定该字体路径即可%20
-----------------------------------%20
require_once("gdimage.inc.php");%20
//header("content-type:%20image/jpeg");//输出到浏览器的话别忘了打开这个%20
$img%20=%20new%20gdimage(ib_upload_path,%20ib_gallery,%20ib_temp);%20
$text%20=%20array("ice-berg.org","all%20rights%20reserved");%20
$img->maxwidth%20=%20$img->maxheight%20=%20300;%20
$img->tofile%20=%20true;%20
$img->watermark("mm.jpg", $text);
$img->makethumb("mm.jpg");
$img->displaythumb("mm.jpg");
$img->displaymark("mm.jpg");
新闻热点
疑难解答