一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php大图生成小图代码(缩略图程序)

时间:2022-06-24 21:12:04 编辑:袖梨 来源:一聚教程网

代码如下 复制代码

function bigtosmallimg($file,$path,$w=120,$h=90)
{
$img=$path.$file;
$imgarr=getimagesize($img);
$sw=$imgarr[0];//原图宽
$sh=$imgarr[1];//原图高
$stype=$imgarr[2];
//按比例缩放
if($sw/$sh>$w/$h){
$mw=$w;
$mh=(int)$sh*($w/$sw);
}
else{
$mw=(int)$sw*($h/$sh);
$mh=$h;
}

switch($stype){//根据上传好的图形文件类型新建一个用来生成缩略图的源文件。
case 1:
$srcf = imagecreatefromgif($img);
break;
case 2:
$srcf = imagecreatefromjpeg($img);
break;
case 3:
$srcf = imagecreatefrompng($img);
break;
default:
showmsg('程序调用错误。');
break;
}

$desf =imagecreatetruecolor($mw,$mh);

imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh);
$sm_name=$path."s_".$file;
switch($stype){
case 1:
imagegif($desf,$sm_name);
break;
case 2:
imagejpeg($desf,$sm_name);
break;
case 3:
imagepng($desf,$sm_name);
break;
default:
showmsg('无法生成www.111com.net' . $stype . '的缩略图。');
break;
}
imagedestroy($desf);
imagedestroy($srcf);

}

//此缩略图调用方法

代码如下 复制代码

bigtosmallimg($file,$path,$w=120,$h=90);
/*

$file = 图片的路径
$path = 生成后保存的路径
$w =图片宽度
$h =图片高度
*/

热门栏目