From 2bb2976ab684e73184ddbbc784ec1fd6a9377cfd Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 17 Jun 2013 22:08:50 +0200 Subject: [PATCH] Issue #1996868 by jhedstrom: Start converting image.inc to an Image component. --- core/includes/image.inc | 44 ++----------- core/lib/Drupal/Component/Image/Image.php | 64 +++++++++++++++++++ .../Tests/Component/Image/ImageTest.php} | 62 ++++++++++++------ 3 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 core/lib/Drupal/Component/Image/Image.php rename core/{modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php => tests/Drupal/Tests/Component/Image/ImageTest.php} (60%) diff --git a/core/includes/image.inc b/core/includes/image.inc index 17a73e89552..9e2d06f4e8f 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,13 @@ 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. + * @deprecated as of Drupal 8.0. Use + * \Drupal\Component\Image\Image::scaleDimensions() directly instead. * * @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::scaleDimensions($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 00000000000..635b3746076 --- /dev/null +++ b/core/lib/Drupal/Component/Image/Image.php @@ -0,0 +1,64 @@ += $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/ImageTest.php similarity index 60% rename from core/modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php rename to core/tests/Drupal/Tests/Component/Image/ImageTest.php index ec4b0f3f1a2..05d1be9e13f 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsScaleUnitTest.php +++ b/core/tests/Drupal/Tests/Component/Image/ImageTest.php @@ -2,29 +2,61 @@ /** * @file - * Definition of Drupal\image\Tests\ImageDimensionsScaleUnitTest. + * Contains \Drupal\Tests\Component\Image\ImageTest. */ -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 the Image component. + * + * @see \Drupal\Component\Image\Image */ -class ImageDimensionsScaleUnitTest extends UnitTestBase { +class ImageTest extends UnitTestCase { public static function getInfo() { return array( - 'name' => 'image_dimensions_scale()', - 'description' => 'Tests all control flow branches in image_dimensions_scale().', + 'name' => 'Tests for the Image component', + 'description' => 'Tests all control flow branches in Drupal\Component\Image\Image.', 'group' => 'Image', ); } /** * Tests all control flow branches in image_dimensions_scale(). + * + * @dataProvider providerTestScaleDimensions */ - function testImageDimensionsScale() { + function testScaleDimensions($input, $output) { + // Process the test dataset. + $return_value = Image::scaleDimensions($input['dimensions'], $input['width'], $input['height'], $input['upscale']); + + // Check the width. + $this->assertEquals($output['dimensions']['width'], $input['dimensions']['width'], sprintf('Computed width (%s) does not equal expected width (%s)', $output['dimensions']['width'], $input['dimensions']['width'])); + + // Check the height. + $this->assertEquals($output['dimensions']['height'], $input['dimensions']['height'], sprintf('Computed height (%s) does not equal expected height (%s)', $output['dimensions']['height'], $input['dimensions']['height'])); + + // Check the return value. + $this->assertEquals($output['return_value'], $return_value, 'Incorrect return value.'); + } + + /** + * Provides data for image dimension scale tests. + * + * @return array + * Keyed array containing: + * - 'input' - Array which contains input for + * Image::scaleDimensions(). + * - 'output' - Array which contains expected output after passing + * through Image::scaleDimensions. Also contains a boolean + * 'return_value' which should match the expected return value. + * + * @see testScaleDimensions() + */ + public function providerTestScaleDimensions() { // Define input / output datasets to test different branch conditions. $test = array(); @@ -134,18 +166,6 @@ class ImageDimensionsScaleUnitTest extends UnitTestBase { ), ); - 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; } }