1 file.inc file_validate_image_resolution(File $file, $maximum_dimensions = 0, $minimum_dimensions = 0)

Verifies that image dimensions are within the specified maximum and minimum.

Non-image files will be ignored. If an image toolkit is available the image will be scaled to fit within the desired maximum dimensions.

Parameters

File $file: A file entity. 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.

See also

hook_file_validate()

Related topics

File

core/includes/file.inc, line 1917
API for handling file uploads and server file management.

Code

function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
  $errors = array();
  // Check first that the file is an image.
  if ($info = image_get_info($file->uri)) {
    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) {
        $width = empty($width) ? NULL : $width;
        $height = empty($height) ? NULL : $height;
        // Try to resize the image to fit the dimensions.
        if ($image = image_load($file->uri)) {
          image_scale($image, $width, $height);
          image_save($image);
          $file->filesize = $image->info['file_size'];
          if (!$height) {
            backdrop_set_message(t('The image was resized to fit within the maximum allowed width of %width pixels.', array('%width' => $width)));
          }
          elseif (!$width) {
            backdrop_set_message(t('The image was resized to fit within the maximum allowed height of %height pixels.', array('%height' => $height)));
          }
          else {
            backdrop_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
          }
        }
        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 at least as large as the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
      if ($width > 0 && $height > 0) {
        if ($info['width'] < $width || $info['height'] < $height) {
          $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
        }
      }
      elseif ($width > 0 && $info['width'] < $width) {
        $errors[] = t('The image is too small; the minimum width is %width pixels.', array('%width' => $width));
      }
      elseif ($info['height'] < $height) {
        $errors[] = t('The image is too small; the minimum height is %height pixels.', array('%height' => $height));
      }
    }
  }
  return $errors;
}