diff --git a/core/modules/file/file.module b/core/modules/file/file.module index ed7592f..e22082f 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -426,13 +426,17 @@ function file_validate_is_image(File $file) { * @param \Drupal\file\File $file * A file entity. This function may resize the file affecting its size. * @param $maximum_dimensions - * An optional string in the form WIDTHxHEIGHT e.g. '640x480' or '85x85'. If + * (optional) A 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. * @param $minimum_dimensions - * An optional string in the form WIDTHxHEIGHT. This will check that the + * (optional) A string in the form WIDTHxHEIGHT. This will check that the * image meets a minimum size. A value of 0 indicates no restriction. + * @param $scale + * (optional) Boolean value to scale the image down to fit within the maximum + * dimensions if it is too large. Note that this may be resource intensive + * if the upload image is very large. * * @return * An array. If the file is an image and did not meet the requirements, it @@ -440,17 +444,28 @@ function file_validate_is_image(File $file) { * * @see hook_file_validate() */ -function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $minimum_dimensions = 0) { +function file_validate_image_resolution(stdClass $file, $maximum_dimensions = 0, $minimum_dimensions = 0, $scale = TRUE) { $errors = array(); + list($max_width, $max_height) = $maximum_dimensions ? explode('x', $maximum_dimensions) : array(0, 0); + list($min_width, $min_height) = $minimum_dimensions ? explode('x', $minimum_dimensions) : array(0, 0); + // Check first that the file is an image. $image_factory = \Drupal::service('image.factory'); $image = $image_factory->get($file->getFileUri()); if ($image->isSupported()) { if ($maximum_dimensions) { // Check that it is smaller than the given dimensions. - list($width, $height) = explode('x', $maximum_dimensions); - if ($image->getWidth() > $width || $image->getHeight() > $height) { + if ($info['width'] > $max_width || $info['height'] > $max_height) { + $ratio = min($max_width / $info['width'], $max_height / $info['height']); + // Check for exact dimension requirements (scaling allowed). + if (strcmp($minimum_dimensions, $maximum_dimensions) == 0 && $info['width'] / $max_width != $info['height'] / $max_height) { + $errors[] = t('The image must be exactly %dimensions pixels.', array('%dimensions' => $maximum_dimensions)); + } + // Check that scaling won't drop the image below the minimum dimensions. + elseif ($minimum_dimensions && image_get_toolkit() && (($info['width'] * $ratio < $min_width) || ($info['height'] * $ratio < $min_height))) { + $errors[] = t('The image will not fit between the dimensions of %min_dimensions and %max_dimensions pixels.', array('%min_dimensions' => $minimum_dimensions, '%max_dimensions' => $maximum_dimensions)); + } // Try to resize the image to fit the dimensions. $image = $image_factory->get($file->getFileUri()); if ($image->getResource()) { @@ -465,10 +480,9 @@ function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $mi } } - if ($minimum_dimensions) { + if ($minimum_dimensions && empty($errors)) { // Check that it is larger than the given dimensions. - list($width, $height) = explode('x', $minimum_dimensions); - if ($image->getWidth() < $width || $image->getHeight() < $height) { + if ($image->getWidth() < $min_width || $image->getHeight() < $min_height) { $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions)); } }