For compressing the uploaded image I wrote a simple code snippet that immediately overwrites a newly uploaded file with an image whose quality can be specified:
function wt_handle_upload_callback( $data ) {
$image_quality = 30; // Change this according to your needs
$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': {
// Nothing to do here since imagegif doesn't have an 'image quality' option
break;
}
}
return $data;
}
add_filter( 'wp_handle_upload', 'wt_handle_upload_callback' );
Regarding the additional image sizes (just change the return value):
add_filter( 'wp_editor_set_quality', function( $quality ) { return 30; } );