分享
 
 
 

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

王朝php·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

最近几天看了一下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");//输出到浏览器的话别忘了打开这个

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有