테스트 사이트 - 개발 중인 베타 버전입니다

투명 PNG 썸네일 생성을 위한 thumbnail 함수 수정

그누보드5 lib/thumbnail.lib.php 파일의 thumbnail 함수를 아래 코드로 수정합니다.

[code]
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)) {
@mkdir($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 (file_exists($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);
} 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($size[0] < $thumb_width || $size[1] < $thumb_height)
$is_large = false;
}
} else {
if($thumb_height) {
$thumb_width = round(($thumb_height * $size[0]) / $size[1]);
}
}

$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 {
$dst = imagecreatetruecolor($dst_w, $dst_h);

if($src_w < $dst_w) {
if($src_h >= $dst_h) {
$dst_x = round(($dst_w - $src_w) / 2);
$src_h = $dst_h;
} 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;
}
}

if($size[2] == 3) {
$bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagefill($dst, 0, 0, $bgcolor);
imagealphablending($dst, false);
imagesavealpha($dst, true);
} else {
$bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색
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);
}
[/code]

충분한 테스트를 거치지 못해 오류가 있을 수 있습니다. 오류가 있을 경우 댓글로 남겨주시기 바랍니다.

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 6개

원본을 보면 list와 view로 나뉘어져 있는데...
list만 삭제하고 대체하면 되나요? 아니면 둘 다 삭제하고 대체해야 하나요?
thumbanil 함수입니다. list, view 관련 함수는 그대로 유지하셔야 합니다.
초보라 오류 여부는 모르겠네요.
바탕색이 검게 보이는 편이 오히려 더 깔끔해 보이네요.
필요할 경우도 있을 수 있겠으나...
제가 만들고 있는 것과 한 번 비교해 봐야 겠네요.

편리님 수고 많이 하셨고 감사합니다.
딱 필요했는데~ 감사합니다 ㅎㅎ

게시글 목록

번호 제목
772
768
765
755
748
747
744
733
732
728
725
713
708
705
704
700
697
673
671
667
657
651
4
3
1