웹마스터 팁
문서에서 원하는 썸네일 뽑아오기.
2013.02.01 02:30
이 방법은, 요즘 흥하는 소셜커머스형 목록을 가진 게시판에 적용되면 좋은 방법입니다.
관리에 유리해진달까요..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 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도 없는경우 원래 방식처럼 첨부된 또는 본문내에 삽입된 첫번째 이미지를 썸네일로 생성합니다.
댓글 16
-
nado0124
2013.02.01 18:31
-
document.item.php 여셔서, getThumbnail() 함수를 대체하시면됩니다 ^^
-
nado0124
2013.02.01 19:12
document.item.php 파일이 어느 폴더에 있죠? 죄송...
아! 찾았습니다. modules\document\ 에 있는 거 맞죠?
-
헉 ㅠ 죄송합니다.
./modules/document/document.item.php 입니다 ^^
-
이거 괜찮은데요..ㅎㅎ.. 감사합니다...ㅎㅎ
-
감사합니다. ^^
-
군생
2013.02.05 14:43
잘 이식하였습니다 감사합니다~~
-
잘된다니 다행입니다. 감사합니다.
-
매약
2013.02.09 21:09
잘 안되는데, 버전이 다른 건가여? 전 1.5.4.2 사용자 인데여..ㅜㅜ
-
버전관계없을텐데요... getThumbnail() 함수는 쭈욱그대로였으니까요..
찾기(Ctrl + F) 하셔서 function getThumbnail 찾으셔서 그쪽함수전체를 대체해보세요. ^^
-
켠
2013.02.19 00:58
이게.... 외부계정으로 올린 문서내 첨부 이미지도 불러온다는거죠?? 첨부파일로 안넣어도 된다는?!!!
-
아닙니다. ㅠㅠ 소스보시면 아시겠지만.. 첨부된 파일 목록에서 지정한 파일명을 찾아냅니다.
지정한 파일명이 없을경우 thumbnail.jpg 를 찾아내고, 그래도없는경우 원래 동작방식을 유지하는형태입니다.
-
켠
2013.02.19 20:59
아~~ 그렇군요 ! 답변 감사합니다 ~!
-
네 ^^ 요긴하게사용하시길바랍니다
-
그래용
2013.08.07 00:49
위에 내용과는 조금 다른 질문이지만 혹시 링크된 이미지를 모두 썸네일로 만드는 방법이 없을까요?
본문에 <img =""> 이런식으로 여러개 링크해서 이미지를 올리면 1개는 썸네일로 만들어주는데 여러개 만드는 방법을 찾고있습니다.
위에 소스중에서 66번째줄부터 있는 소스를 수정하면 될 것 같지만 도저히 안되네요 ;;;
-
파일명이 정해져있다면
getThumbnail(가로,세로,crop,파일명)
으로 여러개뽑아낼 수 있습니다.
그게아니라면 썸네일을 만들어주는 함수를 따로 짜놓은게 있긴한데.. 별도로 함수를 생성해서 사용하셔두되구요
제목 보고 냅다 달려왔는데요...
제가 워낙 초보라... 어떤 파일에 어느 곳에 , 어떻게 적용하는지 자세히 좀 설명해 주시면 감사하겠습니다.