1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Transparent gif images show black background when resized

Discussion in 'PHP' started by qwikad.com, Jan 13, 2020.

  1. #1
    Gif images with transparent backgrounds end up having black backgrounds and animated gif images turn black completely. Any ideas why?

    
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromgif($filePath);
    imagealphablending($image_p,true);
    imagesavealpha($image_p,true);
    $transparent = imagecolorallocatealpha($image_p,0,0,0,127);
    imagefill($image_p,0,0, $transparent);
    imagecopyresampled($image_p, $filePath,0,0,0,0, $width, $height,
    $width_orig, $height_orig);
    $upload = imagegif($image_p, $filePath);
    
    Code (markup):

     
    qwikad.com, Jan 13, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    You're declaring black with max opacity, which is backwards. It could also indicate you're on an outdated version of GD.

    Also image fill seems to often drop the ball on setting alpha, damned if I know why.. and you want imagealphablending to be FALSE, not true. Yes, that seems like it should be backwards.

    
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromgif($filePath);
    imagealphablending($image_p, false);
    imagesavealpha($image_p, true);
    $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
    imagefilledrectangle($result, 0, 0, $width, $height, $transparent);
    imagecopyresampled($image_p, $filePath, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    $upload = imagegif($image_p, $filePath);
    Code (markup):
    SHOULD fix you right up.
     
    deathshadow, Jan 13, 2020 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    @deathshadow It fixed one thing: animated gifs are not totally black now, just the background. But no, it still shows black color instead of transparency on gif images. Like so:

    a1.png

    a2.png
     
    qwikad.com, Jan 13, 2020 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Well, at that point it could just come down to GIF being a 33 year old format that's fallen into near total disuse. Part of why I tend to put code alongside any uploads now to convert them to webp as really at this point -- unless you're batshit obsessed with cutting out all artifacts -- there's no reason on deployment to still be using anything other than webp/webm.

    I mean you might as well be using BMP or PCX.
     
    deathshadow, Jan 13, 2020 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Try removing that "imagealphablending" line, and in your "imagecopyresampled", the second variable has to be an image resource.
    You are giving a filePath...

    imagecopyresampled($image_p, $filePath,0,0,0,0, $width, $height, $width_orig, $height_orig);

    That should be:
    imagecopyresampled($image_p, $image,0,0,0,0, $width, $height,
    $width_orig, $height_orig);
     
    JEET, Jan 13, 2020 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Side note, reminds me of something stupid I had a client ask about ~25 or so years ago. They said there was something wrong with their printer, because when they printed out GIF files they weren't animated anymore.
     
    deathshadow, Jan 16, 2020 IP
    Saputnik and JEET like this.
  7. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    JEET, Jan 17, 2020 IP
  8. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #8
    :)
    -----------
    I've scrapped the entire idea (resize / rotate images on upload) for now. It consumes a ridiculous amount of php memory for large images. I can, of course, throw a "this image is too large" error but I'd rather not.
     
    qwikad.com, Jan 17, 2020 IP
    JEET likes this.
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Always a risk when using GD.

    Q: Could you get away with having such rotations done client-side? transforms thanks to most browsers leveraging the GPU now can work wonders.
     
    deathshadow, Jan 17, 2020 IP
    JEET likes this.
  10. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #10
    @qwikad.com
    How did you measured memory during processing?
    Thanks
     
    JEET, Jan 17, 2020 IP
  11. gorrior

    gorrior Member

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #11
    Lol no clue. But animated images that are blank is kind of ironically funny ^^
     
    gorrior, Jan 17, 2020 IP
  12. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #12
    @deathshadow
    Where is that button using which we used to give negative red reps to members who spammed threads?
     
    JEET, Jan 17, 2020 IP
  13. gorrior

    gorrior Member

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #13
    Sorry, I didn't mean to spam. I was bored and had nothing to do. I will be more careful from now on.
     
    gorrior, Jan 17, 2020 IP
    JEET likes this.
  14. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #14
    @gorrior
    No need to say sorry, its ok.
    Even I used to do this when I joined this forum. Then some other members pointed it out and I stopped. Then I realized the full potential of this forum, how useful it can be!
    10 years later, I am still here, still learning from so many other awesome members!

    Don't worry, I am not going to give you any negative reps...
     
    JEET, Jan 17, 2020 IP
  15. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #15
    I didn't. As I was testing the script uploading large images (3000 x 3000, etc.) the upload page would go blank. I could, of course, increase php memory limit, but my concern is if multiple users simultaneously upload large images (which can happen) they'll still end up with blank pages or failed uploads.
     
    qwikad.com, Jan 18, 2020 IP
  16. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #16
    I think memory_limit is not shared between sessions. The memory_limit you will set will work for individuals, for each individual process. So if memory_limit has 512M as value, then each user gets that much memory limit.

    From what I have read, PHP starts with a minimum allocation of 8MB, and keeps increasing the limit internally if process needs more, until specified memory_limit threshold is reached.
    This is for each individual process.

    Not all members will upload 3000x3000 images, so most will not need that much memory anyways.
    Best idea is to complete the process as quickly as possible and free the RAM for other processes.
     
    JEET, Jan 18, 2020 IP
    qwikad.com likes this.