0

I've uploaded several images to my blog posts. I didn't cared about their size so I've put there even 5mb images which could be easily compressed for 200kb. I know that wordpress already processed them, cutted and customized, but now I'm worried as my whole blog is very big.

I would like to delete originals or at least replace them with compressed copies.

Can I do it manually, download, delete, compress and upload with the same name ?

or

Is there any simple plugin which will do the same for me ?

Thank you!

3 Answers 3

0

Good question, I have a chance to write a small wrapper that basically implements such functionality...

  1. I was thinking about when I should run my optimization and what exactly I should process files. Ended up in next conclusions...
    1. The main Image will be changed somewhere later, using wp_cron schedule. Nothing particularly interesting... you taking your file, run it through the optimisation pipeline, and in case Success setting param to not optimize it again in post meta, in case of fail mark it as Failed and forget about image.
    2. With copies it was easier. I just use wp_update_attachment_metadata filter $hook to get files list when update metadata was triggered (on image size generation for example).

```

add_filter( 'wp_update_attachment_metadata', 
    array( __CLASS__, 'optimize_images_in_foreach' ), 100, 2 );

Files Optimization.
/**
 * Optimize Copies of imges in loop while images regeneration
 *
 * @param  array  $metadata      Image Metadata (images, exif data, etc...)
 * @param  int    $attachment_id Attachment id
 * @return array
 */
function optimize_images_in_foreach( $metadata, $attachment_id ) {
    $original_file = get_attached_file( $attachment_id );
    if ( ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
       foreach( $metadata['sizes'] as $size => $item )
          do_action( 'optimize_image', 
            str_replace( basename( $original_file ), $item['file'], $original_file ) );
        }
    }    
    return $metadata;
}

```

  1. As you see I have just run own action for images... optimize_image. On first stage it was just wrapper for pngout and jpegtran, later it was a bit harder implementation with advanced cron schedules and Kraken service (image optimization).

```

/**
 * Run Image Optimization to Jpeg's and PNG's

 * @param  string $image IMage Location in filesystem
 * @return void
 */
function optimize_images_in_foreach_jpeg_png_optimization( $image ) {
    $extension = array_pop(explode( '.', basename( $image ) ) );

    // `images_optimization` is global option that holds a cli command for image optimization
    //  examples...
    // 'png'  => /opt/local/bin/optipng = '{$image}', 
    // 'jpeg' => /opt/local/bin/jpegtran -copy none -maxmemory 256Mb  -optimize \
    //                                          -outfile '{$image}' '{$image}'

    $optimization = get_site_option('images_optimization');

    if ( in_array($extension, array( 'jpeg', 'jpg' ) ) ) {
        $type = 'jpeg';
    } elseif ( in_array( $extension, array( 'png' ) ) ) {
        $type = 'png';
    } else {
        $type = 'default';
    }

    if ( ! empty( $optimization[ $type ] ) ) {
        $optimization[$type] = str_replace( '{$image}', $image, $optimization[ $type ] ); 
        // execute is a pipline to exec in this itteration.
        execute( "{$optimization[$type]}" );
    }   
}

```

P.S. I beg a pardon for a code formatting... it's failing to work without "```"

0

You can use Regenerate Thumbnails Plugin

for regenerate all images and you should create required size in theme using:

function custom_thumbs() {
    add_image_size( 'cover-size', 1600, 450, true ); 
}
add_action( 'after_setup_theme', 'custom_thumbs' );

you can use optimized image using :

$attachment_id = get_post_thumbnail_id( $post->ID ); 
$img_src = wp_get_attachment_image_url( $attachment_id, 'cover-size' ); 
$img_srcset = wp_get_attachment_image_srcset( $attachment_id, 'cover-size' );

Above function help you to load page very fast in browser and it will not load original image.

0

Hello the "EWWW Image Optimizer" plugin will do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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