Check Image Dimensions

Description: Checks to see if a given image meets specified image width and height dimensions.
Tested Platform: PHP 7+
Language: PHP
// Check image file and make sure they are equal to width and height specified.
// Expects: Valid path to an image and optional image width/height as integers
// Returns: true or false

function checkImageDimensions($imageFilename = "", $imgWidth = 1, $imgHeight = 1) {
     if (!empty($imageFilename) && file_exists($imageFilename) && (!is_dir($imageFilename))) {
          list($width, $height, $type, $attr) = getimagesize($imageFilename);
        
          if (($width == $imgWidth) && ($height == $imgHeight)) {
               return true;
          }
     }
     return false;
}

Posted: March 18, 2023

Return to the snippets listing