$o_pic = '1.jpg';//要處理的色階起始值$begin_r = 215;$begin_g = 215;$begin_b = 215;list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 獲取原圖像信息$file_ext = get_ext($o_pic);//獲取擴展名$target_im = imagecreatetruecolor($src_w,$src_h);//新圖if($file_ext == 'jpg') //轉換JPG 開始{ $src_im = ImageCreateFromJPEG($o_pic); imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100); for($x = 0; $x < $src_w; $x++) { for($y = 0; $y < $src_h; $y++) { $rgb = imagecolorat($src_im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if($r > $begin_r && $g > $begin_g && $b > $begin_b ){ imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b)); } } }}回復內容:
把一張圖片(根據指定的RGB顏色范圍)透明化。但是實際處理當中,下面的代碼值移除了白色,請教是怎么回事?
$o_pic = '1.jpg';//要處理的色階起始值$begin_r = 215;$begin_g = 215;$begin_b = 215;list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 獲取原圖像信息$file_ext = get_ext($o_pic);//獲取擴展名$target_im = imagecreatetruecolor($src_w,$src_h);//新圖if($file_ext == 'jpg') //轉換JPG 開始{ $src_im = ImageCreateFromJPEG($o_pic); imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100); for($x = 0; $x < $src_w; $x++) { for($y = 0; $y < $src_h; $y++) { $rgb = imagecolorat($src_im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if($r > $begin_r && $g > $begin_g && $b > $begin_b ){ imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b)); } } }}
請不要用jpg,因為jpg不支持透明所以變白了,用imagecreatetruecolor或是imagecreatefrompng
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答