Rumah >pembangunan bahagian belakang >masalah PHP >php png水印不透明怎么办
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视频教程》
Atas ialah kandungan terperinci php png水印不透明怎么办. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!