php生成缩略图的类代码


Posted in PHP onOctober 02, 2008

<?php

/**
* 功能:生成缩略图
* 作者:phpox
* 日期:Thu May 17 09:57:05 CST 2007
*/

class CreatMiniature
{
//公共变量
var $srcFile=""; //原图
var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件
var $im=""; //临时变量
var $srcW=""; //原图宽
var $srcH=""; //原图高

//设置变量及初始化
function SetVar($srcFile,$echoType)
{
if (!file_exists($srcFile)){
echo '源图片文件不存在!';
exit();
}
$this->srcFile=$srcFile;
$this->echoType=$echoType;

$info = "";
$data = GetImageSize($this->srcFile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = ImageCreateFromGIF($this->srcFile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = ImageCreateFromJpeg($this->srcFile);
break;
case 3:
$this->im = ImageCreateFromPNG($this->srcFile);
break;
}
$this->srcW=ImageSX($this->im);
$this->srcH=ImageSY($this->im);
}

//生成扭曲型缩图
function Distortion($toFile,$toW,$toH)
{
$cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}

//生成按比例缩放的缩图
function Prorate($toFile,$toW,$toH)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ftoW=$toW;
$ftoH=$ftoW*($this->srcH/$this->srcW);
}
else
{
$ftoH=$toH;
$ftoW=$ftoH*($this->srcW/$this->srcH);
}
if($this->srcW>$toW||$this->srcH>$toH)
{
$cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
else
{
$cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
}

//生成最小裁剪后的缩图
function Cut($toFile,$toW,$toH)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ctoH=$toH;
$ctoW=$ctoH*($this->srcW/$this->srcH);
}
else
{
$ctoW=$toW;
$ctoH=$ctoW*($this->srcH/$this->srcW);
}
$allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
$cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
ImageDestroy($allImg);
}

//生成背景填充的缩图
function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ftoW=$toW;
$ftoH=$ftoW*($this->srcH/$this->srcW);
}
else
{
$ftoH=$toH;
$ftoW=$ftoH*($this->srcW/$this->srcH);
}
if(function_exists("imagecreatetruecolor"))
{
@$cImg=ImageCreateTrueColor($toW,$toH);
if(!$cImg)
{
$cImg=ImageCreate($toW,$toH);
}
}
else
{
$cImg=ImageCreate($toW,$toH);
}
$backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色
ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
if($this->srcW>$toW||$this->srcH>$toH)
{
$proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
if($ftoW<$toW)
{
ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
}
else if($ftoH<$toH)
{
ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
}
else
{
ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
}
}
else
{
ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
}
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}

function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
{
if(function_exists("imagecreatetruecolor"))
{
@$creatImg = ImageCreateTrueColor($creatW,$creatH);
if($creatImg)
ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
else
{
$creatImg=ImageCreate($creatW,$creatH);
ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
}
else
{
$creatImg=ImageCreate($creatW,$creatH);
ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
return $creatImg;
}

//输出图片,link---只输出,不保存文件。file--保存为文件
function EchoImage($img,$to_File)
{
switch($this->echoType)
{
case "link":
if(function_exists('imagejpeg')) return ImageJpeg($img);
else return ImagePNG($img);
break;
case "file":
if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
else return ImagePNG($img,$to_File);
break;
}
}
}
?>

PHP 相关文章推荐
mysql 搜索之简单应用
Apr 27 PHP
解决File size limit exceeded 错误的方法
Jun 14 PHP
一个好用的PHP验证码类实例分享
Dec 27 PHP
Codeigniter生成Excel文档的简单方法
Jun 12 PHP
PHP中使用CURL模拟登录并获取数据实例
Jul 01 PHP
thinkphp文件引用与分支结构用法实例
Nov 26 PHP
php获取指定范围内最接近数的方法
Jun 02 PHP
PHP安装GeoIP扩展根据IP获取地理位置及计算距离的方法
Jul 01 PHP
yii的入口文件index.php中为什么会有这两句
Aug 04 PHP
PHP类和对象相关系统函数与运算符小结
Sep 28 PHP
php mysql PDO 查询操作的实例详解
Sep 23 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
Nov 17 PHP
PHP实时显示输出
Oct 02 #PHP
PHP在字符串中查找指定字符串并删除的代码
Oct 02 #PHP
php之对抗Web扫描器的脚本技巧
Oct 01 #PHP
利用PHP制作简单的内容采集器的原理分析
Oct 01 #PHP
php数组总结篇(一)
Sep 30 #PHP
PHP EOT定界符的使用详解
Sep 30 #PHP
40个迹象表明你还是PHP菜鸟
Sep 29 #PHP
You might like
2019年中国咖啡业现状与发展趋势
2021/03/04 咖啡文化
php5 and xml示例
2006/11/22 PHP
ThinkPHP之A方法实例讲解
2014/06/20 PHP
php生成图片验证码的方法
2016/04/15 PHP
PHP通过curl获取接口URL的数据方法
2018/05/31 PHP
PHP ob缓存以及ob函数原理实例解析
2020/11/13 PHP
关于取不到由location.href提交而来的上级页面地址的解决办法
2009/07/30 Javascript
lyhucSelect基于Jquery的Select数据联动插件
2011/03/29 Javascript
javascript 基础篇2 数据类型,语句,函数
2012/03/14 Javascript
javascript跑马灯悬停放大效果实现代码
2012/12/12 Javascript
javascript中clone对象详解
2014/12/03 Javascript
JavaScript预解析及相关技巧分析
2016/04/21 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
详谈DOM简介及节点、属性、查找节点的方法
2017/11/16 Javascript
nodejs检测因特网是否断开的解决方案
2019/04/17 NodeJs
vue中使用elementUI组件手动上传图片功能
2019/12/13 Javascript
python基础教程之简单入门说明(变量和控制语言使用方法)
2014/03/25 Python
Python3访问并下载网页内容的方法
2015/07/28 Python
Django Admin 实现外键过滤的方法
2017/09/29 Python
TensorFlow如何实现反向传播
2018/02/06 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
2018/10/16 Python
python 含子图的gif生成时内存溢出的方法
2019/07/07 Python
Python学习笔记之函数的定义和作用域实例详解
2019/08/13 Python
Python 中@property的用法详解
2020/01/15 Python
使用Python脚本从文件读取数据代码实例
2020/01/19 Python
Python实现AI自动抠图实例解析
2020/03/05 Python
Python pickle模块常用方法代码实例
2020/10/10 Python
台湾森森购物网:U-mall
2017/10/16 全球购物
Converse匡威法国官网:美国著名帆布鞋品牌
2018/12/05 全球购物
送餐员岗位职责范本
2014/02/21 职场文书
演讲稿格式
2014/04/30 职场文书
欢迎横幅标语
2014/06/17 职场文书
超市店庆活动方案
2014/08/31 职场文书
2015年毕业生自荐信范文
2015/03/24 职场文书
Python爬取某拍短视频
2021/06/11 Python
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android