兼容性最强的PHP生成缩略图的函数代码(修改版)

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

function ImageResize($srcFile,$toW,$toH,$toFile="")
{
if($toFile==""){ $toFile = $srcFile; }
$info = "";
$data = GetImageSize($srcFile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$im = ImageCreateFromGIF($srcFile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$im = ImageCreateFromJpeg($srcFile);
break;
case 3:
$im = ImageCreateFromPNG($srcFile);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$toWH=$toW/$toH;
$srcWH=$srcW/$srcH;
if($toWH<=$srcWH){
$ftoW=$toW;
$ftoH=$ftoW*($srcH/$srcW);
}
else{
$ftoH=$toH;
$ftoW=$ftoH*($srcW/$srcH);
}
if($srcW>$toW||$srcH>$toH)
{
if(function_exists("imagecreatetruecolor")){
@$ni = ImageCreateTrueColor($ftoW,$ftoH);
if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
else{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
}else{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
if(function_exists('imagejpeg')) ImageJpeg($ni,$toFile);
else ImagePNG($ni,$toFile);
ImageDestroy($ni);
}
ImageDestroy($im);
}

相关文章

工厂模式在Zend Framework中应用介绍

工厂模式在Zend Framework中应用介绍

首先我们先引用些概念: 工厂模式:专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有其同的父类。工厂模式属于类的创建模式,通常根据自变量的不同返回不同类的实例。 工厂模式的实质...

PHP实现C#山寨ArrayList的方法

本文实例讲述了PHP实现C#山寨ArrayList的方法。分享给大家供大家参考。具体如下: class ArrayList { public $length; public $n...

php懒人函数 自动添加数据

复制代码 代码如下: /* *@自动添加数据函数 *@$table 表名 *@$arr 字段库 array("title",array("content",int)) *@ array(...

使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT

例如这样一个表,我想统计email和passwords都不相同的记录的条数 复制代码 代码如下: CREATE TABLE IF NOT EXISTS `test_users` ( `e...

PHP计划任务之关闭浏览器后仍然继续执行的函数

备忘一下这个函数: 函数名称:ignore_user_abort 本函数配置或取得使用端连接中断后,PHP 程序是否仍继续执行。默认值为中断连接后就停止执行。在 PHP 配置文件中 (p...