What to do if php png watermark is opaque

藏色散人
Release: 2023-03-09 15:44:02
Original
2428 people have browsed it

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.

What to do if php png watermark is opaque

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[&#39;height&#39;] < $wInfo[&#39;height&#39;])
            return false;
        //建立图像
        $sCreateFun = "imagecreatefrom" . $sInfo[&#39;type&#39;];
        $sImage = $sCreateFun($source);
        
        $wCreateFun = "imagecreatefrom" . $wInfo[&#39;type&#39;];
        $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[&#39;width&#39;], $wInfo[&#39;height&#39;], $alpha);
        //输出图像
        $ImageFun = &#39;Image&#39; . $sInfo[&#39;type&#39;];
        //如果没有给出保存文件名,默认为原图像名
        if (!$savename) {
            $savename = $source;
            @unlink($source);
        }
        //保存图像
        $ImageFun($sImage, $savename);
        imagedestroy($sImage);
    }
Copy after login

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[&#39;mime&#39;]
            );
            return $info;
        } else {
            return false;
        }
    }
Copy after login

You will encounter this problem later————When the watermark has a transparent background color, the background of the generated picture is black;

As shown

What to do if php png watermark is opaque

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);
Copy after login

In this way, you can add the watermark again

What to do if php png watermark is opaque

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!