thumbnail 생성함수 수정

This commit is contained in:
chicpro
2013-01-28 19:37:01 +09:00
parent f3511d11b3
commit 5ca7cfde11

View File

@ -9,38 +9,38 @@ function it_img_thumb($filename, $filepath, $thumb_width, $thumb_height, $is_cre
} }
//function thumbnail($bo_table, $file, $width, $height, $is_create=false) //function thumbnail($bo_table, $file, $width, $height, $is_create=false)
function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_height, $is_create); function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_height, $is_create)
{ {
global $g4; global $g4;
$thumb_dir = "$g4[path]/data/thumb/$bo_table"; $thumb_filename = preg_replace("/\.[^\.]+$/i", "", $filename); // 확장자제거
if (!is_dir($thumb_dir)) { if (!is_dir($target_path)) {
@mkdir($thumb_dir, 0707); @mkdir($target_path, 0707);
@chmod($thumb_dir, 0707); @chmod($target_path, 0707);
} }
$thumb_file = "$thumb_dir/{$width}x{$height}_{$file}.png"; $thumb_file = "$target_path/{$thumb_filename}_{$thumb_width}x{$thumb_height}.png";
$thumb_time = @filemtime($thumb_file); $thumb_time = @filemtime($thumb_file);
$source_file = "$g4[path]/data/file/$bo_table/$file"; $source_file = "$source_path/$filename";
$source_time = @filemtime($source_file); $source_time = @filemtime($source_file);
if (file_exists($thumb_file)) { if (file_exists($thumb_file)) {
if ($is_create == false && $source_time < $thumb_time) { if ($is_create == false && $source_time < $thumb_time) {
return $thumb_file; return str_replace($target_path.'/', '', $thumb_file);
} }
} }
$size = @getimagesize($source_file); $size = @getimagesize($source_file);
// 이미지 파일이 없거나 아님 // 이미지 파일이 없거나 아님
if (!$size[0]) { if (!$size[0]) {
if (!$height) $height = $width; if (!$thumb_height) $thumb_height = $thumb_width;
$thumb_file = "$g4[path]/data/thumb/{$width}x{$height}_noimg.gif"; $thumb_file = "$target_path/noimg_{$thumb_width}x{$thumb_height}.png";
if (!file_exists($thumb_file)) { if (!file_exists($thumb_file)) {
$target = imagecreate($width, $height); $target = imagecreate($thumb_width, $thumb_height);
imagecolorallocate($target, 250, 250, 250); imagecolorallocate($target, 250, 250, 250);
imagecopy($target, $target, 0, 0, 0, 0, $width, $height); imagecopy($target, $target, 0, 0, 0, 0, $thumb_width, $thumb_height);
imagepng($target, $thumb_file, 0); imagepng($target, $thumb_file, 0);
@chmod($thumb_file, 0606); // 추후 삭제를 위하여 파일모드 변경 @chmod($thumb_file, 0606); // 추후 삭제를 위하여 파일모드 변경
} }
return $thumb_file; return str_replace($target_path.'/', '', $thumb_file);
} }
$is_imagecopyresampled = false; $is_imagecopyresampled = false;
@ -55,29 +55,29 @@ function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_h
$src = imagecreatefrompng($source_file); $src = imagecreatefrompng($source_file);
} }
if ($width) { if ($thumb_width) {
if ($height) { if ($thumb_height) {
$rate = $width / $size[0]; $rate = $thumb_width / $size[0];
$tmp_height = (int)($size[1] * $rate); $tmp_height = (int)($size[1] * $rate);
if ($tmp_height < $height) { if ($tmp_height < $thumb_height) {
$dst = imagecreatetruecolor($width, $height); $dst = imagecreatetruecolor($thumb_width, $thumb_height);
$bgcolor = imagecolorallocate($dst, 250, 250, 250); // 배경색 여기야!!! $bgcolor = imagecolorallocate($dst, 250, 250, 250); // 배경색 여기야!!!
imagefill($dst, 0, 0, $bgcolor); imagefill($dst, 0, 0, $bgcolor);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $tmp_height, $size[0], $size[1]); imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $tmp_height, $size[0], $size[1]);
} else { } else {
$dst = imagecreatetruecolor($width, $height); $dst = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $tmp_height, $size[0], $size[1]); imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]);
} }
} else { } else {
$rate = $width / $size[0]; $rate = $thumb_width / $size[0];
$tmp_height = (int)($size[1] * $rate); $tmp_height = (int)($size[1] * $rate);
$dst = imagecreatetruecolor($width, $tmp_height); $dst = imagecreatetruecolor($thumb_width, $tmp_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $tmp_height, $size[0], $size[1]); imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $tmp_height, $size[0], $size[1]);
} }
} }
imagepng($dst, $thumb_file, 0); // 0 (no compression) ~ 9 imagepng($dst, $thumb_file, 0); // 0 (no compression) ~ 9
chmod($thumb_file, 0606); // 추후 삭제를 위하여 파일모드 변경 chmod($thumb_file, 0606); // 추후 삭제를 위하여 파일모드 변경
return $thumb_file; return str_replace($target_path.'/', '', $thumb_file);
} }
?> ?>