웹마스터 팁
page_full_width" class="col-xs-12" |cond="$__Context->page_full_width">
문서에서 원하는 썸네일 뽑아오기.
2013.02.01 02:30
이 방법은, 요즘 흥하는 소셜커머스형 목록을 가진 게시판에 적용되면 좋은 방법입니다.
관리에 유리해진달까요..
function getThumbnail($width = 80, $height = 0, $thumbnail_type = '',$fixed_file_name = false) { // Return false if the document doesn't exist if(!$this->document_srl) return; // If not specify its height, create a square if(!$height) $height = $width; // Return false if neither attachement nor image files in the document if(!$this->get('uploaded_count') && !preg_match("!<img!is", $this->get('content'))) return; // Get thumbnai_type information from document module's configuration if(!in_array($thumbnail_type, array('crop','ratio'))) { $config = $GLOBALS['__document_config__']; if(!$config) { $oDocumentModel = &getModel('document'); $config = $oDocumentModel->getDocumentConfig(); $GLOBALS['__document_config__'] = $config; } $thumbnail_type = $config->thumbnail_type; } // Define thumbnail information $thumbnail_path = sprintf('files/cache/thumbnails/%s',getNumberingPath($this->document_srl, 3)); $thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, $width, $height, $thumbnail_type); $thumbnail_url = Context::getRequestUri().$thumbnail_file; // Return false if thumbnail file exists and its size is 0. Otherwise, return its path /* if(file_exists($thumbnail_file)) { if(filesize($thumbnail_file)<1) return false; else return $thumbnail_url; } */ // Target File $source_file = null; $is_tmp_file = false; // Find an iamge file among attached files if exists if($this->get('uploaded_count')) { $oFileModel = &getModel('file'); $file_list = $oFileModel->getFiles($this->document_srl, array(), 'file_srl', true); if(count($file_list)) { if($fixed_file_name){ foreach($file_list as $file) { if($file->direct_download!='Y') continue; if(!preg_match("/\.(jpg|png|jpeg|gif|bmp)$/i",$file->source_filename)) continue; if($file->source_filename == $fixed_file_name){ $source_file = $file->uploaded_filename; break; } } } if(!$source_file){ foreach($file_list as $file) { if($file->direct_download!='Y') continue; if(!preg_match("/\.(jpg|png|jpeg|gif|bmp)$/i",$file->source_filename)) continue; if(!$source_file) $source_file = $file->uploaded_filename; if($file->source_filename == 'thumbnail.jpg'){ $source_file = $file->uploaded_filename; break; } if(!file_exists($source_file)) $source_file = null; } } } } // If not exists, file an image file from the content if(!$source_file) { $content = $this->get('content'); $target_src = null; preg_match_all("!src=(\"|')([^\"' ]*?)(\"|')!is", $content, $matches, PREG_SET_ORDER); $cnt = count($matches); for($i=0;$i<$cnt;$i++) { $target_src = trim($matches[$i][2]); if(!preg_match("/\.(jpg|png|jpeg|gif|bmp)$/i",$target_src)) continue; if(preg_match('/\/(common|modules|widgets|addons|layouts)\//i', $target_src)) continue; else { if(!preg_match('/^(http|https):\/\//i',$target_src)) $target_src = Context::getRequestUri().$target_src; $tmp_file = sprintf('./files/cache/tmp/%d', md5(rand(111111,999999).$this->document_srl)); if(!is_dir('./files/cache/tmp')) FileHandler::makeDir('./files/cache/tmp'); FileHandler::getRemoteFile($target_src, $tmp_file); if(!file_exists($tmp_file)) continue; else { list($_w, $_h, $_t, $_a) = @getimagesize($tmp_file); if($_w<$width || $_h<$height) continue; $source_file = $tmp_file; $is_tmp_file = true; break; } } } } if($source_file){ $output = FileHandler::createImageFile($source_file, $thumbnail_file, $width, $height, 'jpg', $thumbnail_type); } if($is_tmp_file) FileHandler::removeFile($source_file); // Return its path if a thumbnail is successfully genetated if($output) return $thumbnail_url; // Create an empty file not to re-generate the thumbnail else FileHandler::writeFile($thumbnail_file, '','w'); return; }
썸네일을 생성하는 document.item.php 의 함수를 수정합니다.
제가 필요해서 만들어 쓰는건데 혹시 필요하신분 있을까봐 공유해봅니다.
함수의 네번째 인자가 추가되었습니다.
추가된 인자는 $fixed_file_name 이며, 기존의 함수가 사용되는곳과의 충돌을 막기위해 기본값을 false로 처리하였습니다.
이것은, 썸네일로 지정할 파일이름이며 첨부된 파일중에 지정한 파일이 있는지 검사합니다.
없는경우 thumbnail.jpg 의 파일명을 가진 파일이 썸네일로 지정이되고, thumbnail.jpg도 없는경우 원래 방식처럼 첨부된 또는 본문내에 삽입된 첫번째 이미지를 썸네일로 생성합니다.