diff --git a/core/includes/image.inc b/core/includes/image.inc index 17a73e8..d5f0391 100644 --- a/core/includes/image.inc +++ b/core/includes/image.inc @@ -6,6 +6,7 @@ */ use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\Component\Image\Image; /** * @defgroup image Image toolkits @@ -114,50 +115,11 @@ function image_scale_and_crop($image, $width, $height) { /** * Scales image dimensions while maintaining aspect ratio. * - * The resulting dimensions can be smaller for one or both target dimensions. - * - * @param array $dimensions - * Dimensions to be modified - an array with components width and height, in - * pixels. - * @param int $width - * (optional) The target width, in pixels. If this value is NULL then the - * scaling will be based only on the height value. - * @param int $height - * (optional) The target height, in pixels. If this value is NULL then the - * scaling will be based only on the width value. - * @param bool $upscale - * (optional) Boolean indicating that images smaller than the target - * dimensions will be scaled up. This generally results in a low quality - * image. - * - * @return bool - * TRUE if $dimensions was modified, FALSE otherwise. - * + * @see \Drupal\Component\Image\Image::dimensionsScale() * @see image_scale() */ function image_dimensions_scale(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) { - $aspect = $dimensions['height'] / $dimensions['width']; - - // Calculate one of the dimensions from the other target dimension, - // ensuring the same aspect ratio as the source dimensions. If one of the - // target dimensions is missing, that is the one that is calculated. If both - // are specified then the dimension calculated is the one that would not be - // calculated to be bigger than its target. - if (($width && !$height) || ($width && $height && $aspect < $height / $width)) { - $height = (int) round($width * $aspect); - } - else { - $width = (int) round($height / $aspect); - } - - // Don't upscale if the option isn't enabled. - if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) { - return FALSE; - } - - $dimensions['width'] = $width; - $dimensions['height'] = $height; - return TRUE; + return Image::dimensionsScale($dimensions, $width, $height, $upscale); } /** diff --git a/core/lib/Drupal/Component/Image/Image.php b/core/lib/Drupal/Component/Image/Image.php new file mode 100644 index 0000000..7ba4cf9 --- /dev/null +++ b/core/lib/Drupal/Component/Image/Image.php @@ -0,0 +1,63 @@ += $dimensions['width'] || $height >= $dimensions['height'])) { + return FALSE; + } + + $dimensions['width'] = $width; + $dimensions['height'] = $height; + return TRUE; + } +} diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php b/core/tests/Drupal/Tests/Component/Image/DimensionsScaleTest.php similarity index 65% rename from core/modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php rename to core/tests/Drupal/Tests/Component/Image/DimensionsScaleTest.php index ec4b0f3..2bf4bab 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php +++ b/core/tests/Drupal/Tests/Component/Image/DimensionsScaleTest.php @@ -2,29 +2,49 @@ /** * @file - * Definition of Drupal\image\Tests\ImageDimensionsScaleUnitTest. + * Definition of Drupal\Tests\Component\Image\DimensionsScaleTest. */ -namespace Drupal\image\Tests; +namespace Drupal\Tests\Component\Image; -use Drupal\simpletest\UnitTestBase; +use Drupal\Component\Image\Image; +use Drupal\Tests\UnitTestCase; /** - * Tests image_dimensions_scale(). + * Tests Drupal\Component\Image\Image::DimensionsScale(). */ -class ImageDimensionsScaleUnitTest extends UnitTestBase { +class DimensionsScaleTest extends UnitTestCase { public static function getInfo() { return array( - 'name' => 'image_dimensions_scale()', - 'description' => 'Tests all control flow branches in image_dimensions_scale().', + 'name' => 'Drupal\Component\Image\Image::imageDimensionsScale()', + 'description' => 'Tests all control flow branches in Drupal\Component\Image\Image::imageDimensionsScale() which is called via image_dimensions_scale().', 'group' => 'Image', ); } /** * Tests all control flow branches in image_dimensions_scale(). + * + * @dataProvider provider */ - function testImageDimensionsScale() { + function testDimensionsScale($input, $output) { + // Process the test dataset. + $return_value = Image::dimensionsScale($input['dimensions'], $input['width'], $input['height'], $input['upscale']); + + // Check the width. + $this->assertEquals($output['dimensions']['width'], $input['dimensions']['width'], sprintf('Computed width (%s) equals expected width (%s)', $output['dimensions']['width'], $input['dimensions']['width'])); + + // Check the height. + $this->assertEquals($output['dimensions']['height'], $input['dimensions']['height'], sprintf('Computed height (%s) equals expected height (%s)', $output['dimensions']['height'], $input['dimensions']['height'])); + + // Check the return value. + $this->assertEquals($output['return_value'], $return_value, 'Correct return value.'); + } + + /** + * Provides data for image dimension scale tests. + */ + public function provider() { // Define input / output datasets to test different branch conditions. $test = array(); @@ -134,18 +154,6 @@ function testImageDimensionsScale() { ), ); - foreach ($tests as $test) { - // Process the test dataset. - $return_value = image_dimensions_scale($test['input']['dimensions'], $test['input']['width'], $test['input']['height'], $test['input']['upscale']); - - // Check the width. - $this->assertEqual($test['output']['dimensions']['width'], $test['input']['dimensions']['width'], format_string('Computed width (@computed_width) equals expected width (@expected_width)', array('@computed_width' => $test['output']['dimensions']['width'], '@expected_width' => $test['input']['dimensions']['width']))); - - // Check the height. - $this->assertEqual($test['output']['dimensions']['height'], $test['input']['dimensions']['height'], format_string('Computed height (@computed_height) equals expected height (@expected_height)', array('@computed_height' => $test['output']['dimensions']['height'], '@expected_height' => $test['input']['dimensions']['height']))); - - // Check the return value. - $this->assertEqual($test['output']['return_value'], $return_value, 'Correct return value.'); - } + return $tests; } }