Que faire si le filigrane php png est opaque

藏色散人
Libérer: 2023-03-09 15:44:02
original
2427 Les gens l'ont consulté

Comment rendre le filigrane php png opaque : ajoutez d'abord un filigrane à l'image via "public function water(){...}" puis utilisez "imagecolorallocate($wImage...);" pour empêcher le png d'être transparent, rendez simplement l'arrière-plan noir.

Que faire si le filigrane php png est opaque

L'environnement d'exploitation de cet article : système Windows 7, PHP version 7.1, ordinateur DELL G3

Ajouter un filigrane à l'image php (le filigrane l'image png devient noire transparente)

La première consiste à ajouter un filigrane à l'image L'utilisation de cette fonction est très simple et l'écriture est très détaillée

/**
     * 为图片添加水印
     * @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);
    }
Copier après la connexion

De plus, la fonction ci-dessus. doit utiliser la fonction suivante

 /**
     * 取得图像信息
     * @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;
        }
    }
Copier après la connexion

Vous rencontrerez ce problème plus tard - lorsque le filigrane a un fond transparent, l'arrière-plan de l'image générée est noir

Comme ; montré dans l'image

Que faire si le filigrane php png est opaque

De cette façon, vous devez effectuer un certain traitement sur l'image du filigrane, comme suit

//2.上色   //防止png透明背景变黑 
        $color=imagecolorallocate($wImage,255,255,255); 
        //3.设置透明 
        imagecolortransparent($wImage,$color); 
        imagefill($wImage,0,0,$color);
Copier après la connexion

De cette façon, vous pouvez ajouter le filigrane à nouveau

Que faire si le filigrane php png est opaque

Apprentissage recommandé : "Tutoriel vidéo PHP"

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!