Con este código es posible detectar las dimensiones de una imagen y evitar que se cargue, obligando al usuario a modificar le imagen antes de subirla, mejorando así la carga de la web y ahorrando espacio en el servidor.
Añadir el siguiente código en functions.php de nuestro tema hijo.
/* * Test tamaño de imagen */ add_filter('wp_handle_upload_prefilter','ypro_validate_image_size'); function ypro_validate_image_size( $file ) { $image = getimagesize($file['tmp_name']); $minimum = array( 'width' => '640', 'height' => '480' ); $maximum = array( 'width' => '2000', 'height' => '2000' ); $image_width = $image[0]; $image_height = $image[1]; $too_small = "Imagen demasiado pequeña. El tamaño mínimo es {$minimum['width']} x {$minimum['height']} pixels. Has intentado subir una imagen de $image_width x $image_height pixels."; $too_large = "Image demasiado grande. El tamaño máximo es {$maximum['width']} x {$maximum['height']} pixels. Has intentado subir una imagen de $image_width x $image_height pixels."; if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) { // add in the field 'error' of the $file array the message $file['error'] = $too_small; return $file; } elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) { //add in the field 'error' of the $file array the message $file['error'] = $too_large; return $file; } else return $file; }
Si sólo deseamos limitar por tamaño mínimo:
add_filter('wp_handle_upload_prefilter','ypro_upload_prefilter'); function ypro_upload_prefilter($file) { $img=getimagesize($file['tmp_name']); $minimum = array('width' => '640', 'height' => '480'); $width= $img[0]; $height =$img[1]; if ($width < $minimum['width'] ) return array("error"=>"La imagen es demasiado pequeña. El ancho mínimo es {$minimum['width']}px. La imagen tiene un ancho de $width px"); elseif ($height < $minimum['height']) return array("error"=>"La imagen es demasiado pequeña. El alto mínimo es {$minimum['height']}px. La imagen tiene un alto de $height px"); else return $file; }