0

I've tried to add my custom image size like:

add_image_size('224_226', 224, 226, false);

I need resize all images to same size what i define in style, when i've uploaded file - Wordpress resize it to '224x174' but i need size 224x226.

I don't need a crop image.

Guys tell me what to do?

  • 1
    When WordPress create thumbnails of a uploaded image and one of the dimension (width or height) of resized image is smaller than your defined values for width and height respectively then WordPress will not magically make it larger but ignore that defined value. It's your task to upload appropriate size image. WordPress resize images keeping aspect ratio same. – Robert hue Oct 6 '15 at 8:48
  • If any of the answer was helpful to you, then consider accepting it. See »What should I do when someone answers my question?« and/or »Why is voting important?«, more information about the WordPress Development model is available at the help center. – Nicolai Nov 9 '15 at 11:54
1

If the last parameter $crop is true, then »images will be cropped to the specified dimensions using center positions«, with »(...) an array can specify positioning of the crop area« - this is generally called hard crop. But you have to make sure that the images are big enough to actually crop the size you want in the end. More information is available at the add_image_size() documentation.

Without having to crop you have to upload images with the correct X to Y ratio, because otherwise, in soft proportional crop mode, it will be resized, to either the X or Y value you have given in the add_image_size() call, whichever is met first. All of that is explained at the documentation for add_image_size().

  • Hi, thanks for your answer, but i don't need a crop, i need to resize image without proportionality – Mozz Oct 6 '15 at 8:41
  • How i can rewrite class or function what responsible for this work? – Mozz Oct 6 '15 at 8:57
  • 1
    Using server-side WP methods you can crop to specific dimensions or proportionally resize to first met dimension (width or height). As far I know, non-proportional resizing is not an option. But you can still us CSS to force a specific aspect ratio for your images, even if that means that images looks distorsioned. – cybmeta Oct 6 '15 at 9:04
  • 1
    Yea, disproportional images are a sure way to make your site look crappy.. @cybmeta – Nicolai Oct 6 '15 at 10:00
  • 1
    @Mozz I imagine you could probably hook into wp_handle_upload_prefilter handle it there in one way or another, but that results in disproportional images, which you really shouldn't do. – Nicolai Oct 6 '15 at 10:02
1

There're no way to achieve what you want in native WordPress way. You have to use both add_image_size() WordPress function and CSS to create resized thumbnail version of an image. @ialocin already explained what add_image_size() does, so not repeating it.

First, you need to set a specific width, a very high value for height and set the hard crop mode to false. Then set the dimension of the thumbnail using CSS. For example:

add_image_size('224_226', 224, 9999, false);  

That will set the thumbnail width to 224px and stretch the height to maximum keeping the current proportion. Then add a class while calling the thumbnail..

the_post_thumbnail( '224_226', array( 'class' => 'image-224-226' ) );  

Finally, set the height and width using the CSS..

.image-224-226 {
    height: 226px;
    width: 224px;
}  

You need to make sure that the height of uploaded image is bigger than the width, otherwise the thumbnail will have a shorter height but the CSS will stretch it to fit and you might get a distort image. The same applied for wider images too.

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.