How to compress original image during upload?
-
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?
- The topic ‘How to compress original image during upload?’ is closed to new replies.