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仿ZOL分页类代码
Oct 02 PHP
在IIS7.0下面配置PHP 5.3.2运行环境的方法
Apr 13 PHP
PHP 函数执行效率的小比较
Oct 17 PHP
PHP安全性漫谈
Jun 28 PHP
ThinkPHP连接数据库及主从数据库的设置教程
Aug 22 PHP
php语言中使用json的技巧及json的实现代码详解
Oct 27 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
Apr 27 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
PHP根据key删除数组中指定的元素
Feb 28 PHP
Yii框架参数配置文件params用法实例分析
Sep 11 PHP
laravel 获取当前url的别名方法
Oct 11 PHP
基于laravel belongsTo使用详解
Oct 18 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
php把大写命名转换成下划线分割命名
2015/04/27 PHP
使用PHPStorm+XDebug搭建单步调试环境
2017/11/19 PHP
php5.x禁用eval的操作方法
2018/10/19 PHP
php常用经典函数集锦【数组、字符串、栈、队列、排序等】
2019/08/23 PHP
PHP实现15位身份证号转18位的方法分析
2019/10/16 PHP
用javascript实现给图片加链接
2007/08/15 Javascript
jQuery的实现原理的模拟代码 -1 核心部分
2010/08/01 Javascript
jquery 学习之一 对象访问
2010/11/23 Javascript
jquerymobile局部渲染的各种刷新方法小结
2014/03/05 Javascript
AngularJS语法详解(续)
2015/01/23 Javascript
jQuery DOM删除节点操作指南
2015/03/03 Javascript
JS+CSS实现的竖向简洁折叠菜单效果代码
2015/10/22 Javascript
JS实时弹出新消息提示框并有提示音响起的实现代码
2016/04/20 Javascript
利用JS判断鼠标移入元素的方向
2016/12/11 Javascript
Vue数据驱动模拟实现4
2017/01/12 Javascript
BootStrapValidator初使用教程详解
2017/02/10 Javascript
javascript 中的继承实例详解
2017/05/05 Javascript
Angular ng-animate和ng-cookies用法详解
2018/04/18 Javascript
微信小程序首页的分类功能和搜索功能的实现思路及代码详解
2018/09/11 Javascript
微信小程序 bindtap 传参的实例代码
2020/02/21 Javascript
原生JS实现留言板
2020/03/26 Javascript
vue.js页面加载执行created,mounted的先后顺序说明
2020/11/07 Javascript
[01:24]2014DOTA2 TI第二日 YYF表示这届谁赢都有可能
2014/07/11 DOTA
python在指定目录下查找gif文件的方法
2015/05/04 Python
Python3实现简单可学习的手写体识别(实例讲解)
2017/10/21 Python
python中提高pip install速度
2020/02/14 Python
Python自动化之UnitTest框架实战记录
2020/09/08 Python
python+opencv3.4.0 实现HOG+SVM行人检测的示例代码
2021/01/28 Python
YSL圣罗兰美妆美国官网:Yves Saint Lauret US
2016/11/21 全球购物
澳大利亚电子产品购物网站:Dick Smith
2017/02/02 全球购物
英国电气世界:Electrical World
2019/09/08 全球购物
文化宣传方案
2014/03/13 职场文书
2015年推普周活动方案
2015/05/06 职场文书
推广普通话的宣传语
2015/07/13 职场文书
美德少年事迹材料(2016推荐版)
2016/02/25 职场文书
Nginx安装完成没有生成sbin目录的解决方法
2021/03/31 Servers