Now that we have the hard-coded version, we’ll break this up into placeholders ( using proper escaping for shell arguments, of course) and we’ll find ourselves with the full, variable-filled version we had before! Gotchas With these numbers at hand, we can ask Gifsicle to resize our image to 267×150: Given the height, we can calculate the width thusly: $src_width / $src_height * $dst_height, or 500 / 281 * 150, which gives us a width of 267px. Confused? Just remember that we have a 16:9 aspect ratio, meaning the height is the smaller dimension and thus cannot end up smaller than 150px or we risk our 150×150 frame not being filled. Now, let’s say we want a square version of that same image, at 150×150. In order to achieve this, we need to perform two operations: resizing the image to fit as much of it as possible into a 150×150 box, then crop off anything outside that area.įortunately, resizing the image is easy: Gifsicle has a -resize option, and the math that led us to our getimagesize()-compatible calculations up above would tell us that to fit a 500×281 image in a 150×150 box, our target dimensions would be 267×150. The original dimensions of this image are 500×281, approximately a 16:9 aspect ratio. Unfortunately, there's not a lot you can do about that, except to choose a more suitable background or to re-render the animation.Well, that was easy, wasn’t it? In case you’re not well-versed in juggling arguments and using PHP to assemble CLI commands, we’ll apply some real numbers: let’s say we have this gif of America’s Sweetheart, Nick Offerman: This is OK as long as the background you're showing the animation on isn't too far from the original background color, but if it is, you may find that there will be some ugly color fringing around the edges of the animation. You can't actually tell the difference by eye (or, at least, I can't do that on this screen), but it's enough to make GIFsicle consider the two colors different.Īlso note that GIF files only support 1-bit transparency, which means that the edges of the transparent regions will not be anti-aliased. very slightly yellow-greenish white) rather than #ffffff (pure white). For example, for the animation shown above, the actual background color turned out to be #fdfffc (i.e. One detail worth noting is that, if the GIFsicle command doesn't seem to have any effect, you should check that the background color is really correct. Original animation by Wikimedia Commons user WillowW, used under the CC-By 3.0 license. The -O2 option is not strictly necessary, but it's likely to shrink the file size of the resulting animation by optimizing the frames to avoid needlessly re-drawing static parts of the animation.)įor a demostration, here's an animation of the human glyoxalase I (GLO1) enzyme from Wikimedia Commons (left), and the same animation with transparency added using the method shown above (right): This allows extra transparency to be added to the frames without letting the earlier frames show through the transparent parts. (The important options here are -U / -unoptimize and -disposal=previous, which together convert the animation into "flipbook mode", where each frame is fully erased before drawing the next one. Where anim.gif and anim_trans.gif are the source and destination file names, and #ffffff is the hex code of the color you want to make transparent (here, pure white). You can do this with GIFsicle, using the following options: gifsicle -U -disposal=previous -transparent=" #ffffff" -O2 anim.gif > anim_trans.gif
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |