简体   繁体   English

透明叠加层创建PNG

[英]Transparent Overlay Creating PNG

I have the below function and I've gotten it to the size I need, now I'm adding a background image and then looking to add just text overlay with a transparent background on all the overlay text but imagefilledrectangle applies the variable $white which is expected, I'm trying to see if there is anyway to tell the function to be transparent instead of accepting a colour, white in this instance, the PHP docs say that the colour is a required parameter for imagefilledrectangle , any suggestions greatly appreciated. 我具有以下功能,并且已经达到所需的大小,现在我要添加背景图像,然后希望在所有覆盖文本上仅添加具有透明背景的文本覆盖,但是imagefilledrectangle应用变量$white可以预期,我正在尝试查看是否有某种方法告诉该函数透明而不是接受一种颜色,在这种情况下为白色,PHP文档说该颜色是imagefilledrectangle的必需参数,任何建议都imagefilledrectangle赞赏。

The background image is a local image: 背景图像是本地图像:

define("BACKGROUND_FILE", "background.png");

Function: 功能:

public function generateImage()
{
    if (file_exists(BACKGROUND_FILE)) {     
        $im = @imagecreatefrompng(BACKGROUND_FILE);

        if($im) {
            $white = imagecolorallocate($im, 255, 255, 255);
            $black = imagecolorallocate($im, 115, 150, 195);

            imagefilledrectangle($im, 0, 0, 399, 29, $white);

            $text = 'Test';
            $font = 'arial_narrow_7.ttf';

            imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

            imagepng($im);
            imagedestroy($im);
        } else {
            echo 'Error';
        }
    }
}

I tried adding imagecolortransparent($im, $white); 我尝试添加imagecolortransparent($im, $white); before imagefilledrectangle but it didn't make any difference. imagefilledrectangle之前,但没有任何区别。

Figured it out, there is no need to have the imagefilledrectangle line in the function, the below gives transparent overlay text elements as no fill is now being applied. 弄清楚了,函数中不需要有imagefilledrectangle线,下面给出了透明的覆盖文本元素,因为现在不应用填充。

public function generateImage()
{
    if (file_exists(BACKGROUND_FILE)) {     
        $im = @imagecreatefrompng(BACKGROUND_FILE);

        if($im) {
            $black = imagecolorallocate($im, 115, 150, 195);

            $text = 'Test';
            $font = 'arial_narrow_7.ttf';

            imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

            imagepng($im);
            imagedestroy($im);
        } else {
            echo 'Error';
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM