Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 67836

Big (heavy) images with small width and height

$
0
0

Replies: 0

Hello! I set my images to fit within 1024 x 1024 pixels width/height. And that works perfect.
But, when I try to upload Big (heavy) image with dimensions = 800*900 pixels it do not compressed and steel weight 1 Megabyte

I was able to fix this by adding this code to my function.php

// Compress uploaded images
// http://wordpress.stackexchange.com/questions/63707/automatically-replace-original-uploaded-image-with-large-image-size
// https://codex.wordpress.org/Function_Reference/wp_get_image_editor
function replace_uploaded_image($image_data) {

    // if there is no large image : return
    //if (!isset($image_data['sizes']['large'])) return $image_data;

    // paths to the uploaded image and the large image
    $upload_dir = wp_upload_dir();
    $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];

    $width = $image_data['width'];
    $height = $image_data['height'];
    $image = wp_get_image_editor( $uploaded_image_location );
    if ( ! is_wp_error( $image ) ) {
        //$image->rotate( 90 );
        $image->resize( $width, $height, true );
        $image->save( $uploaded_image_location );
    }

    // update image metadata and return them
    //$image_data['width'] = $width;
    //$image_data['height'] = $height;

    return $image_data;
}

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');

But I think, that it would be nice to add this functionality to plugin settings. Maybe someone else, also have this issue.


Viewing all articles
Browse latest Browse all 67836

Trending Articles