티스토리 뷰

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

001function 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')
002{
003    global $g5;
004 
005    if(!$thumb_width && !$thumb_height)
006        return;
007 
008    $source_file "$source_path/$filename";
009 
010    if(!is_file($source_file)) // 원본 파일이 없다면
011        return;
012 
013    $size = @getimagesize($source_file);
014    if($size[2] < 1 || $size[2] > 3) // gif, jpg, png 에 대해서만 적용
015        return;
016 
017    if (!is_dir($target_path)) {
018        @mkdir($target_path, G5_DIR_PERMISSION);
019        @chmod($target_path, G5_DIR_PERMISSION);
020    }
021 
022    // 디렉토리가 존재하지 않거나 쓰기 권한이 없으면 썸네일 생성하지 않음
023    if(!(is_dir($target_path) && is_writable($target_path)))
024        return '';
025 
026    // Animated GIF는 썸네일 생성하지 않음
027    if($size[2] == 1) {
028        if(is_animated_gif($source_file))
029            return basename($source_file);
030    }
031 
032    $ext array(1 => 'gif', 2 => 'jpg', 3 => 'png');
033 
034    $thumb_filename = preg_replace("/\.[^\.]+$/i"""$filename); // 확장자제거
035    $thumb_file "$target_path/thumb-{$thumb_filename}_{$thumb_width}x{$thumb_height}.".$ext[$size[2]];
036 
037    $thumb_time = @filemtime($thumb_file);
038    $source_time = @filemtime($source_file);
039 
040    if (file_exists($thumb_file)) {
041        if ($is_create == false && $source_time $thumb_time) {
042            return basename($thumb_file);
043        }
044    }
045 
046    // 원본파일의 GD 이미지 생성
047    $src = null;
048    $degree = 0;
049 
050    if ($size[2] == 1) {
051        $src = imagecreatefromgif($source_file);
052    else if ($size[2] == 2) {
053        $src = imagecreatefromjpeg($source_file);
054 
055        if(function_exists('exif_read_data')) {
056            // exif 정보를 기준으로 회전각도 구함
057            $exif = @exif_read_data($source_file);
058            if(!empty($exif['Orientation'])) {
059                switch($exif['Orientation']) {
060                    case 8:
061                        $degree = 90;
062                        break;
063                    case 3:
064                        $degree = 180;
065                        break;
066                    case 6:
067                        $degree = -90;
068                        break;
069                }
070 
071                // 회전각도 있으면 이미지 회전
072                if($degree) {
073                    $src = imagerotate($src$degree, 0);
074 
075                    // 세로사진의 경우 가로, 세로 값 바꿈
076                    if($degree == 90 || $degree == -90) {
077                        $tmp $size;
078                        $size[0] = $tmp[1];
079                        $size[1] = $tmp[0];
080                    }
081                }
082            }
083        }
084    else if ($size[2] == 3) {
085        $src = imagecreatefrompng($source_file);
086        imagealphablending($src, true);
087    else {
088        return;
089    }
090 
091    if(!$src)
092        return;
093 
094    $is_large = true;
095    // width, height 설정
096    if($thumb_width) {
097        if(!$thumb_height) {
098            $thumb_height round(($thumb_width $size[1]) / $size[0]);
099        else {
100            if($size[0] < $thumb_width || $size[1] < $thumb_height)
101                $is_large = false;
102        }
103    else {
104        if($thumb_height) {
105            $thumb_width round(($thumb_height $size[0]) / $size[1]);
106        }
107    }
108 
109    $dst_x = 0;
110    $dst_y = 0;
111    $src_x = 0;
112    $src_y = 0;
113    $dst_w $thumb_width;
114    $dst_h $thumb_height;
115    $src_w $size[0];
116    $src_h $size[1];
117 
118    $ratio $dst_h $dst_w;
119 
120    if($is_large) {
121        // 크롭처리
122        if($is_crop) {
123            switch($crop_mode)
124            {
125                case 'center':
126                    if($size[1] / $size[0] >= $ratio) {
127                        $src_h round($src_w $ratio);
128                        $src_y round(($size[1] - $src_h) / 2);
129                    else {
130                        $src_w round($size[1] / $ratio);
131                        $src_x round(($size[0] - $src_w) / 2);
132                    }
133                    break;
134                default:
135                    if($size[1] / $size[0] >= $ratio) {
136                        $src_h round($src_w $ratio);
137                    else {
138                        $src_w round($size[1] / $ratio);
139                    }
140                    break;
141            }
142        }
143 
144        $dst = imagecreatetruecolor($dst_w$dst_h);
145 
146        if($size[2] == 3) {
147            imagealphablending($dst, false);
148            imagesavealpha($dst, true);
149        }
150    else {
151        $dst = imagecreatetruecolor($dst_w$dst_h);
152 
153        if($src_w $dst_w) {
154            if($src_h >= $dst_h) {
155                $dst_x round(($dst_w $src_w) / 2);
156                $src_h $dst_h;
157            else {
158                $dst_x round(($dst_w $src_w) / 2);
159                $dst_y round(($dst_h $src_h) / 2);
160                $dst_w $src_w;
161                $dst_h $src_h;
162            }
163        else {
164            if($src_h $dst_h) {
165                $dst_y round(($dst_h $src_h) / 2);
166                $dst_h $src_h;
167                $src_w $dst_w;
168            }
169        }
170 
171        if($size[2] == 3) {
172            $bgcolor = imagecolorallocatealpha($dst, 0, 0, 0, 127);
173            imagefill($dst, 0, 0, $bgcolor);
174            imagealphablending($dst, false);
175            imagesavealpha($dst, true);
176        else {
177            $bgcolor = imagecolorallocate($dst, 255, 255, 255); // 배경색
178            imagefill($dst, 0, 0, $bgcolor);
179        }
180    }
181 
182    imagecopyresampled($dst$src$dst_x$dst_y$src_x$src_y$dst_w$dst_h$src_w$src_h);
183 
184    // sharpen 적용
185    if($is_sharpen && $is_large) {
186        $val explode('/'$um_value);
187        UnsharpMask($dst$val[0], $val[1], $val[2]);
188    }
189 
190    if($size[2] == 1) {
191        imagegif($dst$thumb_file);
192    else if($size[2] == 3) {
193        if(!defined('G5_THUMB_PNG_COMPRESS'))
194            $png_compress = 5;
195        else
196            $png_compress = G5_THUMB_PNG_COMPRESS;
197 
198        imagepng($dst$thumb_file$png_compress);
199    else {
200        if(!defined('G5_THUMB_JPG_QUALITY'))
201            $jpg_quality = 90;
202        else
203            $jpg_quality = G5_THUMB_JPG_QUALITY;
204 
205        imagejpeg($dst$thumb_file$jpg_quality);
206    }
207 
208    chmod($thumb_file, G5_FILE_PERMISSION); // 추후 삭제를 위하여 파일모드 변경
209 
210    imagedestroy($src);
211    imagedestroy($dst);
212 
213    return basename($thumb_file);
214}



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

댓글