syamilmj / Aqua-Resizer

Resize WordPress images on the fly
502 stars 208 forks source link

Added padding option and code. #58

Closed Grannath closed 4 years ago

Grannath commented 9 years ago

Hi,

short version: I've added a version that, instead of cropping, pads the image with a transparent background on both sides. This makes it necessary to convert to png. Padded versions have the additional suffix "-pad" and are searched for automatically.

Long version: This change was necessary for me because our Wordpress theme uses Aqua Resizer for everything, usually with $crop = true. This is really bad for galleries. But so is no cropping, if only one size is supplied. Padded images are always fully visible without any loss. The requesting script knows exactly which size will be returned. It is very useful for galeries, but also for post images, etc. No more worrying about aspect ratios. Don't loose any information due to unintended cropping. It should not result in any speed losses. No new code is used if not necessary. If padding is requested and necessary, a check for an existing version is done, just as before. The padded version is always .png for transparancy. It has a "-pad" suffix to distinguish it from cropped versions. If no existing pic is found, it is created. This might be a bit slower, but I don't expect any trouble. Both images used are destroyed, so no memory leaks expected either.

Before merging, please test this thoroughly. I do not have the setup to do so, just quickly hacked some code into a running Wordpress site. I don't even know PHP, just googled what I needed. :smile:

mxmzb commented 9 years ago

But why would you want to create an image cropped into a transparent box instead of doing that with CSS?

Grannath commented 9 years ago

Well, first off, you can resize with CSS. Not the point. Secondly, at least in the case of the page I'm administrating, I don't have a choice. I can either crop my gallery pics (stupid), have them resized to a fixed width (annoying for different aspect ratios), hack the script to don't crop despite the options in the theme (breaks the page because it expects pics with the requested aspect ratio), or rebuild the whole site using a different theme. Frankly, this is the preferred way. Thirdly, there are cases where it is convenient. Instead of dynamically creating CSS to position and/or resize the pic, you know exactly what you will get back from the resizer.

But most importantly, don't restrict your functionality because there are other ways to do the same. This is a pure addition that does not interfere with the old script. Tell me a strong reason why this should never, ever be used. Otherwise, feel free to suggest additions to the documentation with alternatives that can be better.

mxmzb commented 9 years ago

The script allows you to not specify the ratio, it is enough to pass either width OR height. By that, you can resize dynamically to a maximum width or height without losing parts of the image or disturb it. Afterwards you can use CSS styling (your third point doesn't really make any sense imho, CSS is thought for exactly that case, to position and style things) to get the exact result you are getting with your approach, with the difference that you don't have transparent borders anywhere, which may result in more dynamic in case, let's say, you want to change the layout of your galleries later (unless you work through all the images again with some image resizing script).