0

I have a pretty basic question. It may be very noob question, but it is bothering me a lot. In my theme I've added a custom image size like this:

function img_update() {
    if ( function_exists( 'add_image_size' ) ) {
        add_image_size( 'blog_feat_img', 720 );
        add_image_size( 'latest_posts_widget_feat_img', 400 );
        add_image_size( 'portfolio_widget_feat_img_1x', 400 );
        add_image_size( 'portfolio_widget_feat_img_2x', 800 );
        add_image_size( 'blog_body_img', 740 );
    }
}
add_action( 'after_setup_theme', 'img_update', 11 );

Now I used another function to add that in media dropdown list, like this:

function mytheme_image_size_names( $sizes ) {
    $sizes['blog_body_img'] = __( 'Blog Body Image', 'isaumya' );

    return $sizes;
}
add_filter( 'image_size_names_choose', 'mytheme_image_size_names', 11, 1 );

Everything fine but I'm having a wired issue. When Iupload an image of 1024x768 it should generate a 740x555 copy as I didn't mention the height, so it should take the proper height as per that width. But instead it is generating 650x488 version for blog_body_img. I have no idea why. I've also tried to pass 0 as height and then true as the crop parameter. Still it generates 650x488 version and not 740x555. Why?

Can anyone help me out with this?

16
  • Try add_image_size( 'blog_body_img', 740, 600, true ); just for debugging purpose. Dec 26, 2014 at 10:38
  • But I don't want to mention the height. I want to be automatically set as per image.
    – iSaumya
    Dec 26, 2014 at 10:50
  • If you don't want to set a height, why do you expecti that the generated image has a height of 555? How is the aspect ratio set?
    – cybmeta
    Dec 26, 2014 at 10:52
  • because I want it to the same aspect ratio. Now if the main image size is 1024x768 and if you scale it down to 740 keeping the same aspect ratio, the height automatically will be 555. Try to do it in photoshop. You will see.
    – iSaumya
    Dec 26, 2014 at 10:58
  • 1
    I've tested add_image_size( 'blog_body_img', 740 );, uploaded an image 1024x768 and I get a image size exactly of 740x555. Strange it is not working for you. Review your code and look for other possible add_image_size you may have.
    – cybmeta
    Dec 26, 2014 at 11:09

2 Answers 2

1

At last I've fixed the issue. In my parent theme, the dev have used the following code

if ( ! isset( $content_width ) )
{
    $content_width = 650;
}

which means if the value of $content_width has not set up yet then set it to 650. Now in my chield theme at the time of overwriting and mentioning image type I've just added few more lines like this:

function img_update() {
    global $content_width;

    if ( isset( $content_width ) )
    {
        $content_width = 740;
    }

    if ( function_exists( 'add_image_size' ) ) {
        add_image_size( 'blog_feat_img', 720 );
        add_image_size( 'latest_posts_widget_feat_img', 400 );
        add_image_size( 'portfolio_widget_feat_img_1x', 400 );
        add_image_size( 'portfolio_widget_feat_img_2x', 800 );
        add_image_size( 'blog_body_img', 740 );
    }
}
add_action( 'after_setup_theme', 'img_update', 11 );

This type I have checked if the value of $content_width has already been set. Which is virtuosos because my function in priority 11 so the parent theme func with first set the value then I overwrite it.

Every thing is fine now.

Previous

wrong image width

Now

everything is ok

1
0

Use '9999' as a height limit for the add_image_size function. Thus, you will have your ratio saved without cropping or squishing the image and only width limit will be followed.

Height is required and has no default value (see here: https://developer.wordpress.org/reference/functions/add_image_size/)

8
  • You are completely wrong. Width and height both are optional in the add_image_size() function. Default value is zeo (0). Read the Codex: codex.wordpress.org/Function_Reference/add_image_size
    – iSaumya
    Dec 26, 2014 at 11:10
  • Yes, you are wrong. The codex says it is optional, the reference says it is required. Only seeing the source code you can be confirmed that is optional with default value set to zero.
    – cybmeta
    Dec 26, 2014 at 11:11
  • Sorry, guys, but the code reference seemed like a more up-to-date source of info for me. Anyways, I can edit the "no default value" and "required" part, but am absolutely sure about the 9999 value, so do try it out, please.
    – vlood
    Dec 26, 2014 at 11:13
  • OK, I'm trying 9999
    – iSaumya
    Dec 26, 2014 at 11:15
  • 2
    See the source code: core.trac.wordpress.org/browser/tags/4.1/src/wp-includes/… That is the more up-to-date because it is the actual code working in WordPress. And I can confirm from my live tests that height is not required, see my comment on the question.
    – cybmeta
    Dec 26, 2014 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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