【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-gdimage.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?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 = true; //是否生成文件
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, $ima