add_image_size() using ratio instead of sizes - WordPress - wordpress

I'm developing a WordPress theme, and I need images that are uploaded via the inbuilt Media Uploader, to be the same ratio. I know this is easily done by defining the height and width and using crop.
Problem is that I want to use these images for a fullscreen slider, so having all images down to 300x200 as an example wouldn't work. I want to keep as much as the default size of the image as possible while cropping it for the ratio.
WordPress comes with a add_image_size() feature, but I'm more like looking for a add_image_ratio() feature. Is this even possible?

You can use a ratio:
$thubmnail_ratio = (150 / 200); // height to width ratio, you can use some existing working ratio
add_image_size( 'thumbnail-small', 100, round(100*$thubmnail_ratio), true );
add_image_size( 'thumbnail-medium', 200, round(200*$thubmnail_ratio), true );
add_image_size( 'thumbnail-large', 400, round(400*$thubmnail_ratio), true );
It should also be possible writing a little helper function that wraps add_image_size(...).

Related

add_image_size with exact width + auto height, but the resized width is limited to a maximum value

I need a little help with resizing images in Wordpress. I want to set exact width of 956px to an image and floating height. I have tried this:
add_image_size('full_widthn', 956, 9999, TRUE);
add_image_size('full_widthn2', 956, 9999, false);
add_image_size('full_widthn3', 956, 9999);
add_image_size('full_widthn4', 956, 100, false);
add_image_size('full_widthn5', 956);
But the result of this code is this. (only 700px width)
The original resolution of the uploaded image is 1070px. Where could be a problem, please?
Fixed Width:
Somewhere in your theme, most likely in functions.php, $content_width is set to 700.
$content_width is a global variable used in themes to set the maximum allowed width for any content in that theme.
Your images are getting resized on upload to the size you specify, but the options you are shown in the Media Editor are adjusted to fit a maximum width that's limited by $content_width in your theme.
From the Wordpress Codex:
Notes: This variable is also used when displaying a pop-up selection in the Media Editor. If you have used the global $_wp_additional_image_sizes to allow a pop-up of your custom image sizes defined using add_image_size() then the $content_width will override the width specified by your add_image_size() function.
Auto height:
In relation to not having a fixed width height, Any of the following from your examples will result in a fixed width of 956px (unless the uploaded image is smaller) and auto height:
add_image_size('full_widthn2', 956, 9999, false);
add_image_size('full_widthn3', 956, 9999);
add_image_size('full_widthn5', 956, 0);
add_image_size('full_widthn5', 956);

Posts images resized

This has been frustrating me for a while- I'm working on a test site- www.temp.lpwgroup.co.uk.gridhosted.co.uk and I want the featured images to be 700px x 200px but on the posts page- /news They are being added in as 600x400 for some reason, linking to a new image url with that at the end of the one i upload. Can anyone let me know if there's additional css I can add so the full image shows in that 7:2 ratio?
Image issue
Thanks,
Joe
seems like you are using Wordpress in your website, You need to add a new thumbnail size in your functions.php
add_action( 'after_setup_theme', 'setup' );
function setup() {
add_image_size( 'size-portfolio', 700, 200, true ); // 400 pixel wide and 200 pixel tall, cropped
}
then use regenerate thumbnails plugin to regenerate your thumbnails

Featured Image Length Not Uniform

I have looked up in all my css of theme i am using on my wordpress blog, but problem is when i use featured image on homepage it is not all in same length. how i can make them stretched to fix in unifrom container?
can anyone tell me specific piece of code where i can fix this length?
i saw this line on functions.php in themes.
add_image_size('maggie-lite-featured-post', 1170, 350, true); //featured post img size
right now you're telling WP to hardcrop, so it's max 1170px width OR 350px height.
To solve this you shoud go for:
add_image_size( 'maggie-lite-featured-post', 1170, 9999 );
So you tell WP to make it 1170width - the height doesn't matter anymore.
Otherwise you could try to remove the "true" trag from the Crop, but I am not sure if this will also help. But def one of both should be your solution.
add_image_size('maggie-lite-featured-post', 1170, 350, false); //featured post img size
All the best

Image Object in Advanced Custom Fields is not resizing properly

We created a custom theme using ACF for Wordpress. Our issue is that we are resizing a single image 1440 x 320 to fit in 2 places. The first place which is working fine is Full post leadspace image which uses the full 1440 x 320 image. The issue we are having is for the Featured post view or Card layout the image needs to resize down to 380 w x 160 H. We are using Advanced Custom fields to add the image to the post. I will attach a photo of whats happening
I guess the problem isn't ACF, but your add_image_size() function.
If you already defined the image size in your functions.php, it should look like this:
add_image_size ( "card", 380, 160, true );
The last argument (true), tells WordPress to crop the image. If this is set to false, the resized image will be 380px wide and the height will be proportionally. If you crop the image, WordPress will first resize the image, and then cut the image to 380x160px

Wordpress Featured image resize and crop not working

I am having trouble calling the featured image of the blog post in my wordpress blog. I have searched websites for the tutorial but it all leads to the same results.
It doesn't follow my assigned size which is 200x180px then the rest is cropped. to have a better view of what i'm trying to do, you can refer to this website:
http://freakify.com/
I am trying to have all the featured images in same sizes (200x180). on a very related topic, the sizes of my featured images is different but not lower than 500px and not more than 650px. I am trying to display it in different places such as my index file and side bar.php file
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 200, 180, true ); // 50 pixels wide by 50 pixels tall, crop mode
add_image_size( 'post-thumbnails', 200, 180 );
I hope you can help me guys.
First, you dont need the add_image_size if you're going to use only a one thumbnail image size, only with the first two lines es enough.
Second, with the 'true' statement you are 'telling' wordpress to hard crop the images, so it just cuts down the image to make them 200x180. On the other way, if you change the 'true' to 'false', then your images will be resized to the maximum width or height they can reach maintaining the aspect ratio.
This are the two methods for resizing the images without changing their appearence, because if you don't mantain the aspect ratio, you'll have stretched images.
My recommendation is to use the 200px as an absolute value, and change the height dynamically.
To achieve this you can do this:
set_post_thumbnail_size( 200, 9999, false );
This way you can have your original images, filling the 200px requirements but not changing their height, so you could preserve the aspect ratio.
Look at http://dfine.me to see this in action, you just need to modify the css to get the items in a proper way.

Resources