이미지크롭관련 질문드립니다.

이미지크롭관련 질문드립니다.

QA

이미지크롭관련 질문드립니다.

본문

안녕하세요 이미지 크롭관련 질문 드립니다.

원본사이즈 : 3024*4032

크롭된 사이즈 : 224 * 300

 

 

필요한 이미지 사이즈

300 * 224  >>> 이렇에 크롭되면 좋습니다.

가로 : 300   ---> 세로가 길어도 무조건 가로 기준으로 300사이즈로 크롭

 

조언 부탁드립니다.

 

/lib/thumbnail.lib.php


function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_height, $is_create, $is_crop=false, $crop_mode='center', $is_sharpen=false, $um_value='80/0.5/3')
{
global $g5;
 
if(!$thumb_width && !$thumb_height)
return;
 
$source_file = "$source_path/$filename";
 
if(!is_file($source_file)) // 원본 파일이 없다면
return;
 
$size = @getimagesize($source_file);
if($size[2] < 1 || $size[2] > 3) // gif, jpg, png 에 대해서만 적용
return;
 
if (!is_dir($target_path)) {
($target_path, G5_DIR_PERMISSION);
@chmod($target_path, G5_DIR_PERMISSION);
}
 
// 디렉토리가 존재하지 않거나 쓰기 권한이 없으면 썸네일 생성하지 않음
if(!(is_dir($target_path) && is_writable($target_path)))
return '';
 
// Animated GIF는 썸네일 생성하지 않음
if($size[2] == 1) {
if(is_animated_gif($source_file))
return basename($source_file);
}
 
$ext = array(1 => 'gif', 2 => 'jpg', 3 => 'png');
 
$thumb_filename = preg_replace("/\.[^\.]+$/i", "", $filename); // 확장자제거
$thumb_file = "$target_path/thumb-{$thumb_filename}_{$thumb_width}x{$thumb_height}.".$ext[$size[2]];
 
$thumb_time = @filemtime($thumb_file);
$source_time = @filemtime($source_file);
 
if (is_file($thumb_file)) {
if ($is_create == false && $source_time < $thumb_time) {
return basename($thumb_file);
}
}
 
// 원본파일의 GD 이미지 생성
$src = null;
$degree = 0;
 
if ($size[2] == 1) {
$src = @imagecreatefromgif($source_file);
$src_transparency = @imagecolortransparent($src);
} else if ($size[2] == 2) {
$src = @imagecreatefromjpeg($source_file);
 
if(function_exists('exif_read_data')) {
// exif 정보를 기준으로 회전각도 구함
$exif = @exif_read_data($source_file);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$degree = 90;
break;
case 3:
$degree = 180;
break;
case 6:
$degree = -90;
break;
}
 
// 회전각도 있으면 이미지 회전
if($degree) {
$src = @imagerotate($src, $degree, 0);
 
// 세로사진의 경우 가로, 세로 값 바꿈
if($degree == 90 || $degree == -90) {
$tmp = $size;
$size[0] = $tmp[1];
$size[1] = $tmp[0];
}
}
}
}
} else if ($size[2] == 3) {
$src = @imagecreatefrompng($source_file);
@imagealphablending($src, true);
} else {
return;
}
 
if(!$src)
return;
 
$is_large = true;
// width, height 설정
// if($thumb_width) {
// if(!$thumb_height) {
// $thumb_height = round(($thumb_width * $size[1]) / $size[0]);
// } else {
// if($crop_mode === 'center' && ($size[0] > $thumb_width || $size[1] > $thumb_height)){
// $is_large = true;
// // } else if($size[0] < $thumb_width || $size[1] < $thumb_height) {
// // $is_large = false;
// }
// }
// } else {
// if($thumb_height) {
// $thumb_width = round(($thumb_height * $size[0]) / $size[1]);
// }
// }
 
$max=$thumb_width; // 여기서부터 추가 시작
if ( $size[0] > $size[1] ) {
$thumb_width = $max ;
$thumb_height = ceil( $size[1] * ( $max / $size[0] ) );
}
else if ( $size[0] < $size[1] ) {
$thumb_height = $max ;
$thumb_width = ceil( $size[0] * ( $max / $size[1] ) );
}
else if ( $size[0] == $size[1] ) {
$thumb_width = ceil( $max );
$thumb_height = ceil( $max );
} // 여기까지 끝
 
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
$dst_w = $thumb_width;
$dst_h = $thumb_height;
$src_w = $size[0];
$src_h = $size[1];
 
$ratio = $dst_h / $dst_w;
 
if($is_large) {
// 크롭처리
if($is_crop) {
switch($crop_mode)
{
case 'center':
if($size[1] / $size[0] >= $ratio) {
$src_h = round($src_w * $ratio);
$src_y = round(($size[1] - $src_h) / 2);
} else {
$src_w = round($size[1] / $ratio);
$src_x = round(($size[0] - $src_w) / 2);
}
break;
default:
if($size[1] / $size[0] >= $ratio) {
$src_h = round($src_w * $ratio);
} else {
$src_w = round($size[1] / $ratio);
}
break;
}
 
$dst = imagecreatetruecolor($dst_w, $dst_h);
 
if($size[2] == 3) {
imagealphablending($dst, false);
imagesavealpha($dst, true);
} else if($size[2] == 1) {
$palletsize = imagecolorstotal($src);
if($src_transparency >= 0 && $src_transparency < $palletsize) {
$transparent_color = imagecolorsforindex($src, $src_transparency);
$current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($dst, 0, 0, $current_transparent);
imagecolortransparent($dst, $current_transparent);
}
}
} else { // 비율에 맞게 생성
$dst = imagecreatetruecolor($dst_w, $dst_h);
$bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색
 
if ( !((defined('G5_USE_THUMB_RATIO') && false === G5_USE_THUMB_RATIO) || (defined('G5_THEME_USE_THUMB_RATIO') && false === G5_THEME_USE_THUMB_RATIO)) ){
if($src_w > $src_h) {
$tmp_h = round(($dst_w * $src_h) / $src_w);
$dst_y = round(($dst_h - $tmp_h) / 2);
$dst_h = $tmp_h;
} else {
$tmp_w = round(($dst_h * $src_w) / $src_h);
$dst_x = round(($dst_w - $tmp_w) / 2);
$dst_w = $tmp_w;
}
}
 
if($size[2] == 3) {
$bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagefill($dst, 0, 0, $bgcolor);
imagealphablending($dst, false);
imagesavealpha($dst, true);
} else if($size[2] == 1) {
$palletsize = imagecolorstotal($src);
if($src_transparency >= 0 && $src_transparency < $palletsize) {
$transparent_color = imagecolorsforindex($src, $src_transparency);
$current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($dst, 0, 0, $current_transparent);
imagecolortransparent($dst, $current_transparent);
} else {
imagefill($dst, 0, 0, $bgcolor);
}
} else {
imagefill($dst, 0, 0, $bgcolor);
}
}
} else {
$dst = imagecreatetruecolor($dst_w, $dst_h);
$bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색
 
if ( ((defined('G5_USE_THUMB_RATIO') && false === G5_USE_THUMB_RATIO) || (defined('G5_THEME_USE_THUMB_RATIO') && false === G5_THEME_USE_THUMB_RATIO)) ){
//이미지 썸네일을 비율 유지하지 않습니다. (5.2.6 버전 이하에서 처리된 부분과 같음)
 
if($src_w < $dst_w) {
if($src_h >= $dst_h) {
$dst_x = round(($dst_w - $src_w) / 2);
$src_h = $dst_h;
if( $dst_w > $src_w ){
$dst_w = $src_w;
}
} else {
$dst_x = round(($dst_w - $src_w) / 2);
$dst_y = round(($dst_h - $src_h) / 2);
$dst_w = $src_w;
$dst_h = $src_h;
}
} else {
if($src_h < $dst_h) {
$dst_y = round(($dst_h - $src_h) / 2);
$dst_h = $src_h;
$src_w = $dst_w;
}
}
 
} else {
//이미지 썸네일을 비율 유지하며 썸네일 생성합니다.
if($src_w < $dst_w) {
if($src_h >= $dst_h) {
if( $src_h > $src_w ){
$tmp_w = round(($dst_h * $src_w) / $src_h);
$dst_x = round(($dst_w - $tmp_w) / 2);
$dst_w = $tmp_w;
} else {
$dst_x = round(($dst_w - $src_w) / 2);
$src_h = $dst_h;
if( $dst_w > $src_w ){
$dst_w = $src_w;
}
}
} else {
$dst_x = round(($dst_w - $src_w) / 2);
$dst_y = round(($dst_h - $src_h) / 2);
$dst_w = $src_w;
$dst_h = $src_h;
}
} else {
if($src_h < $dst_h) {
if( $src_w > $dst_w ){
$tmp_h = round(($dst_w * $src_h) / $src_w);
$dst_y = round(($dst_h - $tmp_h) / 2);
$dst_h = $tmp_h;
} else {
$dst_y = round(($dst_h - $src_h) / 2);
$dst_h = $src_h;
$src_w = $dst_w;
}
}
}
}
 
if($size[2] == 3) {
$bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagefill($dst, 0, 0, $bgcolor);
imagealphablending($dst, false);
imagesavealpha($dst, true);
} else if($size[2] == 1) {
$palletsize = imagecolorstotal($src);
if($src_transparency >= 0 && $src_transparency < $palletsize) {
$transparent_color = imagecolorsforindex($src, $src_transparency);
$current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($dst, 0, 0, $current_transparent);
imagecolortransparent($dst, $current_transparent);
} else {
imagefill($dst, 0, 0, $bgcolor);
}
} else {
imagefill($dst, 0, 0, $bgcolor);
}
}
 
imagecopyresampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 
// sharpen 적용
if($is_sharpen && $is_large) {
$val = explode('/', $um_value);
UnsharpMask($dst, $val[0], $val[1], $val[2]);
}
 
if($size[2] == 1) {
imagegif($dst, $thumb_file);
} else if($size[2] == 3) {
if(!defined('G5_THUMB_PNG_COMPRESS'))
$png_compress = 5;
else
$png_compress = G5_THUMB_PNG_COMPRESS;
 
imagepng($dst, $thumb_file, $png_compress);
} else {
if(!defined('G5_THUMB_JPG_QUALITY'))
$jpg_quality = 90;
else
$jpg_quality = G5_THUMB_JPG_QUALITY;
 
imagejpeg($dst, $thumb_file, $jpg_quality);
}
 
chmod($thumb_file, G5_FILE_PERMISSION); // 추후 삭제를 위하여 파일모드 변경
 
imagedestroy($src);
imagedestroy($dst);
 
return basename($thumb_file);
}

 

이 질문에 댓글 쓰기 :

답변 1

세로, 가로 긴 값을 width 로 변경하게 됩니다.

// 세로사진의 경우 가로, 세로 값 바꿈

해당 주석 값을 찾아 주석처리해주세요

 

$max=$thumb_width; // 여기서부터 추가 시작
if ( $size[0] > $size[1] ) {
$thumb_width = $max ;
$thumb_height = ceil( $size[1] * ( $max / $size[0] ) );
}
else if ( $size[0] < $size[1] ) {
$thumb_height = $max ;
$thumb_width = ceil( $size[0] * ( $max / $size[1] ) );
}
else if ( $size[0] == $size[1] ) {
$thumb_width = ceil( $max );
$thumb_height = ceil( $max );
} // 여기까지 끝

 

이 값 역시 아래 값으로 변경해주세요

 

$max=$thumb_width; // 여기서부터 추가 시작

$thumb_width = $max ;

$thumb_height = ceil( $size[1] * ( $max / $size[0] ) );

 

이렇게 처리하면 될 것 같습니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 6
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT