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

WBOY
Release: 2016-06-07 17:19:22
Original
827 people have browsed it

复制代码 代码如下:

    /*
    *@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                if($dstimgW && $dstimgW                    $dsimgWratio = $dstimgW / $pic_w;
                    $resizereagW =true;

                }
                if($dstimgH && $ $dstimgH                     $dsimgHratio = $dstimgH/$pic_H;
                    $resizerreagH =true;
                }
                //缩略图宽高和原图宽高比,取最小的那个
                if($resizereagW && $resizerreagH){
                    if($dsimgWratio                        $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();
    }
}
?>

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!