Enforce minimum image dimensions in Wordpress


/ Published in: PHP

Add this snippet to your Wordpress theme's functions.php file to reject any image uploads that are below the specified dimensions.


Copy this code and paste it in your HTML
  1. /**
  2.  * Enforce minimum image upload size.
  3.  */
  4. add_filter('wp_handle_upload_prefilter','handle_upload_prefilter');
  5.  
  6. function handle_upload_prefilter($file) {
  7. $img=getimagesize($file['tmp_name']);
  8. $minimum = array('width' => '640', 'height' => '480');
  9. $width= $img[0];
  10. $height =$img[1];
  11.  
  12. if ($width < $minimum['width'] ){
  13. return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px.");
  14. } elseif ($height < $minimum['height']){
  15. return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image width is $width px.");
  16. } else {
  17. return $file;
  18. }
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.