-
-
Save waqasy/1800f9645aeb66d45a6f84ba8194de37 to your computer and use it in GitHub Desktop.
resize new uploads to fit max dimensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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