Prevent image upload unless exact size

Haven’t tested it, but this should work:

// check for file upload size //
{
if( !current_user_can( 'administrator') )
add_filter( 'wp_handle_upload_prefilter', 'mdu_validate_image_size' ); 
}
add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {
    if ( mime_content_type($file) == 'application/zip' ) {
        mdu_validate_zip_image_size($file);
        return $file;
    }
$image = getimagesize($file['tmp_name']);
$minimum = array(
'width' => '800',
'height' => '450'
);
$maximum = array(
'width' => '800',
'height' => '450'
);
$image_width = $image[0];
$image_height = $image[1];

$too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is   $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
// add in the field 'error' of the $file array the message 
$file['error'] = $too_small; 
return $file;
}
elseif ( $image_width > $maximum['width'] || $image_height >  $maximum['height'] ) {
//add in the field 'error' of the $file array the message
$file['error'] = $too_large; 
return $file;
}
else
return $file;
}

function mdu_validate_zip_image_size($file) {
    $zip = new ZipArchive();
    if (true !== $zip->open($file))
    {
        $file['error'] = 'Could not open ZIP archive';
    }

    // Search for the image file.
    for($i = 0; $i < $zip->numFiles; $i++)
    {
        $entry = $zip->statIndex($i);
        $ext = substr($entry['name'], -3);
        if (in_array($ext, array('jpg', 'png'))
        {
            $filename = $entry['name'];
        }
    }

    if (isset($filename) && ($image = $zip->getFromName($filename)))
    {
        list($image_width, $image_height) = getimagesize($image);
    }
    else
    {
        $file['error'] = 'No image found';
    }

    $minimum = array(
    'width' => '800',
    'height' => '450'
    );
    $maximum = array(
    'width' => '800',
    'height' => '450'
    );

    $too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is   $image_width by $image_height pixels.";
    $too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

    if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
        // add in the field 'error' of the $file array the message 
        $file['error'] = $too_small; 
        return $file;
    }
    elseif ( $image_width > $maximum['width'] || $image_height >  $maximum['height'] ) {
        //add in the field 'error' of the $file array the message
        $file['error'] = $too_large; 
        return $file;
    }
    else
        return $file;
    }

}

Leave a Comment