0

I am using the following line of code to create a new image size:

add_image_size('single-post-image', 600, 370, true);

However, this line of code does not guarantee that, the image will be of 600X370 size, it guarantees that, this is the maximum size, and smaller images will not be resized.

In my case however, I need a guarantee that, every image I use will be of 600X370.

0

The answer to you question can be found here: https://stackoverflow.com/questions/10722466/how-do-i-force-wordpress-thumbnails-to-stretch-to-fit-if-uploaded-image-has-smal

Add this code to your functions.php to strip the hardcoded width and height of the images that are integrated inside Wordpress:

add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );
add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 );

function remove_thumbnail_dimensions( $html ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

and you will need to add the following line to your active theme's style.css to stretch the image:

.attachment img {
    width: 100%; /* scale to width of container, or whatever you want */
}

Just make sure you are using the correct selector and width / height in your CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.