-
-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: twig filter to resize & fit - no cropping #1332
Comments
Hi @tomasvanrijsse thanks for this idea. Just to make sure you're aware, there's a filter called
This transforms an image like... into... With |
The expected output for images smaller then the requested limitations would be the original dimensions. That would make sense in the grammatical sense because the original image fits the given dimensions. And the website I'm currently developing has images with black backgrounds and images with white backgrounds. The result would look appropriate if only I would know what background color to apply. |
If I understand correctly, I think Timber needs in the first place a way to proportionally resize images so that they fit a certain width and height. There are a couple of things that are currently missing from the docs, but looking at the code it seems they might be possible already:
Assuming the things above hold true, you might already be able to resize an image proportionally to a certain width / height. What's left from your use-case is to not upscale smaller images, and I think this can be easily implemented through a new Twig filter that looks at the dimensions of the image and conditionally calls the Note: These are all based on my limited experience with Timber, so your mileage may vary. |
I tried
All images where changed to exactly match these dimensions by ignoring the original aspect ratio. |
Yes, you're right, I could see
But this still leaves out your use-case for the enlarge smaller behavior, so to not overload the |
Here's how you could do it with the current API: // functions.php
class MySite extends TimberSite {
function __construct() {
...
add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
...
}
function add_to_twig( $twig ) {
/* this is where you can add your own functions to twig */
$twig->addFilter('fit', new Twig_SimpleFilter('fit', array($this, 'fit_image')));
return $twig;
}
function fit_image($src, $w, $h = 0) {
// Instantiate TimberImage from $src so we have access to dimensions
$img = new TimberImage($src);
// If the image is smaller on both width and height, return original
if ($img->width() <= $w && $img->height() <= $h) {
return $src;
}
// Compute aspect ratio of target box
$aspect = $w / $h;
// Call proportional resize on width or height, depending on how the image's
// aspect ratio compares to the target box aspect ratio
if ($img->aspect() > $aspect) {
return Timber\ImageHelper::resize($src, $w);
} else {
return Timber\ImageHelper::resize($src, 0, $h);
}
}
} Note: Now, this is a very naive implementation (division by zero is not handled yet) provided just as an example, but it works. |
Hitting the same issue, I would propose an alternative implementation. Since the third parameter is already here to define a crop I think it should also allow a This way we keep the syntax "compatible" with the wordpress add_image_size() function. Taking the example given in the issue summary this would give us:
ps: a nice feature would be also to allow the same array syntax to define the crop position as the add_image_size function, would make the transition from wordpress even easier. |
One small benefit if implemented this way is that it is automatically compatible with timmy without having to change anything to it.
|
Here's a little workaround. It's not ideal, but works fine, if you don't want to add a custom filter to Timber.
|
I think the workarounds provided will do for now. As we want to keep Timber core minimal we are going to close this for now. If you want to make a PR for the 2.x branch, please feel free to do so. :) |
I'd like to resize my images without cropping them, just making them fit.
We got used to declaring image sizes in twig but seem to lack this feature.
For example like this:
although that might be a bit confusing to as an alternative:
The text was updated successfully, but these errors were encountered: