diff --git a/core/includes/image.inc b/core/includes/image.inc index f0b91bf..424525d 100644 --- a/core/includes/image.inc +++ b/core/includes/image.inc @@ -158,6 +158,8 @@ function image_get_info($filepath, $toolkit = FALSE) { * The target width, in pixels. * @param $height * The target height, in pixels. + *@param $upscale + * An optional boolean to allow upscale resize. Default TRUE. * * @return * TRUE on success, FALSE on failure. @@ -166,8 +168,13 @@ function image_get_info($filepath, $toolkit = FALSE) { * @see image_resize() * @see image_crop() */ -function image_scale_and_crop(stdClass $image, $width, $height) { +function image_scale_and_crop(stdClass $image, $width, $height, $upscale = TRUE) { $scale = max($width / $image->info['width'], $height / $image->info['height']); + if (!$upscale && $scale >= 1) { + $width = $width / $scale; + $height = $height / $scale; + $scale = 1; + } $x = ($image->info['width'] * $scale - $width) / 2; $y = ($image->info['height'] * $scale - $height) / 2; diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index 933a4ad..d20a536 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -500,7 +500,28 @@ function image_scale_form($data) { $form['upscale'] = array( '#type' => 'checkbox', '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : 0, - '#title' => t('Allow Upscaling'), + '#title' => t('Allow upscaling'), + '#description' => t('Let scale make images larger than their original size'), + ); + return $form; +} + +/** + * Form structure for the image scale and crop form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the scale and crop options. Therefore it does not need + * to include metadata about the effect, nor a submit button. + * + * @param $data + * The current configuration for this scale and crop effect. + */ +function image_scale_and_crop_form($data) { + $form = image_resize_form($data); + $form['upscale'] = array( + '#type' => 'checkbox', + '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : TRUE, + '#title' => t('Allow upscaling'), '#description' => t('Let scale make images larger than their original size'), ); return $form; @@ -824,6 +845,23 @@ function theme_image_scale_summary($variables) { } /** + * Returns HTML for a summary of an image scale and crop effect. + * + * @param $variables + * An associative array containing: + * - data: The current configuration for this scale and crop effect. + * + * @ingroup themeable + */ +function theme_image_scale_and_crop_summary($variables) { + $data = $variables['data']; + if (!isset($data['upscale'])) { + $data['upscale'] = TRUE; + } + return theme('image_resize_summary', array('data' => $data)) . ' ' . ($data['upscale'] ? '(' . t('upscaling allowed') . ')' : ''); +} + +/** * Returns HTML for a summary of an image crop effect. * * @param $variables diff --git a/core/modules/image/image.effects.inc b/core/modules/image/image.effects.inc index a56a429..7aade2e 100644 --- a/core/modules/image/image.effects.inc +++ b/core/modules/image/image.effects.inc @@ -15,8 +15,8 @@ function image_image_effect_info() { 'help' => t('Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.'), 'effect callback' => 'image_resize_effect', 'dimensions callback' => 'image_resize_dimensions', - 'form callback' => 'image_resize_form', - 'summary theme' => 'image_resize_summary', + 'form callback' => 'image_scale_and_crop_form', + 'summary theme' => 'image_scale_and_crop_summary', ), 'image_scale' => array( 'label' => t('Scale'), @@ -200,12 +200,18 @@ function image_crop_effect($image, $data) { * with the following items: * - "width": An integer representing the desired width in pixels. * - "height": An integer representing the desired height in pixels. + * - "upscale": A Boolean indicating that the image should be upscaled if the + * dimensions are larger than the original image. + * * @return * TRUE on success. FALSE on failure to scale and crop image. * @see image_scale_and_crop() */ function image_scale_and_crop_effect($image, $data) { - if (!image_scale_and_crop($image, $data['width'], $data['height'])) { + if (!isset($data['upscale'])) { + $data['upscale'] = TRUE; + } + if (!image_scale_and_crop($image, $data['width'], $data['height'], $data['upscale'])) { watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); return FALSE; } diff --git a/core/modules/image/image.module b/core/modules/image/image.module index c763dc6..88e9c5c 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -210,6 +210,9 @@ function image_theme() { 'image_scale_summary' => array( 'variables' => array('data' => NULL), ), + 'image_scale_and_crop_summary' => array( + 'variables' => array('data' => NULL), + ), 'image_crop_summary' => array( 'variables' => array('data' => NULL), ), diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php index 825fd38..b593a34 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php @@ -92,6 +92,7 @@ function testScaleAndCropEffect() { $this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly'); $this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly'); $this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly'); + $this->assertTrue($calls['crop'][0][5], t('Upscale was passed correctly')); } /** diff --git a/core/modules/system/tests/modules/image_test/image_test.module b/core/modules/system/tests/modules/image_test/image_test.module index de640f0..2239a87 100644 --- a/core/modules/system/tests/modules/image_test/image_test.module +++ b/core/modules/system/tests/modules/image_test/image_test.module @@ -108,8 +108,8 @@ function image_test_save(stdClass $image, $destination) { /** * Image tookit's crop operation. */ -function image_test_crop(stdClass $image, $x, $y, $width, $height) { - _image_test_log_call('crop', array($image, $x, $y, $width, $height)); +function image_test_crop(stdClass $image, $x, $y, $width, $height, $upscale = TRUE) { + _image_test_log_call('crop', array($image, $x, $y, $width, $height, $upscale)); return TRUE; }