8
16
2008
1

[PHP]图片缩放转换方法

 

function coverImg($source,$target,$targetWidth,$targetHeight){
        $system=explode(".",$source);
       
        if (preg_match("/jpg|jpeg/",$system[count($system)-1])){
                $src_img=imagecreatefromjpeg($source);
        }
        if (preg_match("/png/",$system[count($system)-1])){
                $src_img=imagecreatefrompng($source);
        }
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);
        if ($old_x > $old_y) {  //图片宽度大于高度
                $thumb_h = $old_y*($targetWidth/$old_x);
                $thumb_w = $targetWidth;
                //$thumb_h=$targetHeight;
                //$thumb_w=$old_x*($targetHeight/$old_y);
        }
        if ($old_x < $old_y) { //图片高度大于宽度
                $thumb_w = $old_x*($targetHeight/$old_y);
                $thumb_h = $targetHeight;
               
                //$thumb_h=$old_y*($targetWidth/$old_x);
                //$thumb_w=$targetWidth;
        }
        if ($old_x == $old_y) {
                $thumb_w=$targetWidth;
                $thumb_h=$targetHeight;
        }
       
        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
       
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
       
        imagejpeg($dst_img,$target);
        imagedestroy($dst_img);
        imagedestroy($src_img);
        return true;
}

写的比较简单,可以将上传的图片转换为jpg格式,并且可以控制最大宽度和最大高度

Category: PHP | Tags: | Read Count: 2150
Avatar_small
FrancisAnzalone 说:
2018年10月25日 22:26

I have learnt more about coding here. In https://www.topdissertations.org/wordsdoctorate-review/, I have downloaded many materials on programming. I am really glad to get this site.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com