• I am having a hard time writing my own function to regenerate the thumbnail and all intermediate sizes for already uploaded images. For debugging purposes I am setting $img to a pre-existing attachment ID

    
    $img = 303;
    $meta = wp_get_attachment_metadata( $img );
    print("<pre>".print_r($meta,true)."</pre>");
    
    $fullsizepath = wp_get_attachment_url($img);
    print( $fullsizepath );
    
    $newMeta = wp_generate_attachment_metadata( $img, $fullsizepath );
    wp_update_attachment_metadata( $img, $newMeta );
    print("<pre>".print_r($newMeta,true)."</pre>");
    

    The first print function lists the correct pre-existing metadata with all sizes and image_meta. Printing $fullsizepath gives the correct full path. When adding the wp_generate_attachment_metadata() function it takes an extra second to execute which I guess it actually reads the file from disk. But the last print() returns only a minimal metadata array without any size elements or image_meta:

    Array
    (
        [width] => 2994
        [height] => 1952
        [file] => https://localhost/v4.ch3.gr/file/ch3_077-37.jpg
        [sizes] => Array
            (
            )
    
    )

    Am I missing something to make this work?

    thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The returned value has an empty “sizes” element when the files to be resized already exist. It’s the way the image editor multi_resize() function works, which is the source of the sizes array. It doesn’t mean the wrong data is saved to the DB.

    You’ll likely want to hack the source code for wp_generate_attachment_metadata() in order to investigate how it all works. It’s OK to do this temporarily on a test site as long as you have no illusion that the edits will persist after updates.

    Thread Starter ch3

    (@ch3)

    I tried and deleted one of the resized files from disk and also removed one of the size entries in the image_meta. When I run the command again it still produced the same results without generating the missing thumbnail.
    What else do I need to do to force WP to regenerate the thumbnails of an already uploaded image?

    Thread Starter ch3

    (@ch3)

    This code also doesn’t seem to do anything neither give any errors, whether I run it on a file that has been already added to the library or not.

        $img_path = 'D:/myStuff/ch3/web/v4.ch3.gr/file/ch3_102-17.jpg';
        $gd_image_editor = new WP_Image_Editor_GD($img_path); 
        $gd_image_editor->load(); 
        // $gd_image_editor->resize(300,450,false); 
        $gd_image_editor->multi_resize(get_intermediate_image_sizes());
        $gd_image_editor->save($img_path);

    Does it matter that I set a different upload directory?
    define('UPLOADS', 'file');

    Moderator bcworkz

    (@bcworkz)

    It doesn’t matter where your upload directory is in the file system, as long as it’s the upload directory for your site. I don’t think multi_resize() works at all if there are existing files, it appears to be relying upon a fresh upload for it’s data. Unsurprisingly, the returned array has all the expected data when a fresh upload occurs.

    I don’t have a definitive answer for you regarding how to force sized images to be regenerated. I’m barely ahead of you in what I know about this ?? What I would try next is to move the original file to a different temporary directory. Use rename() to update the path without messing with file data. Then delete all of the related resized image files in uploads. Set the new file location as $img_path, then maybe your test code will work.

    If not, you may have to dig into the inner workings of the image editor to determine why.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_generate_attachment_metadata() doesn’t generate files’ is closed to new replies.