Case:
I have a client that doesn’t know much about the digital world. However what she does know is how to get the photo’s from her camera on to the computer and in WordPress. However she doesn’t know how to downsize the photo’s to a normal size.
Solution:
I like WordPress to automatically downsize those photo’s in the background to a maximum width of 1024px.
Problem:
Although I can set maximum width in the settings, I can set the $content_width and I can add new image sizes with ‘add_image_size’. The original photo is still stored in the upload-folder in it’s original size. Meaning the harddisk space will easily get full.
Question:
What should I put in functions.php to let WordPress downsize the original image (if it’s larger then the maximum width)?
I was able to solve it using the following code:
function my_handle_upload ( $params )
{
$filePath = $params['file'];
if ( (!is_wp_error($params)) && file_exists($filePath) && in_array($params['type'], array('image/png','image/gif','image/jpeg')))
{
$quality = 90;
list($largeWidth, $largeHeight) = array( get_option( 'large_size_w' ), get_option( 'large_size_h' ) );
list($oldWidth, $oldHeight) = getimagesize( $filePath );
list($newWidth, $newHeight) = wp_constrain_dimensions( $oldWidth, $oldHeight, $largeWidth, $largeHeight );
$resizeImageResult = image_resize( $filePath, $newWidth, $newHeight, false, null, null, $quality);
unlink( $filePath );
if ( !is_wp_error( $resizeImageResult ) )
{
$newFilePath = $resizeImageResult;
rename( $newFilePath, $filePath );
}
else
{
$params = wp_handle_upload_error
(
$filePath,
$resizeImageResult->get_error_message()
);
}
}
return $params;
}
add_filter( 'wp_handle_upload', 'my_handle_upload' );
The original file was 3,3Mb after it was uploaded, with large dimensions set to 2048×2048, it took only 375Kb on the server (about 90% reduction!)