차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판 | |||
tech:thumbnail_php [2013/04/05 05:00] – V_L | tech:thumbnail_php [2016/07/12 00:56] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | {{tag> | ||
+ | ======PHP로 손톱그림 만들기====== | ||
+ | |||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | <code php><? | ||
+ | /** | ||
+ | * 섬네일 생성 클래스 | ||
+ | * PHPSchool 괴부기님의 코드를 원본으로 상당부분 수정하였습니다. | ||
+ | * | ||
+ | * @class | ||
+ | * @author 괴부기, | ||
+ | * @version 0.2 (2011-06-21) | ||
+ | * @see http:// | ||
+ | * @modified KAi (2011-06-21) | ||
+ | * | ||
+ | * | ||
+ | * - 주요기능 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | * | ||
+ | * - examples | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | class Thumbnail | ||
+ | { | ||
+ | // {{{ Variables | ||
+ | |||
+ | // 기본 옵션 정보 | ||
+ | private static $_TdefaultOptions = Array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | // }}} | ||
+ | // {{{ Functions | ||
+ | |||
+ | /** | ||
+ | * 섬네일 이미지 생성 | ||
+ | * | ||
+ | * @param String $filepath 원본 파일이 있는 경로를 지정한다. | ||
+ | * @param Number $width (optional) 섬네일 넓이. width와 height 둘 중 하나는 반드시 지정되어야 하며, | ||
+ | * 하나가 null 인 경우 다른 값(width 또는 height)을 기준으로 이미지의 비율에 맞게 크기가 설정된다. | ||
+ | * @param Number $height (optional) 섬네일 높이. | ||
+ | * @param String $scale (optional) 축소 방식. 만약, width 또는 height 값 중 하나라도 지정이 안된경우 | ||
+ | * scale 은 자동적으로 SCALE_SHOW_ALL이 되며 이미지 전체가 표시 된다. | ||
+ | * SCALE_EXACT_FIT, | ||
+ | * 맞게 축소가 되는데, SCALE_EXACT_FIT 의 경우 지정된 섬네일 크기에 이미지가 꽉차게 축소된다. | ||
+ | * SCALE_SHOW_ALL 의 경우 섬네일 크기 안에 원본 이미지가 모두 보이도록 축소된다. 이 경우 섬네일에 여백이 | ||
+ | * 생길 수 있다. 기본적으로 여백의 색은 검은색이지만, | ||
+ | * @param Array $options (optional) 기타 옵션. 기타 옵션에서는 출력될 섬네일 파일 형식과, 섬네일 전/후 | ||
+ | * 처리 콜백 함수, 섬네일 저장 경로를 설정 가능하다. | ||
+ | * | ||
+ | * - 옵션 항목 | ||
+ | * $options = Array( | ||
+ | * ' | ||
+ | * ' | ||
+ | * ' | ||
+ | * ' | ||
+ | * ); | ||
+ | * | ||
+ | * export 에서는 gif, jpg, png 를 선택할 수 있다. (기본값은 jpg) | ||
+ | * preprocess, postprocess 에서는 섬네일 생성시 이미지 축소 전/후에 콜백함수를 호출하여 배경 및 워터마크를 | ||
+ | * 넣도록 할 수 있다. 설정방법은 여타 PHP 함수 호출과 마찬가지로, | ||
+ | * 내용 처럼 배열로 클래스 객체와 메소드를 넘겨주면되고, | ||
+ | * 적으면 된다. | ||
+ | * savepath 는 섬네일이 저장된 경로를 설정하는 것으로 미리 설정된 키워드를 통해 원본이미지 경로 정보를 이용 | ||
+ | * 할 수 있다. 기본값은 원본이미지 경로 뒤에 _thumb 이 붙는것으로 원본이미지와 같은 폴더에 저장이 된다. | ||
+ | * 다른 폴더에 저장을 하려면 %PATH% 키워드 앞이나 뒤에 다른 경로를 적거나 %PATH% 대신 다른 경로를 적으면 되며 | ||
+ | * 만약 해당 경로에 폴더가 없다면 자동으로 생성 된다. | ||
+ | * ※ 중요: savepath 설정할때 %EXT% 와 같은 확장자가 빠지면 확장자 없이 이미지 경로가 생성되기 때문에 문제가 | ||
+ | * 될 수 있습니다. | ||
+ | * | ||
+ | * - 설정된 키워드 | ||
+ | * %PATH% 원본 이미지 경로 | ||
+ | * %FILENAME% 원본 이미지 파일명 (확장자 제외) | ||
+ | * %EXT% 섬네일 출력 형식 확장자 (옵션에서 export 로 설정된 형식의 확장자 사용) | ||
+ | * %THUMB_WIDTH 섬네일 넓이 | ||
+ | * %THUMB_HEIGHT 섬네일 높이 | ||
+ | * %IMAGE_WIDTH 원본 이미지 넓이 | ||
+ | * %IMAGE_HEIGHT 원본 이미지 높이 | ||
+ | * | ||
+ | * @return String 섬네일 경로 | ||
+ | */ | ||
+ | public static function create($filepath, | ||
+ | { | ||
+ | // 원본 이미지가 없는 경우 | ||
+ | if ( ! file_exists($filepath)) | ||
+ | Thumbnail:: | ||
+ | |||
+ | // 섬네일 크기가 잘못 지정된 경우 | ||
+ | if ($width <= 1 && $height <= 1) | ||
+ | Thumbnail:: | ||
+ | |||
+ | // 스케일 지정이 안되어 있거나 틀릴 경우 기본 SCALE_SHOW_ALL 으로 지정 | ||
+ | if ( ! $scale || ($scale != SCALE_EXACT_FIT && $scale != SCALE_SHOW_ALL)) | ||
+ | $scale = SCALE_SHOW_ALL; | ||
+ | |||
+ | // 기타 옵션 | ||
+ | if ($options) | ||
+ | $options = array_merge(Thumbnail:: | ||
+ | |||
+ | // 옵션 중 출력 이미지 형식이 잘못 지정된 경우 | ||
+ | if ( ! in_array($options[' | ||
+ | Thumbnail:: | ||
+ | |||
+ | // 이미지 타입이 지원되지 않는 경우 | ||
+ | // 1 = GIF, 2 = JPEG | ||
+ | $type = getimagesize($filepath); | ||
+ | if (($type[2] < 1 || $type[2] > 2) || | ||
+ | ($type[2] ===== 1 && ! function_exists(' | ||
+ | ($type[2] ===== 2 && ! function_exists(' | ||
+ | { | ||
+ | Thumbnail:: | ||
+ | } | ||
+ | |||
+ | |||
+ | // 원본 이미지로부터 Image 객체 생성 | ||
+ | switch ($type[2]) | ||
+ | { | ||
+ | case 1: $image = imagecreatefromgif($filepath); | ||
+ | case 2: $image = imagecreatefromjpeg($filepath); | ||
+ | } | ||
+ | |||
+ | // AntiAlias | ||
+ | if (function_exists(' | ||
+ | imageantialias($image, | ||
+ | |||
+ | |||
+ | // 이미지 크기 설정 | ||
+ | $image_attr = getimagesize($filepath); | ||
+ | $image_width = $image_attr[0]; | ||
+ | $image_height = $image_attr[1]; | ||
+ | |||
+ | if ($width > 0 && $height > 0) | ||
+ | { | ||
+ | // 섬네일 크기 안에 모두 표시 | ||
+ | // 이미지의 가장 큰 면을 기준으로 지정 | ||
+ | switch ($scale) | ||
+ | { | ||
+ | case SCALE_SHOW_ALL: | ||
+ | case SCALE_EXACT_FIT: | ||
+ | default: | ||
+ | } | ||
+ | |||
+ | $thumb_x = $thumb_y = 0; | ||
+ | if ($side ===== ' | ||
+ | { | ||
+ | $ratio = $image_width / $width; | ||
+ | $thumb_width = $width; | ||
+ | $thumb_height = floor($image_height / $ratio); | ||
+ | $thumb_y = round(($height - $thumb_height) / 2); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $ratio = $image_height / $height; | ||
+ | $thumb_width = floor($image_width / $ratio); | ||
+ | $thumb_height = $height; | ||
+ | $thumb_x = round(($width - $thumb_width) / 2); | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // width 또는 height 크기가 지정되지 않았을 경우, | ||
+ | // 지정된 섬네일 크기 비율에 맞게 다른 면의 크기를 맞춤 | ||
+ | $thumb_x = $thumb_y = 0; | ||
+ | if ( ! $width) | ||
+ | { | ||
+ | $thumb_width = $width = intval($image_width / ($image_height / $height)); | ||
+ | $thumb_height = $height; | ||
+ | } | ||
+ | elseif ( ! $height) | ||
+ | { | ||
+ | $thumb_width = $width; | ||
+ | $thumb_height = $height = intval($image_height / ($image_width / $width)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | // 섬네일 객체 생성 | ||
+ | $thumbnail = imagecreatetruecolor($width, | ||
+ | |||
+ | if ($options[' | ||
+ | call_user_func($options[' | ||
+ | |||
+ | @imagecopyresampled($thumbnail, | ||
+ | |||
+ | if ($options[' | ||
+ | call_user_func($options[' | ||
+ | |||
+ | |||
+ | // 저장할 경로 생성 및 디렉토리 검사 | ||
+ | preg_match(' | ||
+ | $savepath = str_replace(Array(' | ||
+ | Thumbnail:: | ||
+ | |||
+ | |||
+ | // 지정된 포멧으로 섬네일이미지 저장 | ||
+ | $iserror = false; | ||
+ | switch ($options[' | ||
+ | { | ||
+ | case EXPORT_GIF: if ( ! imagegif($thumbnail, | ||
+ | case EXPORT_PNG: if ( ! imagepng($thumbnail, | ||
+ | case EXPORT_JPG: | ||
+ | default: if ( ! imagejpeg($thumbnail, | ||
+ | } | ||
+ | |||
+ | if ($iserror) | ||
+ | Thumbnail:: | ||
+ | elseif (Thumbnail:: | ||
+ | { | ||
+ | echo ' | ||
+ | .' | ||
+ | .', | ||
+ | |||
+ | |||
+ | ' | ||
+ | } | ||
+ | |||
+ | return $savepath; | ||
+ | }// END: function create(); | ||
+ | |||
+ | /** | ||
+ | * 기본 옵션 항목을 변경한다. | ||
+ | * | ||
+ | * @param String $name 옵션명 | ||
+ | * @param mixed $value 값 | ||
+ | * @return void | ||
+ | */ | ||
+ | public static function setOption($name, | ||
+ | { | ||
+ | Thumbnail:: | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 기본 옵션 항목의 값을 반환한다. | ||
+ | * | ||
+ | * @param String $name 옵션명 | ||
+ | * @return mixed 값 | ||
+ | */ | ||
+ | public static function getOption($name) | ||
+ | { | ||
+ | return Thumbnail:: | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 경로가 존재하는지 체크하고 없다면 폴더를 생성 | ||
+ | * @param String $path 체크할 경로 | ||
+ | * @return Boolean true | ||
+ | */ | ||
+ | public static function validatePath($path) | ||
+ | { | ||
+ | $a = explode('/', | ||
+ | $p = ''; | ||
+ | foreach ($a as $v) | ||
+ | { | ||
+ | $p.= $v .'/'; | ||
+ | if ( ! is_dir($p)) | ||
+ | mkdir($p, | ||
+ | } | ||
+ | |||
+ | return true; | ||
+ | }// END: function validatePath(); | ||
+ | |||
+ | /** | ||
+ | * 오류 처리 핸들러 | ||
+ | * @param String $msg 메시지 | ||
+ | * @param int $code 오류 코드 | ||
+ | * @param int $type 오류 형식 | ||
+ | */ | ||
+ | public static function raiseError($msg, | ||
+ | { | ||
+ | die($msg); | ||
+ | }// END: function raiseError(); | ||
+ | |||
+ | // }}} | ||
+ | }// END: class Thumbnail | ||
+ | |||
+ | /** | ||
+ | * 샘플 워터마크 클래스 | ||
+ | * 이 클래스에서는 이미지 배경색과 테두리 선을 추가하는 예를 보여줍니다. | ||
+ | * preprocess 및 postprocess 함수에서는 Thumbnail 클래스에서 지정한 파라메터를 넘겨 받습니다. | ||
+ | */ | ||
+ | class ThumbnailWatermark | ||
+ | { | ||
+ | /** | ||
+ | * Thumbnail 클래스에서 preprocess 및 postprocess 함수 호출시 넘겨주는 파라메터들 | ||
+ | * | ||
+ | * @param Resource $resource GD Image 함수용 섬네일 리소스 | ||
+ | * @param Number $thumb_width 섬네일 넓이 | ||
+ | * @param Number $thumb_height 섬네일 높이 | ||
+ | * @param Number $image_width 섬네일 안에서 축소된 이미지의 넓이 | ||
+ | * @param Number $image_height 섬네일 안에서 축소된 이미지의 높이 | ||
+ | * ※만약, 섬네일 scale 이 SCALE_SHOW_ALL 일 경우, 섬네일 크기보다 이미지가 작아질 수 있습니다. | ||
+ | * @return void | ||
+ | */ | ||
+ | public function preprocess($resource, | ||
+ | { | ||
+ | // 입력한 색상으로 전체 이미지를 칠한다. | ||
+ | $color = ImageColorAllocate($resource, | ||
+ | ImageFilledRectangle($resource, | ||
+ | } | ||
+ | |||
+ | public function postprocess($resource, | ||
+ | { | ||
+ | $color = ImageColorAllocate($resource, | ||
+ | ImageLine($resource, | ||
+ | ImageLine($resource, | ||
+ | ImageLine($resource, | ||
+ | ImageLine($resource, | ||
+ | } | ||
+ | }// END: class ThumbnailWatermark</ | ||
+ | |||
+ | ^ 누구나 수정하실 수 있습니다. [[http:// | ||
+ | |||
+ | |||