====== 縮圖 ====== http://my-web-design.blogspot.com/2007/10/phpgd2.html dstimg = $imgout; //來源圖檔 $this->srcimg = $imgsrc; //改變後的寬度 $this->resize_width = $width; //改變後的高度 $this->resize_height = $height; //是否裁圖 $this->cut = $cut; //是否放大圖像 $this->enlarge = $enlarge; //初始化圖檔 $this->initi_img(); //來源圖檔實際寬度 $this->width = imagesx($this->im); //來源圖檔實際高度 $this->height = imagesy($this->im); //生成新圖檔 $this->newimg(); //結束圖形 ImageDestroy ($this->im); } function newimg() { if(($this->cut)=="1") //裁圖 { if($this->enlarge=='0')//不放大圖像,只縮圖 { //調整輸出的圖片大小,如不超過指定的大小則維持原大小 if($this->resize_width < $this->width) $resize_width = $this->resize_width; else $resize_width = $this->width; if($this->resize_height < $this->height) $resize_height = $this->resize_height; else $resize_height = $this->height; } else//放大圖像 { $resize_width = $this->resize_width; $resize_height = $this->resize_height; } //改變後的圖檔的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //實際圖檔的比例 $ratio = ($this->width)/($this->height); if($ratio>=$resize_ratio) //高度優先 { $newimg = imagecreatetruecolor($resize_width,$resize_height); //生成白色背景 $white = imagecolorallocate($newimg, 255, 255, 255); imagefilledrectangle($newimg,0,0,$resize_width,$resize_height,$white); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $resize_width,$resize_height, (($this->height)*$resize_ratio), $this->height); $this->status = ImageJpeg ($newimg,$this->dstimg); } if($ratio<$resize_ratio) //寬度優先 { $newimg = imagecreatetruecolor($resize_width,$resize_height); //生成白色背景 $white = imagecolorallocate($newimg, 255, 255, 255); imagefilledrectangle($newimg,0,0,$resize_width,$resize_height,$white); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $resize_width, $resize_height, $this->width, (($this->width)/$resize_ratio)); $this->status = ImageJpeg ($newimg,$this->dstimg); } } else //不裁圖 { if($this->enlarge=='0')//不放大圖像,只縮圖 { //調整輸出的圖片大小,如不超過指定的大小則維持原大小 if($this->resize_width < $this->width) $resize_width = $this->resize_width; else $resize_width = $this->width; if($this->resize_height < $this->height) $resize_height = $this->resize_height; else $resize_height = $this->height; } else//放大圖像 { $resize_width = $this->resize_width; $resize_height = $this->resize_height; } //改變後的圖檔的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //實際圖檔的比例 $ratio = ($this->width)/($this->height); if($this->width>=$this->height) //圖片較寬 { $newimg = imagecreatetruecolor($resize_width,($resize_height)/$ratio); //生成白色背景 $white = imagecolorallocate($newimg, 255, 255, 255); imagefilledrectangle($newimg,0,0,$resize_width,($resize_width)/$ratio,$white); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $resize_width, ($resize_width)/$ratio, $this->width, $this->height); $this->status = ImageJpeg ($newimg,$this->dstimg); } if($this->width<$this->height) //圖片較高 { $newimg = imagecreatetruecolor(($resize_height)*$ratio,$resize_height); //生成白色背景 $white = imagecolorallocate($newimg, 255, 255, 255); imagefilledrectangle($newimg,0,0,($resize_height)*$ratio,$resize_height,$white); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($resize_height)*$ratio, $resize_height, $this->width, $this->height); $this->status = ImageJpeg ($newimg,$this->dstimg); } } } //初始化圖檔 function initi_img() { //取得圖片的類型 $getimgdata=@getimagesize($this->srcimg); $this->type = $getimgdata['mime']; //根據類型選擇讀取方式 if($this->type=='image/gif') { $this->im = imagecreatefromgif($this->srcimg); } else if($this->type=='image/png') { $this->im = imagecreatefrompng($this->srcimg); } else { $this->im = imagecreatefromjpeg($this->srcimg); } } } ?> 使用方式: