function file_validate_image_resolution

7.x file.inc file_validate_image_resolution(stdClass $file, $maximum_dimensions = 0, $minimum_dimensions = 0)
6.x file.inc file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0)

If the file is an image verify that its dimensions are within the specified maximum and minimum dimensions. Non-image files will be ignored.

Parameters

$file: A Drupal file object. This function may resize the file affecting its size.

$maximum_dimensions: An optional string in the form WIDTHxHEIGHT e.g. '640x480' or '85x85'. If an image toolkit is installed the image will be resized down to these dimensions. A value of 0 indicates no restriction on size, so resizing will be attempted.

$minimum_dimensions: An optional string in the form WIDTHxHEIGHT. This will check that the image meets a minimum size. A value of 0 indicates no restriction.

Return value

An array. If the file is an image and did not meet the requirements, it will contain an error message.

Related topics

2 string references to 'file_validate_image_resolution'
upload_node_form_submit in drupal-6.x/modules/upload/upload.module
Save new uploads and store them in the session to be associated to the node on upload_save.
user_validate_picture in drupal-6.x/modules/user/user.module

File

drupal-6.x/includes/file.inc, line 829
API for handling file uploads and server file management.

Code

function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
  $errors = array();

  // Check first that the file is an image.
  if ($info = image_get_info($file->filepath)) {
    if ($maximum_dimensions) {
      // Check that it is smaller than the given dimensions.
      list($width, $height) = explode('x', $maximum_dimensions);
      if ($info['width'] > $width || $info['height'] > $height) {
        // Try to resize the image to fit the dimensions.
        if (image_get_toolkit() && image_scale($file->filepath, $file->filepath, $width, $height)) {
          drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));

          // Clear the cached filesize and refresh the image information.
          clearstatcache();
          $info = image_get_info($file->filepath);
          $file->filesize = $info['file_size'];
        }
        else {
          $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
        }
      }
    }

    if ($minimum_dimensions) {
      // Check that it is larger than the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
      if ($info['width'] < $width || $info['height'] < $height) {
        $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
      }
    }
  }

  return $errors;
}