php png水印不透明的方法:首先透過「public function water(){...}」為圖片加上浮水印;然後透過「imagecolorallocate($wImage...);」等函數來防止png透明背景變黑即可。
本文操作環境:Windows7系統、PHP7.1版,DELL G3電腦
php圖片加入浮水印(浮水印png圖片透明處變黑)
首先是為圖片加上浮水印,該函數的使用很簡單,寫的很詳細
/** * 为图片添加水印 * @static public * @param string $source 原文件名 * @param string $water 水印图片 * @param string $$savename 添加水印后的图片名 * @param string $alpha 水印的透明度 * @return void */ public function water($source, $water, $savename=null, $alpha=80 ,$w_pos = 9) { //检查文件是否存在 if (!file_exists($source) || !file_exists($water)) return false; //图片信息 $sInfo = self::getImageInfo($source); $wInfo = self::getImageInfo($water); //如果图片小于水印图片,不生成图片 if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height']) return false; //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo['type']; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo['type']; $wImage = $wCreateFun($water); //$wImage //2.上色 //防止png透明背景变黑 $color=imagecolorallocate($wImage,255,255,255); //3.设置透明 imagecolortransparent($wImage,$color); imagefill($wImage,0,0,$color); //设定图像的混色模式 imagealphablending($wImage, true); //imageAlphaBlending($wImage, false); //imageSaveAlpha($wImage, true); //图像位置,默认为右下角右对齐 //$posY = $sInfo["height"] - $wInfo["height"]; //$posX = $sInfo["width"] - $wInfo["width"]; switch($w_pos) { case 1: $wx = 5; $wy = 5; break; case 2: $wx = ($sInfo["width"] - $wInfo["width"]) / 2; $wy = 0; break; case 3: $wx = $sInfo["width"] - $wInfo["width"]; $wy = 0; break; case 4: $wx = 0; $wy = ($sInfo["height"] - $wInfo["height"]) / 2; break; case 5: $wx = ($sInfo["width"] - $wInfo["width"]) / 2; $wy = ($sInfo["height"] - $wInfo["height"]) / 2; break; case 6: $wx = $sInfo["width"] - $wInfo["width"]; $wy = ($sInfo["height"] - $wInfo["height"]) / 2; break; case 7: $wx = 0; $wy = $sInfo["height"] - $wInfo["height"]; break; case 8: $wx = ($sInfo["width"] - $wInfo["width"]) / 2; $wy = $sInfo["height"] - $wInfo["height"]; break; case 9: $wx = $sInfo["width"] - $wInfo["width"]; $wy = $sInfo["height"] - $wInfo["height"]; break; case 10: $wx = rand(0,($sInfo["width"] - $wInfo["width"])); $wy = rand(0,($sInfo["height"] - $wInfo["height"])); break; default: $wx = $sInfo["width"] - $wInfo["width"]; $wy = $sInfo["height"] - $wInfo["height"]; break; } //生成混合图像 imagecopymerge($sImage, $wImage, $wx, $wy, 0, 0, $wInfo['width'], $wInfo['height'], $alpha); //输出图像 $ImageFun = 'Image' . $sInfo['type']; //如果没有给出保存文件名,默认为原图像名 if (!$savename) { $savename = $source; @unlink($source); } //保存图像 $ImageFun($sImage, $savename); imagedestroy($sImage); }
補充,上面的函數中需要用到下面這個函數
/** * 取得图像信息 * @static * @access public * @param string $image 图像文件名 * @return mixed */ static function getImageInfo($img) { $imageInfo = getimagesize($img); if ($imageInfo !== false) { $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1)); $imageSize = filesize($img); $info = array( "width" => $imageInfo[0], "height" => $imageInfo[1], "type" => $imageType, "size" => $imageSize, "mime" => $imageInfo['mime'] ); return $info; } else { return false; } }
後來會遇到這樣的問題--當水印有透明底色時,產生的圖片背景為黑色;
#如圖
################################# #######這樣就需要對水印圖片做一下處理,如下###//2.上色 //防止png透明背景变黑 $color=imagecolorallocate($wImage,255,255,255); //3.设置透明 imagecolortransparent($wImage,$color); imagefill($wImage,0,0,$color);
以上是php png浮水印不透明怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!