首页 > 解决方案 > 无法在 PHP 函数中使用 RGB 设置透明度

问题描述

在函数中使用 rgb 格式时不能使用透明度:

"message": "无法读取颜色 rgb(0 0 1 / 50%)。"

如果我使用像#ffff 或#000 这样的普通颜色,它会起作用。

我正在使用此功能在图像上添加文本水印。:

public static function addwatermark( $name ) {
    $thumbnail = Image::make($name);
    $thumbnail->text('WATERMARKTEXT', 90, 50, function ($font) use ($thumbnail) {
        $font->file('public/watermarkfont.ttf');
        $font->color(rgb(0 0 1 / 50%));
    });
    $thumbnail->save($name)->destroy();
}

参考:https ://onlinewebtutorblog.com/how-to-add-watermark-text-on-images-laravel-8/

标签: javascriptphpfunctionrgbcolor-scheme

解决方案


问题是您试图在rgb()要使用时添加不透明度rgba(),它具有处理不透明度的 alpha 通道。

public static function color_inverse($color){
    $color = str_replace('#', '', $color);
    if ($color == '000000'){ 
        return "rgba(255, 255, 255, 1)"; 
    }
    return "rgba(0, 0, 0, 0.5)";
}

在这里,您可以找到文档color中水印方法可用的所有格式。

不透明度从101为 100%,0为 0%,因此0.5为 50% 的不透明度。


推荐阅读