[PHP] Losing alpha transparency

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by echo8era, Sep 25, 2010.

  1. echo8era

    echo8era New Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I can't seem to keep semi-transparent areas of my images semi-transparent. Unless I'm misunderstanding the documentation for imagealphablending() and imagesavealpha(), this should be working:
    PHP:
      $BG imagecreatefrompng('BackgroundImages/MrsMitch1.png');
      
    $Width imagesx($BG);
      
    $Height imagesy($BG);
     
      
    $Image imagecreatetruecolor($Width$Height);
      
    imagealphablending($Imagefalse);
      
    imagesavealpha($Imagetrue);
     
      
    imagecopymerge($Image$BG0000$Width$Height100);
      
    // Code to draw on $Image here
     
      // Cache it to reduce need to redraw image each request
      
    imagepng($BG'Cache/MrsMitch.png'0);
      
    imagedestroy($Image);
      
    imagedestroy($BG);
     
    Last edited: Sep 25, 2010
  2. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    It looks like you need to explicitly call imagesavealpha($BG) as well, doesn't it? You only have single-bit transparency on the copy of the image you merged into the final image. Remember that the image object you created from the original PNG isn't a PNG anymore, it's an image object. (Yes, it may be reasonable to expect that it would inherit the "include entire alpha channel" info from the PNG. Your experiment proves that it won't export with the alpha channel unless you force it to.)
     
    • Like Like x 1
  3. echo8era

    echo8era New Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Whoops, one of the lines in the original example should have been
    PHP:
      //Cache it to reduce need to redraw image each request
      
    imagepng($Image'Cache/MrsMitch.png'0);
    Teaches me to proofread after simplifying example code.

    What do you know, that works. Thanks.

    Aarrgghh!!! I kind of wish ASP.Net worked - at least I know what I'm doing there instead of the guessing game my attempt at coding in PHP currently is.
     
  4. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    I feel your pain -- PHP is kind of new to me as well*, although I have Gandalf skillz on platforms I can't afford to use for personal projects. Cruising forums like this helps. For some reason, other people's problems are less frustrating than my own (probably because there's no cost to me, either monetary or in terms of personal pride, if I can't figure it out). I wouldn't have known about the imagesavealpha() thing if I hadn't read your question and tried to figure it out, but now I have a tool I didn't have in the box a couple of days ago.

    *I did use it back when it was still an abbreviation for "Personal Home Page Tools", Rasmus was still in the Toronto area, and it wasn't much good for anything but forms processing. Things have changed just a little bit since then.
     

Share This Page