Jump to content

GD: Transparency to a gif (from mask?)


Accipiter

Recommended Posts

Hi all,

I am trying to create automatically generated avatars.
The user uploads an image, and then I copy my "foreground" on top of it (in shape of a wreath).
So.. What I have is a truecolor resource in a specific width and height

But as I use imagegif() to send it, I want the "background" colored pixels to be transparent.
Example of how it looks now:
[url=http://www.thesodality.org/test.php]http://www.thesodality.org/test.php[/url]

The [color=blue]blue[/color] is the user image, the entirely brown ones (not the ones antialiasing the green leaves) should be transparent.
My two options are:
1) Using a black and white mask image. This way I won't risk making any pixels within the user image transparent.
2) Setting the brown color to transparent. If the users image contains the exact same brown, it will get holes :P

Unfortunately, I have failed to succeed with any of the two.

Help is highly appreciated!

/Accipiter

Ps. If you want to see how I've done it:

[code]
// $thumb is the user uploaded image, resized to the same width and height as the overlay.

$overlay = imagecreatefrompng($overlay_image_path);
imagealphablending($overlay, true);
imagecopy($thumb, $overlay, 0,0,0,0, $width, $height);

// This part fails!
$image = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocate($image, 223,192,137); // Those numbers are the brown color
imagecolortransparent($image, $transparent);

imagecopy($image, $thumb, 0,0,0,0, $width, $height);


header ("Content-Type: image/gif");

imagegif($image);[/code]
Link to comment
Share on other sites

I finally succeeded.
If anyone else needs to set a selected part transparent, this is one way:

[code]// $thumb is the user uploaded image, resized to the same width and height as the overlay.

// Adding the overlay image upon the users pictures
$overlay = imagecreatefrompng($overlay_image_path);
imagealphablending($overlay, true);
imagecopy($thumb, $overlay, 0,0,0,0, $width, $height);

// Reducing the colors to 255 (not 256. I want the last one available)
imagetruecolortopalette ($thumb, false, 255);

$r = $g = $b = 0;
// Finding a unique, unused color
while( imagecolorexact($thumb, $r, $g, $b) != -1 )
{
if( $r <= $g )
$r++;
elseif( $g <= $b)
$g++;
else
$b++;
}

imagecolorset ( $thumb, 255, $r, $g, $b );  // Set the last, unused index, to our unused color

// The mask is a Black&White image with the White set to transparent.
$mask = imagecreatefromgif($mask_image_path);
$imaskbg = imagecolorexact ( $mask, 0,0,0 );  // Get the palette index of the black color
imagecolorset( $mask, $imaskbg, $r, $g, $b );  // Change the black color to our unused color

imagecopy( $thumb, $mask, 0,0,0,0,$width, $height); // Copy it over. The white will be transparent.
imagecolortransparent($thumb, 255);  // Set our color to transparent.

header ("Content-Type: image/gif'");
imagegif($thumb);[/code]

It worked for me atleast :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.