產生多種類型的縮圖
-
/***************************************
- *作者:落夢天蠍(beluckly)
- *完成時間: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; //輸出圖片類型,link--不儲存為檔案;file--儲存為檔案
- var $im=""; //暫存變數
- var $srcW=""; //原圖寬
- var $srcH=""; //原圖高
-
- //設定變數與初始化
- function SetVar($srcFile ,$echoType)
- {
- $this->srcFile=$srcFile;
- $this->echoType=$echoType;
-
- $info = "";
- $data = GetImageSize($this->srcFile,$info);
- switch ($data[2])
- {
- case 1:
- if(!function_exists("imagecreatefrom")){
- echo "你的GD庫不能使用GIF格式的圖片,請使用Jpeg或PNG格式!返回";
- exit();
- }
- $this->im = ImageCreateFromGIF($this->srcFile);
- break;
- case 2:
- if(!function_exists("imagecreatefromjpeg"){首選項";
- exit(); ;
- 鍵;
- 鍵3:
- $this->im = ImageCreateFromPNG($this->srcFile); $this->im);
- $this->srcH=ImageSY($this->im); ,$toW,$toH )
- {
- $cImg=$this->CreateImage($this->im,$toW,$toH,0, 0,0,0,$this->srcW,$this->srcH);
- 回傳$this -> EchoImage ( $ cImg , $ toFile ); 屬性函數Prorate($toFile,$toW,$toH)
- {
- $toWH=$toW/$toH;
- $srcWH=$this-> srcW/ $this->srcH;
- if($toWH {
- $ftoW=$toW;
- $ftoH=$ftoW*( $this->srcH/$ this-> srcW ); > }
- if ( $this -> srcW > $toW || $this -> srcH > $toH )
- {
- $cImg = $ this -> CreateImage ( $this -> im , $ftoW , $ftoH , 0 0 , 0 , 0 , $ this -> srcW , $ this -> srcH ) ;
- }
- else
-
- $cImg=$ this->CreateImage($this->im,$this->srcW,$this->srcH,0,0,0,0; , $this->srcW , $this-> srcH ); / 傳回輸入檔案的金鑰
- function Cut($ toFile,$toW,$toH )
- {
- $toWH=$toW/$toH;
- $ srcWH =$this- >srcW/$this->srcH;
- if($至WH9 =$srcWH)
- {
- $ctoH=$toH;
- $ctoW=$ctoH*( $this ->srcW/ $this->srcH);
- }
- else
- {
- $ctoW=$toW;
- $ctoH=$ctoW*($this->srcH/ $this - > srcW ) ; this - > srcH ); /2, $toW, $toH );
- return $this ->EchoImage($cImg,$toFile); }
-
- // 安裝預設檔
- 返回BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
- {
- $toWH=$toW/$toH;
- $srcWH =$this->srcW/$this->srcH;
- if($toWH {
- $ftoW=$toW;
- $ftoH=$ ftoW*($this ->srcH/$this->srcW)
- $ftoW=$ftoH*($this->; srcW/$this->srcH);
- }
- if(function_exists("createTrueColorImage"))
- {
- @$cImg=CreateTrueColorImage($toW,$ ); if ( !$cImg)
- {
- $cImg=CreateImage($toW,$toH); ($toW ,$toH於);
- }
- $backcolor = imageColorAllocate ($cImg, $bk1) , $bk2, $bk3); //公共配置規範
- ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
- if($this->srcW>$toW||$this->srcH > ;$ toH)
- {
- $proImg=$this->CreateImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this-> srcH ) , $ftoW , $ftoH , 100 ); / 2 , 0 , 0 , $ftoW , $ftoH , 100 ); ); $cImg, $proImg, 0,($toH-$ftoH)/2,0, 0,$ftoW,$ftoH); $proImg , 0 , 0 , 0 , $ ftoW , $ ftoH ); ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH, 100);
- return $this->EchoImage($cImg,$toFile);
- ImageDestroy($cImg);
- }
-
- function CreatImage($img,$creatW,$creatH,$stX ,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
- {
- if(function_exists("imagecreatetruecolor"))
- {
- @$creatImg = ImageCreate. );
- if($creatImg)
- ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
- {
- $creatImg=ImageCreate($creatW,$creatH);
- ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$creatW$4,$,$creatW$Y,$creatW$4,$ ,$srcImgH);
- }
- }
- else
- {
- $creatImg=ImageCreate($creatW,$creatH);
- ImageCopyResized($creatImg,$3,$ $dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
- }
- return $creatImg;
- }
-
- // 輸出圖片,連結圖片,連結圖片,連結連結--只輸出,不儲存檔案。 "link":
- if(function_exists('imagejpeg')) return ImageJpeg($img);
- else return ImagePNG($img);
- ;
- case "file":break
- if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
- else return ImagePNG($img,$to_File);
- 中斷;
- }
- }
- }
- 🎜>}
- ?>
-
-
- 複製程式碼
-
-
php
|