Quantcast
Channel: PHP Image Cant Make background transparent - Stack Overflow
Viewing all articles
Browse latest Browse all 2

PHP Image Cant Make background transparent

$
0
0

I've got a snippet of code,

The idea of it is to create a transparent canvas to a defined height/width, so I'm able to place either a jpg/png/gif in there.

However if it places a PNG in there with transparency it needs to preserve the transparency for it.

This is a sample of the code

    header("Content-type: image/png");    $canvas = imagecreatetruecolor($maxWidth, $maxHeight);    $image = $_GET['file'];    $leftOffset = ($maxWidth / 2) - ($width / 2);    $topOffset = ($maxHeight / 2) - ($height / 2);    $quality    = (isset($_GET['quality'])) ? (int) ceil($_GET['quality'] / 10) : ceil($DEFAULT_QUALITY / 10);    $quality = $quality == 10 ? 9 : $quality;     switch($mime){        case "image/jpeg":            $image = imagecreatefromjpeg($image);            $red = imagecolorallocate($canvas, 0, 255, 0);            imagecolortransparent($canvas, $red);            imagefill($canvas, 0, 0 ,$red);        break;        case "image/gif":            $image = imagecreatefromgif($image);        break;        case "image/png":            $background = imagecolorallocate($canvas, 255, 0, 0);            imagecolortransparent($canvas, $background);            imagealphablending($canvas, false);            imagesavealpha($canvas, true);             $image = imagecreatefrompng($image);        break;    }    imagecopyresampled($canvas, $image, $leftOffset, $topOffset, 0, 0, $width, $height, $width, $width);    imagepng($canvas, null, $quality);    imagedestroy($canvas);

However the problem is, the canvas remains black, whilst the box area around the image is transparent.

The image below demonstrates this, the Lime green colour is the body {background: lime;} so you're able to see the transparent areas

enter image description here

I tried using a transparent colour after the imagecreatetruecolor

$red = imagecolorallocate($canvas, 255, 0, 0);imagecolortransparent($canvas, $red);imagefill($canvas, 0, 0 ,$red);

If the .png has a fade effect, it gets a nasty red glow around it where the colour is no longer exactly 255, 0, 0

Any suggestions?

Thanks


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images