简体   繁体   中英

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.

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); before imagefilledrectangle but it didn't make any difference.

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.

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';
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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