兼容性比较好的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);
}

相关文章

php文件操作之小型留言本实例

本文实例讲述了php文件操作之小型留言本。分享给大家供大家参考。具体如下: Index.php文件如下: <?php $path = "DB/"; //定义路径 $...

PHP 可阅读随机字符串代码

复制代码 代码如下: /************** *@length - length of random string (must be a multiple of 2) *****...

php删除文件夹及其文件夹下所有文件的函数代码

复制代码 代码如下: <? function deldir($dir) { //先删除目录下的文件: $dh=opendir($dir); while ($file=readdir...

php中获取远程客户端的真实ip地址的方法

(1).REMOTE_ADDR:浏览当前页面的用户计算机的ip地址 (2).HTTP_X_FORWARDED_FOR: 浏览当前页面的用户计算机的网关 (3).HTTP_CLIENT_I...

php自动获取字符串编码函数mb_detect_encoding

当在php中使用mb_detect_encoding函数进行编码识别时,很多人都碰到过识别编码有误的问题,例如对与GB2312和UTF- 8,或者UTF-8和GBK(这里主要是对于cp9...