Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

(❀╹◡╹)

[PHP] 이미지 resize, crop 함수 본문

Programing/WEB

[PHP] 이미지 resize, crop 함수

진 주 2020. 11. 10. 16:47
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $crop = true, $watermark = false, $quality = 70) {
    $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;
    }
    
    $src_img = $image_create($source_file);

    $is_lotate = false;
    if ($mime == 'image/jpeg') {
        // 자동으로 이미지가 회전되는 현상 해결
        $exif = exif_read_data($source_file);
        if(!empty($exif['Orientation'])) {
            switch($exif['Orientation']) {
                case 8:
                    $src_img = imagerotate($src_img, 90, 0);
                    $is_lotate = true;
                    break;
                case 3:
                    $src_img = imagerotate($src_img, 180, 0);
                    break;
                case 6:
                    $src_img = imagerotate($src_img, -90, 0);
                    $is_lotate = true;
                    break;
            }
        }
    }

    if (!$crop) {
        // 회전됨에 따라 가로, 세로 뒤바꾸기
        if ($is_lotate) {
            $tmp_max_width = $max_width;
            $max_width = $max_height;
            $max_height = $tmp_max_width;
            $tmp_width = $width;
            $width = $height;
            $height = $tmp_width;
        }

        if ($width >= $height) {
            // 가로가 클 경우 - 가로 제한을 기준으로 리사이즈 (세로:비율에 따른 축소, 가로:max-width) -- (original height / original width) x new width = new height
            $max_height = ceil(($height / $width) * $max_width);
            $max_width = $max_width;
        } else {
            // 세로가 클 경우 - 세로 제한을 기준으로 리사이즈 (세로:max-height, 가로:비율에 따른 축소) -- (original width / original height) x new height = new width
            $max_width = ceil(($width / $height) * $max_height);
            $max_height = $max_height;
        }
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);

    // png는 배경 불투명하게 
    if ( $mime == 'image/png' ) {
        imagealphablending($dst_img, false);
        imagesavealpha($dst_img, true);
        $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
        imagefilledrectangle($dst_img, 0, 0, $width, $height, $transparent);
    }

    if ($crop) {
        $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);
        }
    } else {
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $max_width, $max_height, $width, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if( $watermark == true ) {
        $stamp = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . '/images/watermark.png');
        $im = imagecreatefromjpeg($dst_dir);

        $sx = imagesx($stamp);
        $sy = imagesy($stamp);

        $x_dst = ( $max_width - $sx ) / 2;
        $y_dst = ( $max_height - $sy ) / 2;

        imagecopy($im, $stamp, $x_dst, $y_dst, 0, 0, imagesx($stamp), imagesy($stamp));
        imagejpeg($im, $dst_dir, 100);
        if($im)imagedestroy($im);
    }

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}

 

$crop을 false로 줄 경우 원본 비율에 맞춰서 줄여줌