Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
resize new uploads to fit max dimensions
<?php
/*
Plugin Name: Resize Original Upload
*/
add_filter('wp_handle_upload', 'max_dims_for_new_uploads', 10, 2 );
function max_dims_for_new_uploads( $array, $context ) {
// $array = array( 'file' => $new_file, 'url' => $url, 'type' => $type )
// $context = 'upload' || 'sideload'
$ok = array( 'image/jpeg', 'image/gif', 'image/png' );
if ( ! in_array( $array['type'], $ok ) ) return $array;
$editor = wp_get_image_editor( $array['file'] );
if ( is_wp_error( $editor ) )
return $editor;
$editor->set_quality( 90 );
$editor->resize( 1500, 1500 ); // (int) max width, (int) max height[, (bool) crop]
$editor->save( $array['file'] );
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment