Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active September 30, 2020 10:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trepmal/4999215 to your computer and use it in GitHub Desktop.
Save trepmal/4999215 to your computer and use it in GitHub Desktop.
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