Resizing Animated GIFs Without CGO or Third-Party Libraries

GIF animation icons enable users to express themselves much better. Resizing animated GIFs is necessary to support them.
What would you use when implementing GIF resizing in your Go web application? ImageMagick as we did? Another CLI? Or a C library wrapper?
There is little knowledge of GIF resizing in Pure Go. There are articles about basic image resizing, but they don't cover animated GIF-specific processing. Therefore, popular methods to implement it are using CLIs or C library wrappers.
However, those invoke several unignorable issues. Calling CLIs directly from the code causes security concerns. Image-processing CLIs and C libraries are typically large files that don't fit into a container environment. Also, they cause performance overheads that affect app latency.
Stop using them by implementing GIF resizing with pure Go! It requires some specific knowledge, but it's so simple that we can implement it with only standard libraries and sub-repositories (golang.org/x libraries)!
Learn how to implement GIF resizing with pure Go step by step and the cautionary points you may face during the implementation.

LEVEL: Advanced

Abstract

- Why we needed to implement GIF resizing in pure Go (5 min)
-- Self Introduction
-- Introducing traQ (https://github.com/traPtitech/traQ), a web application I had maintained
-- Explain why we wanted to get rid of ImageMagick
--- Wanted to replace base image with distroless due to security concerns, but blocked by ImageMagick library installation
--- Docker image size was large (over 600MiB), but we don't utilize most of the library
- (1) Basic resizing implementation (5 min)
-- Image resizing with the `golang.org/x/image/draw` package
-- Basic structure of `*(image/gif).GIF` struct
-- Implementation: https://github.com/logica0419/proposals/blob/main/GopherCon2024/step1.go
- (2) Handling frame optimization (5 min)
-- What is frame optimization?
-- How frame optimization affects the`*(image/gif).GIF` struct
--- Each frame is trimmed
--- Frame size differs in each frame
-- Calculating frame size for each frame
--- Calculate the resize ratio for width & height
--- Calculate the resized frame size for each frame
-- Implementation: https://github.com/logica0419/proposals/blob/main/GopherCon2024/step2.go
- (3) Handling black glitchy noise (10 min)
-- Simple explanation of image resizing algorithm and its implementation in Go
-- Problem of transparent pixel handling
-- Frame stacking before resizing
-- Implementation: https://github.com/logica0419/proposals/blob/main/GopherCon2024/step3.go
- (4) Handling frame disposal (10 min)
-- GIF frame disposal specification
--- None: keep stacked frame to the next frame
--- Background: fill the canvas with background color after the current frame rendering
--- Previous: put back canvas to the previous condition
-- Handling frame disposal in Go
--- After-resize processing for the temporary canvas
-- Implementation: https://github.com/logica0419/proposals/blob/main/GopherCon2024/step4.go
- Conclusion (10 min)
-- Summary of the implementation
-- A little explanation of parallelization of resizing
--- The resizing step is the slowest
--- Frame stacking can't be parallelized (must be in order)
--- Wrapping only resizing step with goroutine
-- Effect on our app
--- Docker image shrinks down (40~50% in size)
--- Able to move on to distroless
--- Performance improvement (x3 faster in same CPU usage)
--- Able to select the resizing kernel
-- Introducing resigif (https://github.com/logica0419/resigif) library as a result of work.

GoLab is a conference made by Develer.
Develer is a company based in Campi Bisenzio, near Florence. Our motto is : "Technology to give life to your products". We produce hardware and software to create exceptional products and to improve industrial processes and people's well being.
In Develer we have passion for the new technologies and we offer our clients effective solutions that are also efficient, simple and safe for the end users. We also believe in a friendly and welcoming environment where anybody can give their contribution. This passion and this vision are what we've been driven to organize our conference "made by developers for developers".


Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2024 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS