• WordPress will compress scaled thumbnails of picture during upload, but, original image will be is same quality.

    I am trying to compress original image too. I wish to make image in original resolution available for users, but to compress it and save disk space. For example original 16 MP, hi resolution image can be 5 MB, but after applying 85% compression filter it can be 2-3 MB without noticeable quality loss.

    Also I need to preserve image EXIF data.

    Solution (need tweak):

    This function will compress original photo too, but it will strip EXIF/IPTC data too.

    function wt_handle_upload_callback( $data ) {
        $image_quality = 85; // 85% commpresion of original image
        $file_path = $data['file'];
        $image = false;
    
        switch ( $data['type'] ) {
            case 'image/jpeg': {
                $image = imagecreatefromjpeg( $file_path );
                imagejpeg( $image, $file_path, $image_quality );
                break;
            }
    
            case 'image/png': {
                $image = imagecreatefrompng( $file_path );
                imagepng( $image, $file_path, $image_quality );
                break;
            }
    
            case 'image/gif': {
                // No 'image quality' option
                break;
            }
        }
    
        return $data;
    }
    add_filter( 'wp_handle_upload', 'wt_handle_upload_callback' )

    How to preserve EXIF data, and to compress original image?

Viewing 2 replies - 16 through 17 (of 17 total)
  • Moderator bcworkz

    (@bcworkz)

    Apparently ??

    The problem is you are using PHP’s imagecreatefrom*(). I’m not sure what’s involved with those functions, but anything you do with WP will not change how native PHP functions work. Furthermore, I believe these PHP functions always use GD, which always strips EXIF data.

    For the WP filters to work, you need to recompress images with WP_Image_Editor_Imagick::resize(). This means you need to determine the original image dimensions so you can feed them to the resize method if you wish to retain the original size.

    Thread Starter Advanced SEO

    (@jole5)

    Yes, you are right. Function above compress image using native PHP functions and GD library, which removes EXIF (that is nature of GD).

    WordPress Image API uses ImageMagick if available (it is in my case), and there will be solution.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘How to compress original image during upload?’ is closed to new replies.