diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 8c8aea2..85d3494 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -201,8 +201,8 @@ public function rotate($degrees, $background = NULL) { /** * {@inheritdoc} */ - public function scaleAndCrop($width, $height) { - return $this->apply('scale_and_crop', array('width' => $width, 'height' => $height)); + public function scaleAndCrop($width, $height, $upscale = TRUE) { + return $this->apply('scale_and_crop', array('width' => $width, 'height' => $height, 'upscale' => $upscale)); } /** diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 02db478..63ad482 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -142,11 +142,14 @@ public function scale($width, $height = NULL, $upscale = FALSE); * The target width, in pixels. * @param int $height * The target height, in pixels. + * @param bool $upscale + * (optional) Boolean indicating that files smaller than the dimensions will + * be scaled up. This generally results in a low quality image. * * @return bool * TRUE on success, FALSE on failure. */ - public function scaleAndCrop($width, $height); + public function scaleAndCrop($width, $height, $upscale = TRUE); /** * Crops an image to a rectangle specified by the given dimensions. diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 4f1fc79..2c8ca4f 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -58,6 +58,10 @@ image.effect.image_scale: image.effect.image_scale_and_crop: type: image_size label: 'Image scale and crop' + mapping: + upscale: + type: boolean + label: 'Upscale' image.settings: type: mapping diff --git a/core/modules/image/image.module b/core/modules/image/image.module index ba8c979..0a87bb4 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -137,6 +137,10 @@ function image_theme() { 'variables' => array('data' => NULL, 'effect' => array()), 'template' => 'image-crop-summary', ), + 'image_scale_and_crop_summary' => array( + 'variables' => array('data' => NULL, 'effect' => array()), + 'template' => 'image-scale-and-crop-summary', + ), 'image_rotate_summary' => array( 'variables' => array('data' => NULL, 'effect' => array()), 'template' => 'image-rotate-summary', diff --git a/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php index 7fed9b1..f653917 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -24,11 +24,76 @@ class ScaleAndCropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->scaleAndCrop($this->configuration['width'], $this->configuration['height'])) { + if (!$image->scaleAndCrop($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) { watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()), WATCHDOG_ERROR); return FALSE; } return TRUE; } + /** + * {@inheritdoc} + */ + public function transformDimensions(array &$dimensions) { + $width = $this->configuration['width']; + $height = $this->configuration['height']; + $image_width = $dimensions['width']; + $image_height = $dimensions['height']; + $upscale = $this->configuration['upscale']; + $scale = max($width / $image_width, $height / $image_height); + if (!$upscale && $scale >= 1) { + // When upscale is false and scale > 1, image will have original + // dimensions. + return TRUE; + } + // The new image will have the exact dimensions defined for the effect. + $dimensions['width'] = $this->configuration['width']; + $dimensions['height'] = $this->configuration['height']; + } + + /** + * {@inheritdoc} + */ + public function getSummary() { + $summary = array( + '#theme' => 'image_scale_and_crop_summary', + '#data' => $this->configuration, + ); + $summary += parent::getSummary(); + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return parent::defaultConfiguration() + array( + 'upscale' => TRUE, + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, array &$form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $form['upscale'] = array( + '#type' => 'checkbox', + '#default_value' => $this->configuration['upscale'], + '#title' => t('Allow Upscaling'), + '#description' => t('Let scale make images larger than their original size'), + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, array &$form_state) { + parent::submitConfigurationForm($form, $form_state); + + $this->configuration['upscale'] = $form_state['values']['upscale']; + } + } diff --git a/core/modules/image/src/Tests/ImageAdminStylesTest.php b/core/modules/image/src/Tests/ImageAdminStylesTest.php index 990c93f..0a2c205 100644 --- a/core/modules/image/src/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php @@ -80,6 +80,7 @@ function testStyle() { 'image_scale_and_crop' => array( 'data[width]' => 120, 'data[height]' => 121, + 'data[upscale]' => 1, ), 'image_crop' => array( 'data[width]' => 130, diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index f2cb31a..285c890 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -55,10 +55,10 @@ function testResizeEffect() { * Test the image_scale_effect() function. */ function testScaleEffect() { - // @todo: need to test upscaling. $this->assertImageEffect('image_scale', array( 'width' => 10, 'height' => 10, + 'upscale' => TRUE, )); $this->assertToolkitOperationsCalled(array('scale')); @@ -66,6 +66,7 @@ function testScaleEffect() { $calls = $this->imageTestGetAllCalls(); $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); + $this->assertTrue($calls['scale'][0][2], 'Upscale was passed correctly'); } /** @@ -95,6 +96,7 @@ function testScaleAndCropEffect() { $this->assertImageEffect('image_scale_and_crop', array( 'width' => 5, 'height' => 10, + 'upscale' => TRUE, )); $this->assertToolkitOperationsCalled(array('scale_and_crop')); @@ -102,6 +104,7 @@ function testScaleAndCropEffect() { $calls = $this->imageTestGetAllCalls(); $this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly'); $this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly'); + $this->assertTrue($calls['scale_and_crop'][0][2], 'Upscale was passed correctly'); } /** diff --git a/core/modules/image/templates/image-scale-and-crop-summary.html.twig b/core/modules/image/templates/image-scale-and-crop-summary.html.twig new file mode 100644 index 0000000..d51e132 --- /dev/null +++ b/core/modules/image/templates/image-scale-and-crop-summary.html.twig @@ -0,0 +1,37 @@ +{# +/** + * @file + * Default theme implementation for a summary of an image scale and crop effect. + * + * Available variables: + * - data: The current configuration for this resize effect, including: + * - width: The width of the resized image. + * - height: The height of the resized image. + * - upscale: If images larger than their original size can scale. + * - effect: The effect information, including: + * - id: The effect identifier. + * - label: The effect name. + * - description: The effect description. + * + * @ingroup themeable + */ +#} +{% if data.width and data.height -%} + {{ data.width|e }}x{{ data.height|e }} +{%- else -%} + {% if data.width %} + {% trans %} + width {{ data.width|e }} + {% endtrans %} + {% elseif data.height %} + {% trans %} + height {{ data.height|e }} + {% endtrans %} + {% endif %} +{%- endif %} + +{% if data.upscale %} + {% trans %} + (upscaling allowed) + {% endtrans %} +{% endif %}