4 ms·
I've probably done this ten times in ten different ways over the years. Most recently with ~150 lines of Go via nginx proxy_pass (or fastcgi_pass). I needed to
by staunch 13y ago
I've probably done this ten times in ten different ways over the years. Most recently with ~150 lines of Go via nginx proxy_pass (or fastcgi_pass). I needed to be able to control how to resize images while maintaining aspect ratio on a fixed size canvas. The first request for an image is disappointingly slow for larger images, but it's not terrible. Writing out the generated images to a cache directory and using try_files, so subsequent requests are static, is definitely key.
- hierro 13y agoGo's image module is painfully slow, supports a limited set of formats and fails on lots of optimized images. I also have implemented this in Go, but using cgo and graphicksmagicks, which is way faster and decodes almost any image you throw at it (there are some issues with very optimized GIFs, but I fallback to gifsoup for "deoptimizing" them in those cases). In fact, I even added a function for cropping and resizing an image to a given size in the module itself (since I think is a very common need), while keeping the aspect ratio and also giving the option to just center the result or grab the part of the image with higher entropy (e.g. suppose you have an image with a person in the side and then a lot of blue sky, you probably don't want to crop and image and end up with just sky in the thumbnail). This is just one of a few benchmarks I wrote: BenchmarkResizePngMagick 20 80665091 ns/op 689 B/op 3 allocs/op BenchmarkResizePngNative 1 9689016519 ns/op 351200 B/op 27 allocs/op (yup, that's 120x faster) The bindings are mostly documented, but I haven't gotten around to releasing the code yet, although I do hope do publish it soon. If you're interested, send me an email and I'll let you know when I put the code in Github.
- staunch 13y agoSent you an email. Thanks. I actually kind of like using the native Go libraries, because they're so simple I feel like I can actually trust them. But, you're right, they're really slow and strict in what they parse. I'll probably need to do something different in the future, so your code would certainly be interesting to check out.
- simonz05 13y agoSent an email as well.