WordPress.org

Make WordPress Core

Opened 23 months ago

Closed 23 months ago

Last modified 23 months ago

#24759 closed defect (bug) (invalid)

wp_generate_attachment_metadata not generating custom image sizes

Reported by: ryanengland Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.2
Component: Media Keywords:
Focuses: Cc:

Description

I'm having a little trouble resizing some images to the correct size according to a custom image size.

I have been troubleshooting this for a while and trying to work out what is going on, and it would seem that this is an issue with the wp_generate_attachment_metadata function that I've not come across before.

My code to set up the 6 custom image sizes in functions.php is as follows:

add_action('after_setup_theme', 'theme_setup');
function theme_setup() {
    add_image_size( 'TeamMemberImage', '978', '297', true);
    add_image_size( '464by260', '464', '260', true);
    add_image_size( 'AwardLogo', '180', '80', true);
    add_image_size( 'Small', '222', '125');
    add_image_size( 'HomePageRotator', '1420', '665');
    add_image_size( 'Col33Image', '300', '170');
}

I'm using the following code:

$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
print_r($metadata);

For newer images uploaded within the last week or so, the output of the above code is as below:

Array
(
    [width] => 464
    [height] => 261
    [file] => 2013/05/464x261_images-Ade.jpg
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [file] => 464x261_images-Ade-150x150.jpg
                    [width] => 150
                    [height] => 150
                    [mime-type] => image/jpeg
                )

            [464by260] => Array
                (
                    [file] => 464x261_images-Ade-464x260.jpg
                    [width] => 464
                    [height] => 260
                    [mime-type] => image/jpeg
                )

            [AwardLogo] => Array
                (
                    [file] => 464x261_images-Ade-180x80.jpg
                    [width] => 180
                    [height] => 80
                    [mime-type] => image/jpeg
                )

            [Small] => Array
                (
                    [file] => 464x261_images-Ade-222x125.jpg
                    [width] => 222
                    [height] => 125
                    [mime-type] => image/jpeg
                )

            [Col33Image] => Array
                (
                    [file] => 464x261_images-Ade-300x168.jpg
                    [width] => 300
                    [height] => 168
                    [mime-type] => image/jpeg
                )

        )

    [image_meta] => Array
        (
            [aperture] => 0
            [credit] =>
            [camera] =>
            [caption] =>
            [created_timestamp] => 0
            [copyright] =>
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] =>
        )

)

So this is correct. It generates some, but not all, custom image sizes and creates URLs for each of these. However, for older images (possibly uploaded prior to adding the custom image sizes), the output of the above code is as follows:

Array
(
    [width] => 978
    [height] => 300
    [file] => 2013/03/wk_cover_mizunoseiei1.jpg
    [sizes] => Array
        (
            [464by260] => Array
                (
                    [file] => wk_cover_mizunoseiei1-464x260.jpg
                    [width] => 464
                    [height] => 260
                    [mime-type] => image/jpeg
                )

        )

    [image_meta] => Array
        (
            [aperture] => 0
            [credit] =>
            [camera] =>
            [caption] =>
            [created_timestamp] => 0
            [copyright] =>
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] =>
        )

)

So it seems to only generate the one of the custom image sizes (464by260) and none of the others.

Change History (3)

comment:1 @markoheijnen23 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

WordPress doesn't generate images when for new image sizes. You need a plugin like http://wordpress.org/plugins/regenerate-thumbnails/ to generate all image sizes after you added a new image size.

This ticket can be seen as duplicate of #15311

comment:2 @ryanengland23 months ago

But regenerate-thumbnails uses the same function -

http://plugins.trac.wordpress.org/browser/regenerate-thumbnails/trunk/regenerate-thumbnails.php line 372:

$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );

This is what lead me to this issue in the first place. I was using this plugin originally, and it refused to update older images for new image sizes. Is there no way of generating new image sizes for existing images?

comment:3 @markoheijnen23 months ago

I always have something different in my mind with that function. That function should just work. I just tried regenerate thumbnails and it does work for me.

Can you do a var_dump on that specific image of $sizes inside wp_generate_attachment_metadata(); It seems that something goes wrong with that. Maybe a plugin that uses the filter 'intermediate_image_sizes_advanced'.

Note: See TracTickets for help on using tickets.