• I am trying to set “width” / “height” in the function add_image_size() using the filter image_resize_dimensions which is working good so far but I have no idea how to update image attributes (new size and height ) after the resize

    Here is my code:

    add_action( 'after_setup_theme', 'set_custom_image_size', 20 );
    function set_custom_image_size() {
        add_image_size( 'normal', 101, 102 );
    }
     
    add_filter( 'image_resize_dimensions', 'normal_image_resize_dimensions', 11, 6 );
    function normal_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
        if( $dest_w === 101 ){ //if normal image size
            $width = $orig_w / 2;
            $height = $orig_h / 2;
            return array( 0, 0, 0, 0, $width, $height, $orig_w, $orig_h );
        } else { //do not use the filter
            return $payload;
        }
    }
    • This topic was modified 7 years, 6 months ago by KD.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve used the same technique in my themes and the image sizes in meta data and image output attributes are correct without any additional code. This only affects new uploads. Existing images are not updated. You need a plugin like “regenerate thumbnails” to update existing images.

    Thread Starter KD

    (@kkdevta)

    Thanks for the response bcworkz,

    Actually I checked the database and has correct updated values stored. however, when I use the_post_thumbnail(‘normal’), it pulls up the size either as 101 height or 102 width (maintaining the aspect ratio). Why does this happen?

    Right now I am using wp_get_attachment_metadata() which gets me the correct sizes along with wp_get_attachment_image_src()

    ( I am building a theme and do regenerate thumbnails but the issue persists even on the images uploaded after)

    • This reply was modified 7 years, 6 months ago by KD.
    Moderator bcworkz

    (@bcworkz)

    Interesting, I hadn’t been using my on-the-fly sizing for featured images so I hadn’t run into code getting the original sizing before. It appears to be because the_post_thumbnail() ends up using image_constrain_size_for_editor(), which under certain conditions uses the original sizing set with add_image_size() instead of the correct sizing in post meta. This can be overridden with the ‘editor_max_image_size’ filter, but unless you need your code to work with existing post_thumbnail code, I think you are better off with what you have by getting sizing from post meta.

    If you need to utilize the featured image of a post, you can get always get the featured image’s attachment ID in post meta keyed under “_thumbnail_id”. Other than for convenience, there’s no reason to ever use post_thumbnail functions.

    Thread Starter KD

    (@kkdevta)

    That makes a lot of sense! thank you for pointing me at the right direction.
    Highly appreciate the help. (:

    Moderator bcworkz

    (@bcworkz)

    You are welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘update attachment metadata with new dimensions after image_resize_dimenstion’ is closed to new replies.