Restrict image ratio when Vendors upload their product images

You need to add the below code in the functions.php file of your current active theme –

function resize_resolution($file) {
$image = getimagesize($file[‘tmp_name’]);
$minimum = array(
‘width’ => ‘400’, //set your minimum
‘height’ => ‘400’
);
$maximum = array(
‘width’ => ‘2000’, //set your maximum
‘height’ => ‘2000’
);
$image_width = $image[0];
$image_height = $image[1];

$too_small = “Image dimensions are too small.”;
$too_large = “Image dimensions are too large.”;

if ( $image_width < $minimum[‘width’] || $image_height < $minimum[‘height’] ) {
$file[‘error’] = $too_small;
return $file;
}
elseif ( $image_width > $maximum[‘width’] || $image_height > $maximum[‘height’] ) {
$file[‘error’] = $too_large;
return $file;
}
else
return $file;
}
add_filter(‘wp_handle_upload_prefilter’, ‘resize_resolution’);

Leave a Reply