Domanda

I'm trying to create a URL, that spits back a 1x1px png with a certain alpha value e.g. www.mysite/com/png.php?alpha=50 which will output a 1x1 png, with the background colour set to black, and having the opacity at 50%.

I've searched through lots of tutorials and posts trying to figure out how to do this, but I can't find anything that works. Is this possible to do with PHP alone?

Here's the closest I've gotten

<?php
header('Content-Type: image/png'); 
$im = imagecreatetruecolor(500, 300);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocatealpha($im, 255, 255, 255, 50);

imagefilledrectangle($im, 0, 0, 500, 300, $black);

// Save the image
imagepng($im);
imagedestroy($im);
?>

However, that seems to overlay a 50% black colour on top of a white background.

È stato utile?

Soluzione

Set the white to transparent, by adding these lines after $black = ...

$colourWhite = imagecolorallocate($im,255,255,255); imagecolortransparent($im,$colourWhite);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top