+ Reply to Thread
Results 1 to 4 of 4

Thread: [PHP] Losing alpha transparency

  1. #1
    echo8era is offline x10Hosting Member echo8era is an unknown quantity at this point
    Join Date
    May 2009
    Location
    Victoria, Australia, Terra, Sol System, Milkyway Galaxy, Local Group...
    Posts
    3

    Question [PHP] Losing alpha transparency

    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 Code:
      $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 by echo8era; 09-25-2010 at 04:57 AM. Reason: Images Removed

  2. #2
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: [PHP] Losing alpha transparency

    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.)
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  3. #3
    echo8era is offline x10Hosting Member echo8era is an unknown quantity at this point
    Join Date
    May 2009
    Location
    Victoria, Australia, Terra, Sol System, Milkyway Galaxy, Local Group...
    Posts
    3

    Re: [PHP] Losing alpha transparency

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

    Quote Originally Posted by essellar View Post
    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.)
    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. #4
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: [PHP] Losing alpha transparency

    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.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. I am losing it..
    By jaco_don in forum Off Topic
    Replies: 32
    Last Post: 09-01-2008, 02:25 AM
  2. transparency for 300 c
    By XGI Admin in forum The Marketplace
    Replies: 8
    Last Post: 06-07-2008, 03:13 PM
  3. PNG transparency in IE 6...
    By galaxyAbstractor in forum Programming Help
    Replies: 3
    Last Post: 04-25-2008, 05:11 PM
  4. CSS Transparency
    By Zenax in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 02-16-2007, 05:49 AM
  5. Im losing patience...
    By amr1991 in forum Free Hosting
    Replies: 15
    Last Post: 09-08-2006, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers