Skip to content

Instantly share code, notes, and snippets.

@vollyimnetz
Created October 22, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vollyimnetz/4220b7b29be1a779017886c87672bc71 to your computer and use it in GitHub Desktop.
Save vollyimnetz/4220b7b29be1a779017886c87672bc71 to your computer and use it in GitHub Desktop.
<?php
/*
Modification of the following plugin:
Plugin Name: resize-on-upload
Version: 1.0.1
Plugin URI: http://blog.yeticode.co.uk/resize-on-upload
Description: Provides the ability to set a maximum width or height an uploaded image can be, if the image is larger then it is resized.
Author: John Tindell
Author URI: http://blog.yeticode.co.uk/
*/
if ( ! class_exists( 'ROU_Admin' ) ) {
class ROU_Admin{
public function check_image_size($array){
//error_log(print_r($array,true));
$options['rou_max_width'] = 1920;
$options['rou_max_height'] = 1600;
$options = apply_filters('tm_framework_rou_options',$options);
/** HOW TO USE:
add_filter('tm_framework_rou_options','tm_framework_rou_options_filter');
function tm_framework_rou_options_filter($options) {
$options['rou_max_height'] = 1920;
return $options;
}
**/
//skip if file ends with ".noresize"
$_pathinfo = pathinfo($array['url']);
if(substr($_pathinfo['filename'], ((-1)*strlen('.noresize')) ) == '.noresize') {
return $array;
}
//do resize
if($this->startsWith($array['type'],"image")){
// the content type says its an image so lets run with it
$path_to_file = $array['file'];
// check the width and height against the maximum allowed
$image_size = getimagesize($path_to_file);
// if they are greater then scale the image down
//$options = get_option('ROU_Options',"");
// ok actually check the size of the image and not just
// ignore it like....
if($image_size[0] > $options['rou_max_width'] || $image_size[1] > $options['rou_max_height']){
//$new_path = image_resize( $path_to_file, $options['rou_max_width'], $options['rou_max_height'] );
$editor = wp_get_image_editor( $path_to_file );
$new_path = $editor->resize($options['rou_max_width'], $options['rou_max_height']);
$editor->save($path_to_file);
//unlink($path_to_file);
//rename($new_path,$path_to_file);
}
// save image
}
return $array;
} // end check_image_size
//http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions
function startsWith($haystack, $needle){
return !strncmp($haystack, $needle, strlen($needle));
}
} // END ROU_Admin
}
add_action('wp_handle_upload', array(new ROU_Admin(),'check_image_size'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment