Commit c9678f59 authored by xjm's avatar xjm

Issue #2909676 by Miraya, rang501, ericbellot, Jo Fitzgerald, Daniel_Rose:...

Issue #2909676 by Miraya, rang501, ericbellot, Jo Fitzgerald, Daniel_Rose: Improve messages about image resizing
parent 707740e0
......@@ -434,22 +434,40 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
// Check first that the file is an image.
$image_factory = \Drupal::service('image.factory');
$image = $image_factory->get($file->getFileUri());
if ($image->isValid()) {
$scaling = FALSE;
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) {
// Try to resize the image to fit the dimensions.
if ($image->scale($width, $height)) {
$scaling = TRUE;
$image->save();
if (!empty($width) && !empty($height)) {
$message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $maximum_dimensions]);
$message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
[
'%dimensions' => $maximum_dimensions,
'%new_width' => $image->getWidth(),
'%new_height' => $image->getHeight(),
]);
}
elseif (empty($width)) {
$message = t('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $height]);
$message = t('The image was resized to fit within the maximum allowed height of %height pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
[
'%height' => $height,
'%new_width' => $image->getWidth(),
'%new_height' => $image->getHeight(),
]);
}
elseif (empty($height)) {
$message = t('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $width]);
$message = t('The image was resized to fit within the maximum allowed width of %width pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
[
'%width' => $width,
'%new_width' => $image->getWidth(),
'%new_height' => $image->getHeight(),
]);
}
drupal_set_message($message);
}
......@@ -463,7 +481,22 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
// Check that it is larger than the given dimensions.
list($width, $height) = explode('x', $minimum_dimensions);
if ($image->getWidth() < $width || $image->getHeight() < $height) {
$errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => $minimum_dimensions]);
if ($scaling) {
$errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.',
[
'%dimensions' => $minimum_dimensions,
'%width' => $image->getWidth(),
'%height' => $image->getHeight(),
]);
}
else {
$errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.',
[
'%dimensions' => $minimum_dimensions,
'%width' => $image->getWidth(),
'%height' => $image->getHeight(),
]);
}
}
}
}
......
......@@ -62,6 +62,7 @@ public function testResolution() {
}
if ($image_file->getWidth() < $min_resolution['width']) {
$image_that_is_too_small = $image;
$image_that_is_too_small_file = $image_file;
}
if ($image_that_is_too_small && $image_that_is_too_big) {
break;
......@@ -69,7 +70,11 @@ public function testResolution() {
}
$this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article');
$this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
$this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => '50x50']));
$this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [
'%dimensions' => '50x50',
'%width' => $image_that_is_too_small_file->getWidth(),
'%height' => $image_that_is_too_small_file->getHeight(),
]));
$this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
$this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'));
$this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment