Accipiter Posted January 24, 2007 Share Posted January 24, 2007 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 heightBut 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 :PUnfortunately, I have failed to succeed with any of the two.Help is highly appreciated!/AccipiterPs. 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 colorimagecolortransparent($image, $transparent);imagecopy($image, $thumb, 0,0,0,0, $width, $height);header ("Content-Type: image/gif");imagegif($image);[/code] Quote Link to comment Share on other sites More sharing options...
Accipiter Posted January 25, 2007 Author Share Posted January 25, 2007 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 colorwhile( 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 colorimagecolorset( $mask, $imaskbg, $r, $g, $b ); // Change the black color to our unused colorimagecopy( $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 :) Quote Link to comment Share on other sites More sharing options...
Accipiter Posted January 25, 2007 Author Share Posted January 25, 2007 (One last question. How do I set a topic as Solved?) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.