Feature request: an option to fit, not crop images for thumbnails
Currently, when generating thumbnails with aspect ratio that doesn't match source files, images are cropped. So for example, resizing USA flag to square image will loose some of stars in top-left corner.
What I would like to suggest is an option to "fit" instead of "crop" - so if you resize a 1000x500 image to fit into 100x100 frame - it gets resized to 100x50.
Reason is that I'd like to make a simple "gallery" app where user(s) can upload images from camera (~ 12 megapixel) which then are downscaled to several resolutions to be presented on screen. I wonder if it's possible to do all image resizing in pocketbase. Naturally, cropping user-uploaded image is undesirable in this case. :)
API-wise, maybe it's possible to do this via adding an extra letter at the end of the "thumb" query parameter. So ?thumb=100x100 will crop image, as now, and ?thumb=100x100f (note extra f at the end) will fit.
This could be implemented, but I'm not sure about the suggested change to the thumb parameter.
My proposal is to add a new query parameter resize that for now will support cropTop, cropCenter, cropBottom, fit.
Suggestions for different implementation or naming are welcomed.
I'll do some research later to see what other image manipulation apis are using.
thanks! Regarding query parameters - I don't mind either way.
It's just that currently admin can limit what thumb sizes can be requested, generated and stored. While with extra parameters - you will either have to specify allowed values of these parameters in the settings (i.e. "100x100 with cropCenter", "1000x1000 with fit"), or for one thumb size a malicious user will be able to generate multiple thumbnail files - with each possible value of "resize" option. Some might consider it a vulnerability.
@ganigeorgiev : if there's not much interest from other users, feel free to decrease priority on this! :-)
I managed to build pocketbase myself (it's very easy!), and even to build it for arm (32-bit) architecture (just by changing 2 env variables!!), so now it runs on my router!!! :exploding_head:
And yes, code of pocketbase and used libraries is pretty easy to follow even for someone who has no prior experience with go.
I won't show you the code because it's very bad (it won't pass even my own code review), but it works for me!
@Lex-2008 This feature would be nice to have (I'll need it also for Presentator) and it shouldn't require too much code changes (internally PocketBase is using the disintegration/imaging package that already supports the necessary image transformations).
We just need to agree on the format, but I still haven't gotten the time to review what other apis are using. About the vulnerability concerns - It's a good point, but I think it is OK in this case even if we don't validate them because the number of possible values are finite.
I've decided to implement @Lex-2008 suggested format style:
- WxH (eg. 100x50) - crop to WxH viewbox (from center)
- WxHt (eg. 100x50t) - crop to WxH viewbox (from top)
- WxHb (eg. 100x50b) - crop to WxH viewbox (from bottom)
- WxHf (eg. 100x50f) - fit inside a WxH viewbox (without cropping)
- 0xH (eg. 0x50) - resize to H height preserving the aspect ratio
- Wx0 (eg. 100x0) - resize to W width preserving the aspect ratio
For now it is only in master. Will be included in the next feature release (v0.5.0).