• # Solution

    Posté par . En réponse au message Créer une image png transparente. Évalué à 2.

    Bon, quelqu'un sur IRC (Calimero, alias Tipiak : http://linuxfr.org/~Cali_Mero/(...) ) m'a fourni une solution clés en main :)
    Je vous la fournis (et j'avoue que j'aurais jamais été capable de la trouver seul avec la documentation de PHP !)
    function modif_alpha($src , $dest , $alpha){
    /*
    * webmaster@creatixnet.com - licence bananière
    * ("faites de ce code ce que vous voulez...")
    *
    * Paramètres : $src : fichier source
    * $dest : fichier de destination
    * $alpha : transparence (entre 0 (opaque) et 127 (transparent))
    *
    * EX : modif_alpha('image.jpg', 'image.png', 70)
    * rend l'image à moitié transparente.
    */

    if (file_exists($src)) $size = getimagesize($src);
    else return FALSE;

    switch($size[2])
    {
    case 1 :
    if (imagetypes() & IMG_GIF)
    $pic = imagecreatefromgif($src);
    break;
    case 2 :
    if (imagetypes() & IMG_JPG)
    $pic = imagecreatefromjpeg($src);
    break;
    case 3 :
    if (imagetypes() & IMG_PNG)
    $pic = imagecreatefrompng($src);
    break;
    default :
    if (preg_match("/\.wbmp$/i", $src) && (imagetypes() & IMG_WBMP))
    $pic = imagecreatefromwbmp($src);
    }
    if (!$pic) return FALSE;


    $new=imagecreatetruecolor($size[0], $size[1]);
    imagealphablending($new, FALSE);
    imagesavealpha ($new, true);
    $colors_num = imagecolorstotal($pic);


    for($x=0;$x<$size[0];$x++) {
    for($y=0;$y<$size[1];$y++) {
    $colorat=imagecolorat($pic,$x,$y);
    $color = imagecolorsforindex($pic, $colorat);
    $r = $color["red"];
    $g = $color["green"];
    $b = $color["blue"];
    $idx=imagecolorallocatealpha($new,$r,$g,$b,$alpha);
    imagesetpixel($new,$x,$y,$idx);
    }
    }
    imagepng($new, $dest);
    return TRUE;
    }