Resizing all images

时间:2010-12-07 作者:GPX

我刚刚更改了博客的主题,由于页面宽度很小,它打破了布局。我之前的大多数图片(附在帖子中)的宽度都在600px左右,现在我需要按450px的比例调整它们的大小。如何一次性调整它们的大小?有这个插件吗?

P、 美国:编写CSS规则是一个很好的选择,我已经做到了。但我认为调整图像大小也会减少每个页面的下载开销!

4 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

我问a similar but broader question, 我还需要研究并写出一个完整的答案。同时,以下是一些可能对您有所帮助的提示:

我写道a set of plugins that resizes all images to the size used in the editor. 它通过转换<img src="image.jpg" width="200" height="400"/><img src="image-200x400.jpg" width="200" height="400"/>调整img标签的大小),并根据请求捕获并生成缺少的中间大小(按需调整大小器)。您应该能够批量完成第一部分,然后让第二个插件处理新图像。它还适用于部分信息:image-200x.jpg 将返回200像素宽的按比例调整大小的图像。

Regenerate Thumbnails 的确,重新创建了不同大小的图像,但我不认为它也会更新您的帖子内容来引用这些图像?更糟糕的是,如果删除旧的中间尺寸,你的帖子仍然会引用现在不存在的image-600x400.jpg 而不是image-450x300.jpg.

SO网友:Raphael

登录服务器并转到wordpress的根目录。然后执行:

cd wp-content/uploads/;
for img in `find . -name *.jpg -printf \'%p\\n\'`
do
  mogrify -resize "450>x" $img;
done
这将调整上载文件夹(以及所有子文件夹)中每个JPG的大小与此宽度成比例地比450px宽。如果你有PNG,GIF。。。只需在相应文件结束时重新运行即可。

我假设您使用的是标准的上载文件夹(另请调整第一行),并且您的服务器安装了imagemagick。

当然没有担保。

SO网友:Ben

尝试“重新生成缩略图”插件,它可以满足您的需要。

http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/

SO网友:Rıdvan Coşkun

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\' );
}
wordpress主题示例:image

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?