欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP图片等比例缩放生成缩略图函数分享

程序员文章站 2023-12-12 20:15:22
复制代码 代码如下:

复制代码 代码如下:

<?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();
    }
}
?>

上一篇:

下一篇: