<strong id="m880s"></strong>
  • <sup id="m880s"></sup>
  • <strong id="m880s"></strong>
    查看詳情

    PHP生成指定大小的背景透明且尺寸固定居中顯示的縮略圖

    PHP程序開發過程中經常會碰到這樣的問題,根據網頁前臺要求對原圖片進行處理,生成符合網頁前端尺寸要求的縮略圖,同時,還要滿足居中展示和PNG背景透明的要求。

    舉個例子,有一張800*600的圖片,網頁前端縮略圖尺寸是200*200,PHP需要對這個圖片進行以下處理。

    1、壓縮尺寸,將圖片等比壓縮到200*150,注意,這里的尺寸并不是200*200,因為原圖并不是正方形的圖片,寬度只有600;

    2、新建一個畫布,背景色為白色,根據圖片格式,如果是PNG或GIF圖片,設置背景完全透明;

    3、將之前壓縮成200*150的圖片放入畫布中,注意x軸需要偏移(200-150)/2=25px,這樣才能在畫布中居中。


    流程已經清楚了,接下一就是代碼編寫了,以下是相應的函數代碼,轉載請注明出處:江西居道科技有限公司 www.restaurant-expo.com

    /**
    	 * 生成裁剪圖函數(支持圖片格式:gif、jpeg、png和bmp)
    	 * @param  string $src      源圖片路徑
    	 * @param  int    $width    縮略圖寬度(只指定高度時進行等比縮放)
    	 * @param  int    $width    縮略圖高度(只指定寬度時進行等比縮放)
    	 * @param  string $filename 保存路徑(不指定時直接輸出到瀏覽器)
    	 * @return bool
    	 */
    	function cutThumb($src, $width = null, $height = null, $filename = null) {
    		if (!isset($width) && !isset($height))
    			return false;
    		if (isset($width) && $width < 0)
    			return false;
    		if (isset($height) && $height < 0)
    			return false;
    
    		if(empty($width) && empty($height)) return false;
    
    		$size = getimagesize($src);
    		if (!$size)
    			return false;
    
    
    		list($src_w, $src_h, $src_type) = $size;
    		$src_mime = $size['mime'];
    		switch($src_type) {
    			case 1 :
    				$img_type = 'gif';
    				break;
    			case 2 :
    				$img_type = 'jpeg';
    				break;
    			case 3 :
    				$img_type = 'png';
    				break;
    			case 15 :
    				$img_type = 'wbmp';
    				break;
    			default :
    				return false;
    		}
    		if(empty($width)) $width=$height;
    		if(empty($height)) $height=$width;
    		$rate_h = $height/$src_h;
    		$rate_w = $width/$src_w;	//縮放比例
    		//以大的比例進行縮放
    		$rate = $rate_h<$rate_w?$rate_w:$rate_h;
    		//獲取縮放后的圖片大小,及裁剪的左上角位置
    		if($rate<1){
    			$w = $src_w*$rate;
    			$h = $src_h*$rate;
    			$x = ($w-$width)/2;
    			$y = ($h-$height)/2;
    		}else{
    			//不需要進行縮放
    			$w = $src_w;
    			$h = $src_h;
    			$x=0;$y=0;
    			//超出的一邊進行裁剪
    			if($w>$width){
    				$x = ($w-$width)/2;
    			}
    			if($h>$height){
    				$y = ($h-$height)/2;
    			}
    		}
    
    		$imagecreatefunc = 'imagecreatefrom' . $img_type;
    		$src_img = $imagecreatefunc($src);
    		imagesavealpha($src_img,true);
    		//先進行縮放
    		$resize_img = imagecreatetruecolor($w, $h);
    		imagealphablending($resize_img,false);//這里很重要,意思是不合并顏色,直接用$img圖像顏色替換,包括透明色;
    		imagesavealpha($resize_img,true);//這里很重要,意思是不要丟了$thumb圖像的透明色;
    		imagecopyresampled($resize_img, $src_img,0, 0, 0, 0,  $w, $h, $src_w, $src_h);
    
    		//完成縮放,再進行裁剪
    		$dest_img = imagecreatetruecolor($width, $height);
    		imagealphablending($dest_img,false);//這里很重要,意思是不合并顏色,直接用$img圖像顏色替換,包括透明色;
    		imagesavealpha($dest_img,true);//這里很重要,意思是不要丟了圖像的透明色;
    		if ($img_type!='png' && $img_type!='gif'){
    			$bg = imagecolorallocate($dest_img,255,255,255);		//設置背景色為白色,
    		}else{
    			$bg = imagecolorallocatealpha($dest_img,255,255,255,127);	//設置背景色為白色,127表示背景完全透明
    		}
    		imagecolortransparent($dest_img,$bg);	//設置透明
    		imagefill($dest_img,0,0,$bg);
    		imagecopyresampled($dest_img, $resize_img, ($width-$w)/2, ($height-$h)/2,0, 0,$w, $h, $w, $h);	//這里不需要再進行縮放,直接裁剪就可以,所以后面4個參數兩兩相同
    
    		$imagefunc = 'image' . $img_type;
    		if ($filename) {
    			$imagefunc($dest_img, $filename,$img_type=='png'?CONST_IMGQUALITY/10:CONST_IMGQUALITY);	//PNG格式的圖片質量值介于0-9之間
    		} else {
    			header('Content-Type: ' . $src_mime);
    			$imagefunc($dest_img);
    		}
    		imagedestroy($src_img);
    		imagedestroy($resize_img);
    		imagedestroy($dest_img);
    		return true;
    	}

    PHP生成指定大小的背景透明且尺寸固定居中顯示的縮略圖 下載


    原創內容,轉載請注明出處:網站建設,APP開發,小程序開發請找江西居道科技有限公司,http://www.restaurant-expo.com

    智能建站系統代理招商
    所屬分類:文章中心??????Time:2020-10-14 22:46:25??????人氣:459
    關閉
    13517086454
    精品国产精品久久一区免费式|亚洲第一成年网站在线观看|大香伊蕉国产综合影院|天天上天天添天天爱有声
    <strong id="m880s"></strong>
  • <sup id="m880s"></sup>
  • <strong id="m880s"></strong>