php图片等比例缩放生成缩略图函数分享
2014-10-08来源:易贤网

代码如下:

<?php

/*

*@im //需要缩放的图片资源

*@filetype //制作的缩略图文件类型

*@dstimw //缩放的图片的宽度

*@dstimh //缩放的图片的高度

*@thumbname //缩略图文件名字

function makethumb($im,$dstimw,$dstimh,$thumbname ,$filetype){

//获取im的宽度和高度

$pic_w=imagesx($im);

$pic_h=imagesy($im);

$arr = array();

swith($filetype){

case 'jpg':

$arr[$filetype]=imagejpeg;

break;

case 'png';

$arr[$filetype]=imagepng;

break;

case 'jif';

$arr[$filetype]=imagegif;

}

if(($dstimgw && $dstimgw<$pic_w) || ($dstimgh && $dstimgh<$pic_h) ){

if($dstimgw && $dstimgw<$pic_w){

$dsimgwratio = $dstimgw / $pic_w;

$resizereagw =true;

}

if($dstimgh && $ $dstimgh <$pic_h){

$dsimghratio = $dstimgh/$pic_h;

$resizerreagh =true;

}

//缩略图宽高和原图宽高比,取最小的那个

if($resizereagw && $resizerreagh){

if($dsimgwratio<$dsimghratio)

$radio = $dsimgwratio;

else

$radio = $dsimghratio;

}

if($resizereagw && !$resizerreagh ){

$radio = $dsimgwratio;

}

if(!$resizereagw && $resizerreagh){

$radio = $dsimghratio ;

}

$imgneww = $pic_w * $radio;

$imgnewh = $pic_h * $radio;

if(function_exists(imgcopyresampled)){

//创建目标资源画布

$dst = imagecreatetruecolor ($imgneww, $imgnewh);

imagecopyresampled ($dst,$im,0,0,0,0,$imgneww,$imgnewh,$pic_w,$pic_h);

}else{

$dst=imagecreate($imgneww, $imgnewh);

imagecopyresized ($dst, $im,0,0,0,0,$imgneww,$imgnewh,$imgnewh,$pic_w,$pic_h);

}

$arr[$filetype]($dst,$thumbname..$filetype);

imagedestroy ($dst);

}else{//缩略图自身的宽和高已经大于了原图的宽和高

//则缩略图的宽和缩略的高就是原图的宽和原图的高

$arr[$filetype]($im,$thumbname..$filetype);

imagedestroy();

}

}

?>

更多信息请查看IT技术专栏

2026公务员·事业单位培训课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
推荐信息