zoukankan      html  css  js  c++  java
  • thinkphp5.1 图片处理类think-image的png 缩略,裁剪和添加水印透明度丢失的问题

    官方的图片处理类处理png的时候,透明度会丢失

    下面是解决办法

    要修改的文件:

    vendor/topthink/think-image/src/Image.php

    缩略

    要修改的方法

    public function thumb($width, $height, $type = self::THUMB_SCALING)
    

    按如下修改

    // 调整默认颜色
    $color = imagecolorallocate($img, 255, 255, 255);
    // makalo 修改 缩略
    $color = imagecolorallocatealpha($img, 0, 0, 0,127);
    

    image-20201211191525606

    裁剪

    要修改的方法

    public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
    

    按如下修改

    // 调整默认颜色
    //$color = imagecolorallocate($img, 255, 255, 255);
    // makalo 修改 裁剪
    $color = imagecolorallocatealpha($img, 0, 0, 0,127);
    

    image-20201211191801491

    添加水印

    注意:
    添加水印需要将裁剪的方法一起修改,单独修改不行
    只有png才有透明通道

    要修改的方法

    #按裁剪修改
    public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
    #水印
    public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)
    

    按如下修改

    //imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
    // makalo 修改 为了保持PNG的透明效果
    imagecopy($this->im, $water, $x, $y, 0, 0, $info[0], $info[1]);
    

    image-20201211192040747

    参考:https://www.copylian.com/technology/227.html

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/makalochen/p/14122228.html
Copyright © 2011-2022 走看看