php How to make png watermark opaque: first add watermark to the image through "public function water(){...}"; then use "imagecolorallocate($wImage...);" and other functions to prevent png from being transparent Just make the background black.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
Add watermark to php picture (the watermark png picture becomes transparent Black)
The first is to add a watermark to the image. The use of this function is very simple and the writing is very detailed.
/** * 为图片添加水印 * @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); }
Additionally, the following function needs to be used in the above function
/** * 取得图像信息 * @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; } }
You will encounter this problem later————When the watermark has a transparent background color, the background of the generated picture is black;
As shown
In this way, you need to process the watermark image, as follows
//2.上色 //防止png透明背景变黑 $color=imagecolorallocate($wImage,255,255,255); //3.设置透明 imagecolortransparent($wImage,$color); imagefill($wImage,0,0,$color);
In this way, you can add the watermark again
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php png watermark is opaque. For more information, please follow other related articles on the PHP Chinese website!