GD & Transparent Gifs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devonknows
    New Member
    • Nov 2006
    • 137

    GD & Transparent Gifs

    Ok ive been wracking my brains over this for absolutely ages and cant find a simple answer anywere not even on the php.net site so i was wondering if you could help me.

    I have designed a gif file in photoshop with transparent backgrounds, but when i use
    [PHP]$im = imagecreatefrom gif($imgname)[/PHP]
    the transparent images are showing up as white and i cant seem to figure out how to keep the transparent background within the new image.

    Surrounding Code
    [PHP]
    $im = imagecreatefrom gif($imgname);

    $imsize = getimagesize($i mgname);
    $colorTranspare nt = imagecolortrans parent($im);
    $im2 = imagecreate($im size['0'],$imsize['1']);

    imagepalettecop y($im2,$im);
    imagefill($im2, 0,0,$colorTrans parent);
    imagecolortrans parent($im2, $colorTranspare nt);
    imagecopyresize d($im2,$im,0,0, 0,0,$imsize['0'],$imsize['1'], $imsize['0'], $imsize['1']);
    [/PHP]

    I would be most appreciative if someone can help me with this cus ive been looking for hours and cant find anything.

    Kind Regards
    Devon
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    The image, do you display it in a browser? In that case what browser?

    I've seen similar cases where the transparent area goes black or white.

    /MK

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      I remember testing this, and the only way I could keep the transparency was to set the transparent color (via imagecolortrans parent) of the new image to black.

      Seeing as your backgrounds are showing white, maybe this should be white in your case.

      Trying to get the transparent color of the original image (like you do) never worked.

      This worked for me:
      [code=php]
      <?php
      // Create original container
      $original = imagecreatefrom gif($pathToOrig inal);

      // Calculate new size
      $imgSize = getimagesize($p athToOriginal);
      $newx = $imgSize[0] * 0.25;
      $newy = $imgSize[1] * 0.25;

      // Create copy container
      $copy = imagecreate($ne wx, $newy);

      // Set transparent as black
      $cBlack = imagecoloralloc ate($copy, 0, 0, 0);
      imagecolortrans parent($copy, $cBlack);

      // Copy
      imagecopyresamp led($copy, $original, 0, 0, 0, 0, $newx, $newy, $imgSize[0], $imgSize[1]);
      ?>
      [/code]
      I never used it because it didn't seem reliable, but under controlled circumstances it might work.

      Comment

      Working...