[함수] php5 자동 섬네일 만들기 함수 JPG, GIF용 :: PHP5의 추가된 사항을 올리는 곳입니다.[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

PHP5의 추가된 사항을 올리는 곳입니다.
[1]
등록일:2007-10-10 22:57:04 (0%)
작성자:
제목:[함수] php5 자동 섬네일 만들기 함수 JPG, GIF용
스태틱으로  자동  썸네일  만들기를  해  보았습니다.  
아무  클래스에나  사용  가능합니다.  

옵션은  두가지  입니다,  
항산  긴  쪽이  기준이  됩니다.  

1.  가로*세로중  긴쪽을  4,  짧은  쪽을  3으로  
예로  1280x800  이면  4:3  비율로,  800x1280이면  3:4비율로  섬네일을  만듭니다.  
2.  길이에  맞게  비례를  맞춘다.  
예로  1280x800  짜리  이미지를  115px로  만들고  싶으면,  물론  가로가  길기  때문에  가로를  115px로  생성하고  세로는  그에  상응하는  비례에  맞게  사이즈를  맞추어서  생성해줍니다.  
계산  식으로  하면  1280/115  =  11.1,  800/11.1=72.  
해서,  115x72사이즈로  섬네일을  만들어  줍니다.  
1번의  예로  한다면,  115px에  맞춘다면  4:3이니  무조건  115  x  86으로  만들어  줍니다.  

1번은  섬네일  무조건  고정일때,  2번은  이미지  왜곡이  업는  완벽한  섬네일을  만들수  있겟죠.  

물론  파일  경로는  직접  만들어  주셔야  합니다.  

php4에서도  static만  없음  사용  가능하실겁니다.  

/**  
  *  $max_side  원하시는  사이즈를  넣으시면  긴  쪽을  거기에  맞추어  섬네일을  생성한다.  
  *  $fixed  =  true  일때는  무조건  4:3  이나  3:4로  맞추어  생성한다.  false이면  
  *  길이에  비례하는  섬네일을  생성한다.  
  *  $file  원본  파일이  있는  경로를  지정한다.  
  *  예:  "upload/pic/pic1.gif"  
  *  @return  섬네일  경로  
  */  
class  thumb  {  
  public  function  __construct()  {}  

  //  create  thumbnail,  will  accept  jpeg  and  gif.  

        public  static  function  create_thumbnail($file,  $max_side  =  false,  $fixed  =  false)  {  
                //  1  =  GIF,  2  =  JPEG  
                if(!$max_side)  $max_side  =  100;  
                if(file_exists($file))  {                            
                        $type  =  getimagesize($file);  
                  
                        if(!function_exists('imagegif')  &&  $type[2]  ==  1)  {  
                                $error  =  'Filetype  not  supported.  Thumbnail  not  created.';  
                        }  
                        elseif  (!function_exists('imagejpeg')  &&  $type[2]  ==  2)  {  
                                $error  =  'Filetype  not  supported.  Thumbnail  not  created.';  
                        }  
                        else  {          
                                //  create  the  initial  copy  from  the  original  file  
                                if($type[2]  ==  1)  {  
                                        $image  =  imagecreatefromgif($file);  
                                }  
                                elseif($type[2]  ==  2)  {  
                                        $image  =  imagecreatefromjpeg($file);  
                                }  
                                  
                                if(function_exists('imageantialias'))  
                                        imageantialias($image,  TRUE);  
          
                                $image_attr  =  getimagesize($file);  
          
                                //  figure  out  the  longest  side  
          
                                if($image_attr[0]  >  $image_attr[1]):  
                                        $image_width  =  $image_attr[0];  
                                        $image_height  =  $image_attr[1];  
                                                                                
                                        if($fixed)  {  
                                                $image_new_width    =  $max_side;  
                                                $image_new_height  =  (int)($max_side  *  3  /  4);  
                                                //  4:3  ratio  
                                        }  else  {  
                                                $image_new_width  =  $max_side;  
                  
                                                $image_ratio  =  $image_width  /  $image_new_width;  
                                                $image_new_height  =  (int)  ($image_height  /  $image_ratio);  
                                        }  
                                        //width  >  height  
                                else:  
                                        $image_width  =  $image_attr[0];  
                                        $image_height  =  $image_attr[1];  
                                        if($fixed)  {  
                                                $image_new_height  =  $max_side;  
                                                $image_new_width    =  (int)($max_side  *  3  /  4);  
                                                //  3:4  ratio  
                                        }  else  {  
                                                $image_new_height  =  $max_side;  
                  
                                                $image_ratio  =  $image_height  /  $image_new_height;  
                                                $image_new_width  =  (int)  ($image_width  /  $image_ratio);  
                                        }  
                                        //height  >  width  
                                endif;  
          
                                $thumbnail  =  imagecreatetruecolor($image_new_width,  $image_new_height);  
                                @  imagecopyresampled($thumbnail,  $image,  0,  0,  0,  0,  $image_new_width,  $image_new_height,  $image_attr[0],  $image_attr[1]);  
                                  
                                $thumb  =  preg_replace('!(\.[^.]+)?$!',  '.thumbnail'.'$1',  basename($file),  1);  
                                $thumbpath  =  str_replace(basename($file),  $thumb,  $file);  

                                //  move  the  thumbnail  to  it's  final  destination  
                                if($type[2]  ==  1)  {  
                                        if  (!imagegif($thumbnail,  $thumbpath))  {  
                                                $error  =  'Thumbnail  path  invalid';  
                                        }  
                                }  
                                elseif($type[2]  ==  2)  {  
                                        if  (!imagejpeg($thumbnail,  $thumbpath))  {  
                                                $error  =  'Thumbnail  path  invalid';  
                                        }  
                                }          
                        }  
                }  else  {  
                        $error  =  'File  not  found';  
                }  
          
                if(!empty($error))  {  
                        die($error);  
                }  else  {  
                        return  $thumbpath;  
                }  
                  
        }  

};  

$imagethumb  =  thumb::create_thumbnail("upload/pic/pic1.gif",  115);  
//  115에  맞는  비율로  정확히  줄인다.  

$imagethumb  =  thumb::create_thumbnail("upload/pic/pic1.gif",  115,  1);  
//  115로  줄이지만  4:3  이나  3:4이다.  

출처  :  http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=46878&sca=&sfl=wr_subject%7C%7Cwr_content&stx=php5&sop=and&page=3
[본문링크] [함수] php5 자동 섬네일 만들기 함수 JPG, GIF용
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=1082
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.