Skip to content
Advertisement

WordPress image size not sizing correctly

In WordPress admin, I have defined the size for medium as such:

enter image description here

I have then uploaded an image and defined in my code to use medium

$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );

However, the image size is being rendered at 370 x 186px. With the original size of the image being 1200 x 600. Seems like the width settings are being applied, but not the height?

Advertisement

Answer

WordPress is maintaining the aspect ratio for the crop. 1200×600 (which is a ratio of 2:1) is downsized to 370×185 (ratio 2:1), the latter of which is rounded up to 186.

If you look at the thumbnail version there’s a checkbox to crop the image exactly, however WordPress doesn’t do that for the default image sizes because it is an unexpected experience. If you do want it to crop, you can manually register your own size using add_image_size and pass true for the last parameter which is crop

add_action(
    'after_setup_theme',
    static function() {
        add_image_size( 'custom-medium', 370, 325, true );
    }
);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement