Newsgroup: it.comp.www.php
Titolo: GD Lib e trasparenza immagini

Buonasera a tutto il NG,
ecco il problema che sto cercando di risolvere da tempo senza successo:
durante la creazione delle immagini con le GD 2, se l'immagine sorgente ha
sfondo trasparente (sia essa una gif o una png), l'immagine restituita ha
sfondo nero.
Incollo qui il dettaglio della funzione che utilizzo:
$thumb_src_img = imagecreatefromgif($upfile); 
$thumb_dst_img = imagecreatetruecolor($thumb_w, $thumb_h); 
imagecopyresampled($thumb_dst_img, $thumb_src_img, 0, 0, 0, 0, $thumb_w,   
$thumb_h,imagesx($thumb_src_img),imagesy($thumb_src_img)); 
imagegif($thumb_dst_img, $dst_file, 100);
Tra tutte le mie prove, ho provato la nota n.1 dei contributi degli utenti
riportato nel man alla pagina
http://it.php.net/manual/en/function.imagecolortransparent.php
 senza alcun
successo.
Qualcuno sa aiutarmi?
Grazie in anticipo.
Dana

 Dana
Ci sono varie soluzioni, ad esempio prendi spunto da questi, secondo 
eseigenze:
(da 
http://it.php.net/manual/en/function.imagecopy.php
)
--------------------------------------------------------------------
mbostrom at paragee dot com
30-Nov-2002 09:56
If you want to copy a non-rectangular (hence transparent) image onto a 
background (for example, a pawn onto a chessboard) do the following:
First, create the pawn image pawn.png in your favorite graphics program. 
  Do NOT make the image transparent, instead, give it a distinct solid 
background color.  You will flag this color as transpernt inside PHP, 
otherwise imagecopy will not honor the transparency.
Then:
$board = imagecreatefrompng ("board.png");
$pawn  = imagecreatefrompng ("pawn.png");
imagecolortransparent ($pawn, imagecolorat ($pawn, 0, 0));
imagecopy ($board, $pawn, $x, $y, 0, 0, $pawnWidth, $pawnHeight);
imagedestroy ($pawn);
-----------------------------------------------------------------------
  RT
09-May-2004 12:09
Although the following function doesn't use imagecopy(), I thought it 
might help in related tasks. Please see the code comments for details of 
it's operation. I made this function to assist in creating images using 
multiple "layers". For example if you wanted to dynamically create a 
logo image with seperate colors for say the logo itself and a glow 
around the logo, these steps would be followed:
-Using an image editor (like Photoshop), create a png-24 image with just 
the logo on a transparent background. The logo can be any color or 
multiple colors, but the final image created by this function will be of 
a single color.
-Create a similar image with just the glow (no logo)
-Create a background image
-Apply this colorize() function to the logo image and the glow image 
with your desired color for each.
-You can now use imagecopy() to merge all three into a single image 
ready for a browser.
Here's the code
<?php
/*======Colorize=====
(requires GD 2.0.1 or greater)
This function requires the following arguments:
  $src_path = A string representing the relative path to the src image. 
Ex: "images/myimage.png". This
    image must be a png-24 with an alpha channel.
  $dest_path = A string representing the relative path to the image to 
be created.
  $hex_color = A string representing a color in html format, including 
the # sign. Ex: "#D2E5FF"
This function examines the transparency of the source image, pixel by 
pixel, and creates a new
one-color image with this same "transparency map".
====================*/
function colorize($src_path, $dest_path, $hex_color) {
    //get the png-24 image - it must have an alpha channel for this 
funciton to be effective
    $src = imagecreatefrompng($src_path);
    //get width
    $w = imagesx($src);
    //get height
    $h = imagesy($src);
    //create same size destination image
    $dest = imagecreatetruecolor($w, $h);
    //this must be set to false in order to be able to overwright the 
defualt black pixels of the background with our new
    //transparent pixels. Otherwise our new pixel would just be applied 
on top of the black.
    imagealphablending($dest, false);
    //get decimal components of the passed hex color
    $red = hexdec(substr($hex_color, 1, 2));
    $green = hexdec(substr($hex_color, 3, 2));
    $blue = hexdec(substr($hex_color, 5, 2));
    for ($i = 0; $i < $h; $i++) { //this loop traverses each row in the 
image
        for ($j = 0; $j < $w; $j++) { //this loop traverses each pixel 
of each row
            //get the color & alpha info of the current pixel
            $retrieved_color = imagecolorat($src, $j, $i);
            //put this info into an array
            $rgba_array = imagecolorsforindex($src, $retrieved_color);
            //get the transparency of the pixel as a number from 0 
(opaque) to 127 (transparent)
            $alpha = $rgba_array['alpha'];
            //allocate the color to paint. Note that we may continue to 
overwright this color since our image is not palleted
            $color_to_paint = imagecolorallocatealpha($dest, $red, 
$green, $blue, $alpha);
            //paint the pixel
            imagesetpixel($dest, $j, $i, $color_to_paint);
        }
    }
    //this allows the new transparency info to be saved with the image
    imagesavealpha($dest, true);
    //write the image to the destination file
    imagepng($dest, $dest_path);
}
?>
-- 
Spoken: How much is that gorilla in the window?
Take our advice, At any price, A gorilla like Magilla is mighty nice.
Gorilla, Magilla Gorilla for sale