Image Dimension Restriction

Is it possible to restrict the uploaded image into square? Basically won’t accept anything if the dimensions of the image is not square.

You can change the image size in Settings > Media. While you can’t change the width, you can set the height to the same dimension; this way, uploaded images will be cropped according to the set proportions.

But that does not restrict the uploaded images. It just crops them and accept the uploaded images. I want the upload function to only accept 2x2 images. Basically reject if the uploaded image is not square.

Unfortunately there’s no option to require a specific size, this would require a custom implementation. I highly recommend setting the image size and cropping rules in Settings/Media, because users may not be familiar with image editing tools to resize and crop images before the uploading, this may lower the conversions.

Not an exact solution to your issue but you can restrict users to uploading minimum file size by adding this code to your functions.php


add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file)
{

    $img=getimagesize($file['tmp_name']);
    $minimum = array('width' => '400', 'height' => '400');
    $width= $img[0];
    $height =$img[1];

    if ($width < $minimum['width'] )
        return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");

    elseif ($height <  $minimum['height'])
        return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
    else
        return $file; 
}

This example has the minimum set to 400 by 400 but you can change it to whatever you want. :slight_smile:

2 Likes

Hello, i used this snippet a it worked! Thanks! The only downside is that restrict this to any type of file. Can it be adjusted only to images?

Sorry, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work https://fvrr.co/32e7LvY

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.