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 相关文章推荐
用PHP实现小型站点广告管理(修正版)
Oct 09 PHP
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
Dec 16 PHP
PHP中的session永不过期的解决思路及实现方法分享
Apr 20 PHP
php正则表达匹配中文问题分析小结
Mar 25 PHP
destoon调用discuz论坛中带图片帖子的实现方法
Aug 21 PHP
PHP动态柱状图实现方法
Mar 30 PHP
PHP数学运算与数据处理实例分析
Apr 01 PHP
php+jQuery+Ajax简单实现页面异步刷新
Aug 08 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
Jun 16 PHP
Yii2框架实现登陆添加验证码功能示例
Jul 12 PHP
php高性能日志系统 seaslog 的安装与使用方法分析
Feb 29 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 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
如何开始收听短波广播
2021/03/01 无线电
PHP求小于1000的所有水仙花数的代码
2012/01/10 PHP
yii框架配置默认controller和action示例
2014/04/30 PHP
关于递归运算的顺序测试代码
2011/11/30 Javascript
js判断屏幕分辨率的代码
2013/07/16 Javascript
javascript中不提供sleep功能如何实现这个功能
2014/05/27 Javascript
ECMAScript5(ES5)中bind方法使用小结
2015/05/07 Javascript
深入探究AngularJS框架中Scope对象的超级教程
2016/01/04 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
详解用webpack把我们的业务模块分开打包的方法
2017/07/20 Javascript
如何制作一个Node命令行图像识别工具
2018/12/12 Javascript
Vue动态路由缓存不相互影响的解决办法
2019/02/19 Javascript
Layui 动态禁止select下拉的例子
2019/09/03 Javascript
微信小程序防止多次点击跳转(函数节流)
2019/09/19 Javascript
python实现根据主机名字获得所有ip地址的方法
2015/06/28 Python
Python实现约瑟夫环问题的方法
2016/05/03 Python
利用Django-environ如何区分不同环境
2018/08/26 Python
如何用C代码给Python写扩展库(Cython)
2019/05/17 Python
对python 调用类属性的方法详解
2019/07/02 Python
Python统计时间内的并发数代码实例
2019/12/28 Python
浅谈python之自动化运维(Paramiko)
2020/01/31 Python
python生成任意频率正弦波方式
2020/02/25 Python
matplotlib之pyplot模块之标题(title()和suptitle())
2021/02/22 Python
阿迪达斯比利时官方商城:adidas比利时
2016/10/10 全球购物
倩碧英国官网:Clinique英国
2018/08/10 全球购物
北京某公司的.net笔试题
2014/03/20 面试题
工厂门卫岗位职责
2013/11/25 职场文书
青年教师典范事迹材料
2014/01/31 职场文书
积极向上的团队口号
2014/06/06 职场文书
反四风个人对照检查材料思想汇报
2014/09/25 职场文书
乔布斯辞职信(中英文对照)
2015/05/12 职场文书
企业安全隐患排查治理制度
2015/08/05 职场文书
再读《皇帝的新衣》的读后感悟!
2019/08/07 职场文书
python实现黄金分割法的示例代码
2021/04/28 Python
Mysql中where与on的区别及何时使用详析
2021/08/04 MySQL
Windows server 2003卸载和安装IIS的图文教程
2022/07/15 Servers