Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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;
    }
}

Sorich87 said...

Note: This function is not very scalable. It will loop through all the images and regenerate thumbnails one by one, which may consume a large amount of memory. So, you may want to enhance it.

...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),

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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