Juszeil Conception

Juszeil Conception

  • 主 頁
  • 部 落 格
  • 相 簿
  • 關 於
  • 更 新 資 訊
  • 網站地圖

«2017 - 3»
日一二三四五六
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
TODAY

Hide Banner | LOGIN
2023-3-21 |

BLOG 部落格

Total found 1 articles on 2017-3-25.

蝔 撘 隤 閮 2017-3-25 18:54:23

PHP - Resize and crop image from center


//resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];
 
    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;
 
        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            break;
 
        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 80;
            break;
 
        default:
            return false;
            break;
    }
    
    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);
    
    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }
    
    $image($dst_img, $dst_dir, $quality);
 
    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");
Relate Post : PHP - 憒雿霈 PHP Y(array) 頧 JavaScript 臭誑雿輻函拐辣(Object)澆 ( json_encode() 頧 PHP - 瑼X Email IP 澆臬血瘜 PHP - APC module Web Cache PHP湔詨賣貊蝔格寞 PHP - 蝬脣貊瘜 (雿輻 http_build_query) Linux / UNIX Setup and Run PHP Script As A Cron Job PHP - 憒雿Y頝典僑摨衣隞質” PHP - run on background PHP - 憒雿曉箏拙交撌桐憭撠"隞" MyISAM InnoDB 雓閫
Comments :
No Comments

Post your comment:


Post your comment by Guest :
Verify Code :


Back To Top

Find Me

Powered By 2013-2015 ©. Juszeil Conception version 2.0
Queries Executed : 0.0107 seconds