Is it possible to restrict the size of WordPress featured image before uploading

I need to restrict the size of the featured images to 640*480.

I’ve set up the size of the thumbnails and enabled the cropping in my functions.php, but if the sizes of the uploaded images are smaller than 640*480, they won’t be cropped.

So, I came up with two methods to solve this problem:

  1. I tried to add a filter for all uploaded images like this,

    function image_upload_requirements( $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" => "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;
    }
    

    }
    add_filter( ‘wp_handle_upload_prefilter’, ‘image_upload_requirements’ );

The problem is this restriction applied to all the images not only the featured images. This is not what I want.

  1. I wonder if there’s another method, like scaling the image first to make it larger than 640*480 and then cropping it, but I didn’t find a way to do it.

Thanks for answering my questions!

Read more here: Is it possible to restrict the size of WordPress featured image before uploading

Leave a Reply

Your email address will not be published. Required fields are marked *