Resizing all images

I have just changed the theme on my blog, and because of a small page width, it breaks the layout. Most of my earlier images (attached in posts) are around 600px in width, and now I need to resize all of them them proportionately to 450px. How do I resize them all at one shot? Is there a plugin for this?

P.S.: Writing a CSS rule is a good option, and I’ve already done that. But I figure resizing the images would also knock off the download overhead for each page!

Related posts

Leave a Reply

4 comments

  1. I asked a similar but broader question, and I still need to research and write a complete answer. In the meantime here are some pointers that might help you:

    I wrote a set of plugins that resizes all images to the size used in the editor. It works by converting <img src="image.jpg" width="200" height="400"/> to <img src="image-200x400.jpg" width="200" height="400"/> (Resize img tags) and catching and generating those missing intermediate sizes on request (On-Demand Resizer). You should be able to do the first part in bulk, and then let the second plugin handle the new images. It also works with partial information: image-200x.jpg will return a proportionally-resized image 200 pixels wide.

    Regenerate Thumbnails indeed re-creates images at different sizes, but I don’t think it also updates your post content to refer to these images? Even worse, if it deletes the old intermediate sizes, your post will still reference the now non-existing image-600x400.jpg instead of image-450x300.jpg.

  2. Log in to your server and go to the root directory of your wordpress. Then execute:

    cd wp-content/uploads/;
    for img in `find . -name *.jpg -printf '%pn'`
    do
      mogrify -resize "450>x" $img;
    done
    

    This will resize every JPG in your upload folder (and all subfolders!) that are wider than 450px to this width proportionally. If you have PNG, GIF, … just rerun with the respective file ending.

    I assume that you use the standard upload folder (adapt first line otherwise) and that your server has imagemagick installed.

    No warranty, of course.

  3. if ( defined( 'WP_CLI' ) && WP_CLI ) {
        class RegenThumbs extends WP_CLI_Command
        {
            var $errors = false;
            var $unique_size_name = 'past_favourites';
              var $unique_size = 170; 
    
            public function __construct()
            {
                global $wpdb;
    
                if ( !$images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID asc" ) ) {
                    WP_CLI::error( "Unable to find any images. Are you sure some exist?" );
                    return;
                }
    
                WP_CLI::line( 'Found ' . count( $images ) . ' pictures to regenerate!' );
    
                foreach ( $images as $image )
                    $this->process( $image->ID );
    
                if ( $this->errors )
                    WP_CLI::error( 'Finished regenerating images - however, there were some errors throughout.' );
                else
                    WP_CLI::success( 'Finished - without any errors either!' );
            }
    
            function process( $id=false )
            {
            if( ! $id )
            return;            
    
                $image = get_post( $id );
    
                if ( !$image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) ) {
                    $this->errors = true;
                    WP_CLI::line( "FAILED: {$image->post_title} - invalid image ID" );
                    return;
                }
    
            $meta_data = wp_get_attachment_metadata( $image->ID );
    
            if( isset( $meta_data['sizes'][ $this->unique_size_name ] ) && $meta_data['sizes'][ $this->unique_size_name  ]['width'] == $this->unique_size )
            {
            WP_CLI::line( "SKIPPED: $image->ID - There is an image called " . $this->unique_size_name );
            return;
            }
    
            $fullsizepath = str_replace( array( 
                            '/home/username/public_html/', 
                            '/home/17196/domains/domain.com/html/',
                            '/nfs/c02/h07/mnt/17196/domains/domain.com/html/',
                            '/mnt/gs02/herd04/17196/domains/domain.com/html/',
                            '/nfs/c02/h04/mnt/17196/domains/domain.com/html/'
                       ), 
                        '/srv/www/domain.com/current/', 
                        get_attached_file( $image->ID ) 
                );
    
                if ( false === $fullsizepath || !file_exists( $fullsizepath ) ) {
                    $this->errors = true;
                    WP_CLI::line( "FAILED: {$image->post_title} -  Can't find it $fullsizepath" );
                    return;
                }
    
                // 5 minutes per image should be PLENTY
                @set_time_limit( 900 );
    
                $array_path = explode( DIRECTORY_SEPARATOR, $fullsizepath );
                $array_file = explode( '.', $array_path[ count( $array_path ) - 1 ] );
    
                $imageFormat = $array_file[ count( $array_file ) - 1 ];
    
                unset( $array_path[ count( $array_path ) - 1 ] );
                unset( $array_file[ count( $array_file ) - 1 ] );
    
                $imagePath = implode( DIRECTORY_SEPARATOR, $array_path ) . DIRECTORY_SEPARATOR . implode( '.', $array_file );
                $dirPath   = explode( DIRECTORY_SEPARATOR, $imagePath );
                $imageName = sprintf( "%s-", $dirPath[ count( $dirPath ) - 1 ] );
                unset( $dirPath[ count( $dirPath ) - 1 ] );
                $dirPath = sprintf( "%s%s", implode( DIRECTORY_SEPARATOR, $dirPath ), DIRECTORY_SEPARATOR );
    
                // Read and delete files
                $dir   = opendir( $dirPath );
                $files = array();
                while ( $file = readdir( $dir ) ) {
                    if ( !( strrpos( $file, $imageName ) === false ) ) {
                        $thumbnail = explode( $imageName, $file );
                        if ( $thumbnail[ 0 ] == "" ) {
                            $thumbnailFormat = substr( $thumbnail[ 1 ], -4 );
                            $thumbnail       = substr( $thumbnail[ 1 ], 0, strlen( $thumbnail[ 1 ] ) - 4 );
                            $thumbnail       = explode( 'x', $thumbnail );
                            if ( count( $thumbnail ) == 2 ) {
                                if ( is_numeric( $thumbnail[ 0 ] ) && is_numeric( $thumbnail[ 1 ] ) ) {
                                    WP_CLI::line( "Thumbnail: {$thumbnail[0]} x {$thumbnail[1]} was deleted." );
                                    @unlink( $dirPath . $imageName . $thumbnail[ 0 ] . 'x' . $thumbnail[ 1 ] . $thumbnailFormat );
                                }
                            }
                        }
                    }
                }
    
                $metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
    
                if ( is_wp_error( $metadata ) ) {
                    WP_CLI::line( $metadata->get_error_message() );
                    return;
                }
    
                if ( empty( $metadata ) ) {
                    $this->errors = true;
                    WP_CLI::line( 'Unknown failure reason.' );
                    return;
                }
                wp_update_attachment_metadata( $image->ID, $metadata );
                WP_CLI::line( esc_html( get_the_title( $image->ID ) ) . " (ID {$image->ID}): All thumbnails were successfully regenerated in  " . timer_stop() . "  seconds " );
            }
    
        }
        WP_CLI::addCommand( 'regen_thumbs', 'RegenThumbs' );
    }
    

    Sample wordpress theme : image