(❀╹◡╹)
[PHP] 이미지 resize, crop 함수 본문
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로 줄 경우 원본 비율에 맞춰서 줄여줌
'Programing > WEB' 카테고리의 다른 글
[PHP] 현재 접속 URL구하기 (0) | 2021.02.03 |
---|---|
[PHP] CKEditor 연동, 이미지 업로드 (1) | 2020.11.20 |
[PHP] 전화번호 하이픈(-) 추가, 정규식 (0) | 2020.11.06 |
[jQuery] Slick Slider fade 처리 (0) | 2020.09.16 |
[jQuery] Slick Slider - 로드 시 display:none 일때 refresh 대체 (0) | 2020.09.16 |