Forum Moderators: coopster

Message Too Old, No Replies

GD Image Processing - Maintaining Alpha

Finding out how to keep an images transparency after processing

         

Jackally

8:28 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



Hey,

I've been attempting to build a PHP script that takes a PNG image with a transparent background (made earlier in Fireworks), and adds a "smooth" filter, before writing some text onto it and outputting it to a HTML page.

The problem is, if I load this PNG image with a transparent background into the script, the script automatically fills in the transparent background with white.


<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
imagefilter($im, IMG_FILTER_SMOOTH,1);
/* Generate the text to write onto the image (the PHP Version). */
/* Don't know much about this */
$font = 'tahoma.ttf';
$bbox = imagettfbbox(10, 45, $font, 'Powered by PHP' . phpversion());
/* Coordinates for the text */
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 10;
$y = $bbox[1] + (imagesy($im) / 7) - ($bbox[5] / 10) - 5;
/* Write text */
$textcolor = imagecolorallocate($im, 0, 3, 0);
imagettftext($im, 9, 0, $x, $y, $textcolor, $font, 'Powered by PHP ' . phpversion());
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('base.png');
imagepng($img);
?>

So I then thought I try using "imagecolortransparent" to remove the white background. This worked, until I got to adding the "smooth" filter, which then filled in the alpha area with black.


<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
// Removes white background (made from the original transparency)
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background transparent
imagecolortransparent($im, $white);
imagefilter($im, IMG_FILTER_SMOOTH,1);
//Generate the text to write onto the image (the PHP Version).
//Don't know much about this
$font = 'tahoma.ttf';
$bbox = imagettfbbox(10, 45, $font, 'Powered by PHP' . phpversion());
// Coordinates for the text
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 10;
$y = $bbox[1] + (imagesy($im) / 7) - ($bbox[5] / 10) - 5;
// Write text
$textcolor = imagecolorallocate($im, 0, 3, 0);
imagettftext($im, 9, 0, $x, $y, $textcolor, $font, 'Powered by PHP ' . phpversion());
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('base.png');
imagepng($img);
?>

Next, I tried using a transparent GIF image. The alpha in this image was processed by the script fine, though again, when I got to adding the "smooth" filter, it filled in the alpha area with black.

Is there anything else I can do? Am I missing some crucial function or detail?

Note: I'd much prefer if I could stick to PNG rather than GIF, if possible.

My base image is available at
<snip>

Thanks in advance.

[edited by: eelixduppy at 8:33 pm (utc) on Sep. 29, 2008]
[edit reason] no URLs, please [/edit]

grallis

10:48 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



HI Jackally -

I'm by no means a GD wizard, but have written some image modifying scripts, and I'll tell you I had a rough time with PNGs as well. Some PNGs would work fine, others would not depending on what type of PNG it was. I have yet to find a free PHP snippet online that would properly do anything useful to an image beyond resizing a JPEG. I have also yet to hear of anyone I know who has successfully written a fully-featured PHP script to modify images.

I think one reason is PHP is kind of slow when it comes to messing with images. Second reason may be that the GD library isn't quite as handy as we'd like it to be.

My suggestion is to, if you can, use PHP to execute a server-side Java or C application to take care of the images for you.

Java is quite handy with images - it has a few amazing native methods that can do anything you'd like to an image, far better than GD and a lot faster. If I can dig up my old script, I'll let you know what I did.

Jackally

6:33 am on Sep 30, 2008 (gmt 0)

10+ Year Member



Okay, thanks; in the meantime i'll look into your suggestions.