Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently if a user uploads a transparent gif, when Drupal resizes it, it makes all those transparent spots black, which is not desirably at all. You can imagine the users frustration. This problem has bugging me for a long time but I finally came up with a solution that is elegant and fixes it in core, where the root of the problem lies.
See the before and after screenshots for this patch in action. Test it with transparent gifs, adding them to your user profile as your user image.
Comment | File | Size | Author |
---|---|---|---|
#14 | drupal_image_gifs5.patch | 2.04 KB | quicksketch |
#11 | drupal_image_gifs.patch | 2.04 KB | quicksketch |
#6 | after-flower.png | 39.48 KB | m3avrck |
#6 | t.patch | 1.77 KB | m3avrck |
#5 | Before resizing | 61.02 KB | quicksketch |
Comments
Comment #1
m3avrck CreditAttribution: m3avrck commentedThis should be backported to Drupal 5 as well.
Comment #2
m3avrck CreditAttribution: m3avrck commentedThis should be backported to Drupal 5 as wel.
Comment #3
quicksketchI'd *love* to get this fixed! Pngs have worked fine for some time because of the special case made for them when transparency exists. We should do the same for gifs.
However, this patch produced some unexpected behavior and I couldn't get it to work with the images I was trying. The most peculiar was when uploading an image with an index color marked as transparent that wasn't black, such as the drupalicon3.gif attached shows up with no transparency and white spaces filled with black.
Comment #4
m3avrck CreditAttribution: m3avrck commentedUpdated patch now works with both types of transparent gifs.
Comment #5
quicksketchThis patch definitely makes headway. I'm now getting mostly transparent gifs but looks like the resizing is creating some distortion in the image somehow. Uploading a transparent GIF ends up with most of the originally transparent area trasparent but some other places filled with black. See the attached images.
Minor note: we should use
elseif
instead ofelse if
.Comment #6
m3avrck CreditAttribution: m3avrck commentedHmm, you sure you applied the correct patch? I just tried it with your image and got the following (see attached). Seems to be working well over here.
Info...
PHP 5.2.0
GD 2.0.28
Updated coding style patch.
Comment #7
quicksketchI tried using the patch in #6 on a fresh D6, the problem is still occurring.
GD bundled (2.0.34 compatible)
PHP 5.2.3
Curiously, I got the gif-drupalicon3.gif to resize without the distortion if I set the user size dimensions to 200x200 (exactly 50% of the original). The default 85x85, as well as 100x100 and 300x300 all result in the distortion problem.
I haven't been able to make the flower image to resize without the unexpected black dots.
Comment #8
m3avrck CreditAttribution: m3avrck commentedOk here's an updated patch which fixes those issues.
After some research, it turns out those odd colors were showing up because of mixing TrueColor GIFs and regular palette ones and then trying to draw a transparent background.
The magic line is:
This seems to solve those issues. However, some resized GIFs look a bit granulated because of it. There is no way around this unfort, based on the current GD library.
However, this is far better than a really bad black GIF image and is quite tolerable. I'm just being picky designer here :)
Comment #9
m3avrck CreditAttribution: m3avrck commentedIt seems attachments aren't working on Drupal right now, here is the patch, make yer own ;-)
Comment #10
m3avrck CreditAttribution: m3avrck commentedComment #11
quicksketchAwesome! This last patch works great with all the images I tested. This is a great fix that is long overdue for proper GIF scaling.
I rolled an actual patch which should be the same as the one posted above. m3avrck could you confirm I didn't make any mistakes and RTBC?
Comment #12
m3avrck CreditAttribution: m3avrck commentedConfirmed, glad to see attachments are working again, thanks for creating that file!
This should be applied to 5.x after 6.x too.
Comment #13
Gábor HojtsyCommitted to 6.x, thanks.
Comment #14
quicksketchSame patch for Drupal 5's image.inc file, before it was split into image.inc and image.gd.inc. Tested and confirmed the same fix works perfectly in Drupal 5. Could we get another RTBC?
Comment #15
m3avrck CreditAttribution: m3avrck commentedYup looks good. Patch is exactly the same as for 6.x but just a different file. Works the same so we're good to go, rock!
Comment #16
mokargas CreditAttribution: mokargas commentedI'm trying to apply this patch to 5.1, image.inc and get this message
I'm not terribly clear on patch for windows, so can someone shed light on why this is occuring?
Comment #17
drummCOmmitted to 5.x.
Comment #18
(not verified) CreditAttribution: commentedAutomatically closed -- issue fixed for two weeks with no activity.