Posted By

f6design on 11/30/11


Tagged

wordpress images


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jbyerson
alexsancho


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.

  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  

You need to login to post a comment.