다양한 유형의 썸네일 생성
- /*************************************
- *저자: 꿈꾸는 전갈자리(블럭)
- * 완료 시간: 2006-12-18
- *클래스 이름: CreatMiniature
- *기능: 여러 유형의 썸네일 생성
- *기본 매개변수: $srcFile, $echoType
- *사용 방법 도착 매개변수:
- $toFile, 생성된 파일
- $toW, 생성된 너비
- $toH, 생성된 높이
- $bk1, 255가 가장 높은 배경색 매개변수
- $bk2, 배경색 매개변수
- $bk3 , 배경색 매개변수
- * 예:
- include("thumb.php");
- $cm=new CreatMiniature();
- $cm->SetVar(" 1.jpg","file");
- $cm->Distortion("dis_bei.jpg",150,200);
- $cm->Prorate("pro_bei.jpg",150,200);
- $cm->Cut("cut_bei.jpg",150,200);
- $cm->BackFill("fill_bei.jpg",150,200);
-
- ******** ********************************/
-
- class CreatMiniature
- {
- //공용 변수
- var $srcFile=""; //원본 이미지
- var $echoType; //파일로 저장되지 않은 링크
- var $im=""; /임시 변수
- var $srcW=""; //원본 이미지 너비
- var $srcH=""; //원본 이미지 높이
-
- //변수 설정 및 초기화
- function SetVar ($ srcFile,$echoType)
- {
- $this->srcFile=$srcFile;
- $this->echoType=$echoType;
-
- $info = "";
- $data = GetImageSize($this->srcFile,$info);
- 스위치($data[2])
- {
- 사례 1:
- if(!function_exists("imagecreatefromgif" )) {
- echo "GD 라이브러리는 GIF 형식의 이미지를 사용할 수 없습니다. Jpeg 또는 PNG 형식을 사용하십시오!반환";
- exit();
- }
- $this->im = ImageCreateFromGIF($this->srcFile);
- break;
- 사례 2:
- if(!function_exists(" imagecreatefromjpeg")){
- echo "GD 라이브러리는 jpeg 형식의 이미지를 사용할 수 없습니다. 다른 형식의 이미지를 사용하세요!返回";
- exit();
- }
- $this->im = ImageCreateFromJpeg($this ->srcFile);
- break;
- 사례 3:
- $this->im = ImageCreateFromPNG($this->srcFile)
- break;
- }
- $ this->srcW=ImageSX($this->im);
- $this->srcH=ImageSY($this->im)
- }
-
- //생성型缩图
- function Distortion($toFile,$toW,$toH)
- {
- $cImg=$this->CreatImage($this->im,$toW,$toH,0, 0,0,0,$this->srcW,$this->srcH);
- return $this->EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- }
-
- //生成按比例缩放的缩图
- function Prorate($toFile,$toW,$toH)
- {
- $toWH=$toW/$toH;
- $srcWH=$this->srcW/$this->srcH;
- if($toWH< =$srcWH)
- {
- $ftoW=$toW;
- $ftoH=$ ftoW*($this->srcH/$this->srcW);
- }
- else
- {
- $ftoH=$toH;
- $ftoW=$ftoH*($ this->srcW/$this->srcH);
- }
- if($this->srcW>$toW||$this->srcH>$toH)
- {
- $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
- return $this->EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- }
- else
- {
- $cImg=$this->CreatImage($ this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
- return $this- >EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- }
- }
-
- //生成最小裁剪后的缩图
- function Cut($ toFile,$toW,$toH)
- {
- $toWH=$toW/$toH;
- $srcWH=$this->srcW/$this->srcH;
- if($ WH< =$srcWH)
- {
- $ctoH=$toH;
- $ctoW=$ctoH*($this->srcW/$this->srcH);
- }
- else
- {
- $ctoW=$toW;
- $ctoH=$ctoW*($this->srcH/$this->srcW);
- }
- $allImg=$this ->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
- $cImg=$this- >CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
- $this 반환 ->EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- ImageDestroy($allImg);
- }
-
- //生成背景填充的缩图
- 기능 BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
- {
- $toWH=$toW/$toH;
- $srcWH= $this->srcW/$this->srcH;
- if($toWH< =$srcWH)
- {
- $ftoW=$toW;
- $ftoH=$ftoW*($ this->srcH/$this->srcW);
- }
- else
- {
- $ftoH=$toH;
- $ftoW=$ftoH*($this-> srcW/$this->srcH);
- }
- if(function_exists("imagecreatetruecolor"))
- {
- @$cImg=ImageCreateTrueColor($toW,$toH);
- if (!$cImg)
- {
- $cImg=ImageCreate($toW,$toH);
- }
- }
- else
- {
- $cImg=ImageCreate($toW ,$toH);
- }
- $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜color
- ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
- if($this->srcW>$toW||$this->srcH> ;$toH)
- {
- $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$ this->srcH);
- /*
- if($ftoW< $toW)
- {
- ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0, 0,0,$ftoW,$ftoH,100);
- }
- else if($ftoH<$toH)
- {
- ImageCopyMerge($cImg,$proImg,0,($toH- $ftoH)/2,0,0,$ftoW,$ftoH,100);
- }
- */
- if($ftoW<$toW)
- {
- ImageCopy($cImg ,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
- }
- else if($ftoH<$toH)
- {
- ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
- }
- else
- {
- ImageCopy($cImg ,$proImg,0,0,0,0,$ftoW,$ftoH);
- }
- }
- else
- {
- ImageCopyMerge($cImg,$this->im, ($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
- }
- return $this->EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- }
-
- function CreatImage($img,$creatW,$creatH,$ dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
- {
- if(function_exists("imagecreatetruecolor"))
- {
- @$creatImg = ImageCreateTrueColor($creatW,$ creatH);
- if($creatImg)
- ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
- else
- {
- $creatImg=ImageCreate($creatW,$creatH);
- ImageCopyReised($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$ srcImgW,$srcImgH);
- }
- }
- else
- {
- $creatImg=ImageCreate($creatW,$creatH);
- ImageCopyReised($creatImg,$img,$dstX ,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
- }
- return $creatImg;
- }
-
- //출동그림,링크 ---只输流,不保存文件。file--保存为文件
- function EchoImage($img,$to_File)
- {
- switch($this->echoType)
- {
- 케이스 "링크":
- if(function_exists('imagejpeg')) return ImageJpeg($img);
- else return ImagePNG($img);
- break;
- 케이스 "파일":
- if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
- else return ImagePNG($img,$to_File);
- break;
- }
- }
-
- }
- ?>
复代码
|