方式一:
$Dw){ $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; IF($height>$Dh){ $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; } }ElseIF($height>$Dh){ $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; IF($width>$Dw){ $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; } }Else{ $width=$width; $height=$height; } $nImg = ImageCreateTrueColor($width,$height); #建立一個真彩色畫布 ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);#重採樣拷貝部分映像並調整大小 ImageJpeg ($nImg,$Image); #以JPEG格式將映像輸出到瀏覽器或檔案 Return True; #如果是執行產生縮圖操作則 }Else{ $w=ImagesX($Img); $h=ImagesY($Img); $width = $w; $height = $h; $nImg = ImageCreateTrueColor($Dw,$Dh); IF($h/$w>$Dh/$Dw){ #高比較大 $width=$Dw; $height=$h*$Dw/$w; $IntNH=$height-$Dh; ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h); }Else{ #寬比較大 $height=$Dh; $width=$w*$Dh/$h; $IntNW=$width-$Dw; ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h); } ImageJpeg ($nImg,$Image); Return True; }}?>
方式二:
PHP圖片壓縮的類 array("output"=>"imagejpeg"), "gif" => array("output"=>"imagegif"), "png" => array("output"=>"imagepng"), "wbmp" => array("output"=>"image2wbmp"), "jpeg" => array("output"=>"imagejpeg"));/** * 建構函式 */function ThumbHandler(){ $this->mask_font_color = "#ffffff"; $this->font = 2; $this->font_size = 12;}/** * 取得圖片的寬 */function getImgWidth($src){ return imagesx($src);}/** * 取得圖片的高 */function getImgHeight($src){ return imagesy($src);}/** * 設定圖片產生路徑 * * @param string $src_img 圖片產生路徑 */function setSrcImg($src_img, $img_type=null){ if(!file_exists($src_img)) { die("圖片不存在"); } if(!empty($img_type)) { $this->img_type = $img_type; } else { $this->img_type = $this->_getImgType($src_img); } $this->_checkValid($this->img_type); // file_get_contents函數要求php版本>4.3.0 $src = ''; if(function_exists("file_get_contents")) { $src = file_get_contents($src_img); } else { $handle = fopen ($src_img, "r"); while (!feof ($handle)) { $src .= fgets($fd, 4096); } fclose ($handle); } if(empty($src)) { die("圖片源為空白"); } $this->h_src = @ImageCreateFromString($src); $this->src_w = $this->getImgWidth($this->h_src); $this->src_h = $this->getImgHeight($this->h_src);}/** * 設定圖片產生路徑 * * @param string $dst_img 圖片產生路徑 */function setDstImg($dst_img){ $arr = explode('/',$dst_img); $last = array_pop($arr); $path = implode('/',$arr); $this->_mkdirs($path); $this->dst_img = $dst_img;}/** * 設定圖片的顯示品質 * * @param string $n 品質 */function setImgDisplayQuality($n){ $this->img_display_quality = (int)$n;}/** * 設定圖片的產生品質 * * @param string $n 品質 */function setImgCreateQuality($n){ $this->img_create_quality = (int)$n;}/** * 設定文字浮水印 * * @param string $word 浮水印文字 * @param integer $font 浮水印字型 * @param string $color 浮水印字型顏色 */function setMaskWord($word){ $this->mask_word .= $word;}/** * 設定字型顏色 * * @param string $color 字型顏色 */function setMaskFontColor($color="#ffffff"){ $this->mask_font_color = $color;}/** * 設定浮水印字型 * * @param string|integer $font 字型 */function setMaskFont($font=2){ if(!is_numeric($font) && !file_exists($font)) { die("字型檔不存在"); } $this->font = $font;}/** * 設定文字字型大小,僅對truetype字型有效 */function setMaskFontSize($size = "12"){ $this->font_size = $size;}/** * 設定圖片浮水印 * * @param string $img 浮水印圖片源 */function setMaskImg($img){ $this->mask_img = $img;}/** * 設定浮水印橫向位移 * * @param integer $x 橫向位移量 */function setMaskOffsetX($x){ $this->mask_offset_x = (int)$x;}/** * 設定浮水印縱向位移 * * @param integer $y 縱向位移量 */function setMaskOffsetY($y){ $this->mask_offset_y = (int)$y;}/** * 指定浮水印位置 * * @param integer $position 位置,1:左上,2:左下,3:右上,0/4:右下 */function setMaskPosition($position=0){ $this->mask_position = (int)$position;}/** * 設定圖片合并程度 * * @param integer $n 合并程度 */function setMaskImgPct($n){ $this->mask_img_pct = (int)$n;}/** * 設定文字合并程度 * * @param integer $n 合并程度 */function setMaskTxtPct($n){ $this->mask_txt_pct = (int)$n;}/** * 設定縮圖邊框 * * @param (類型) (參數名) (描述) */function setDstImgBorder($size=1, $color="#000000"){ $this->img_border_size = (int)$size; $this->img_border_color = $color;}/** * 水平翻轉 */function flipH(){ $this->_flip_x++;}/** * 垂直翻轉 */function flipV(){ $this->_flip_y++;}/** * 設定剪下類型 * * @param (類型) (參數名) (描述) */function setCutType($type){ $this->cut_type = (int)$type;}/** * 設定圖片剪下 * * @param integer $width 矩形剪下 */function setRectangleCut($width, $height){ $this->fill_w = (int)$width; $this->fill_h = (int)$height;}/** * 設定源圖剪下起始座標點 * * @param (類型) (參數名) (描述) */function setSrcCutPosition($x, $y){ $this->src_x = (int)$x; $this->src_y = (int)$y;}/** * 建立圖片,主函數 * @param integer $a 當缺少第二個參數時,此參數將用作百分比, * 否則作為寬度值 * @param integer $b 圖片縮放後的高度 */function createImg($a, $b=null){ $num = func_num_args(); if(1 == $num) { $r = (int)$a; if($r < 1) { die("圖片縮放比例不得小於1"); } $this->img_scale = $r; $this->_setNewImgSize($r); } if(2 == $num) { $w = (int)$a; $h = (int)$b; if(0 == $w) { die("目標寬度不能為0"); } if(0 == $h) { die("目標高度不能為0"); } $this->_setNewImgSize($w, $h); } if($this->_flip_x%2!=0) { $this->_flipH($this->h_src); } if($this->_flip_y%2!=0) { $this->_flipV($this->h_src); } $this->_createMask(); $this->_output(); // 釋放 if(imagedestroy($this->h_src) && imagedestroy($this->h_dst)) { Return true; } else { Return false; }}/** * 產生浮水印,調用了產生浮水印文字和浮水印圖片兩個方法 */function _createMask(){ if($this->mask_word) { // 擷取字型資訊 $this->_setFontInfo(); if($this->_isFull()) { die("浮水印文字過大"); } else { $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色 $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); $this->_createMaskWord($this->h_dst); } } if($this->mask_img) { $this->_loadMaskImg();//載入時,取得寬高 if($this->_isFull()) { // 將浮水印產生在原圖上再拷 $this->_createMaskImg($this->h_src); $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色 $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->start_y, $this->copy_w, $this->copy_h); } else { // 建立新圖並拷貝 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色 $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); $this->_createMaskImg($this->h_dst); } } if(empty($this->mask_word) && empty($this->mask_img)) { $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色 $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); }}/** * 畫邊框 */function _drawBorder(){ if(!empty($this->img_border_size)) { $c = $this->_parseColor($this->img_border_color); $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色 }}