Resizing Images for a Gallery-Plugin?

I’m currently working on a gallery plugin similar to the one found on Posterous. I’m needing all images in the gallery in a very small thumbnail-size. Probably only 50px wide.

What’s the best practice to implement the image resizing?

Read More

1.) Adding the Image size via add_image_size()?
This method has the obvious disadvantage, that all previously uploaded images aren’t resized and cannot display properly.

2.) Using timthumb.php or other scripts to resize the thumbnails on the fly?

Related posts

Leave a Reply

1 comment

  1. Its best practice to always use internal WordPress API functions that already exist in place of external scripts.

    You would need to regenerate the thumbnails yourself, which is not overly difficult, if for instance you give yourself a head start and study the source code of existing and similar solutions;

    1. Regenerate Thumbnails Plugin
    2. Simple Image Sizes

    …which are two plugins designed to do just the very thing.

    Also the following is an extract from this answer on the same subject which may also in turn be taken as sample from the Regenerate Thumbnail plugin as an example or be based upon it for example sake. Either way the same suggestion applies “see how others are doing it”

    function regenerateThumbnails() {
    
        $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%'" );
    
        foreach ( $images as $image ) {
            $id = $image->ID;
            $fullsizepath = get_attached_file( $id );
    
            if ( false === $fullsizepath || !file_exists($fullsizepath) )
                return;
    
            if ( wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $fullsizepath ) ) )
                return true;
            else
                return false;
        }
    }
    

    …and I agree, but at least it plants the seed. Refer to the attachments section of the Function Reference in the Codex for a list of common image related functions, in particular (but not limited too),