开发者

PNG manipulation removes transparency

I am simply trying to return an PNG image through PHP, but I have a problem with the transparency not showing right. (Basically one PHP file will be capable of returning any of my images.)

I use simple code to return the image:

<?php
    $im = imagecreatefrompng("images/fakehairsalon.png");
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
?>

The original image looks like this:

PNG manipulation removes transparency

And the one returned via PHP (and that piece 开发者_开发技巧of code) looks like this:

PNG manipulation removes transparency

Is there anything I can do to prevent this and make the image come through normal?


As explained in a user comment, you must do this:

<?php
$im = imagecreatefrompng("borrame.png");
header('Content-Type: image/png');

imagealphablending($im, true); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)

imagepng($im);
imagedestroy($im);
?>

Update: This answer was assuming that your code is an except from a bigger script you are using to do on-the-fly image manipulation.

If you don't want to change the original file, this is a plain waste of memory and CPU cycles. You can use file system functions to read it as a regular file, such as readfile().

It's also worth noting that using PHP to deliver a file only makes sense if you want to do something else as well, such as:

  • Restricting access to the file
  • Keeping a counter
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜