diff --git a/core/lib/Drupal/Component/Utility/Image.php b/core/lib/Drupal/Component/Utility/Image.php new file mode 100644 index 0000000..dd06806 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/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/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index db48af1..bcb9ce4 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Image; use Drupal\system\Plugin\ImageToolkitInterface; +use \Drupal\Component\Utility\Image as ImageUtility; /** * Defines an image object to represent an image file. @@ -261,7 +262,7 @@ public function scale($width = NULL, $height = NULL, $upscale = FALSE) { ); // Scale the dimensions - if they don't change then just return success. - if (!static::scaleDimensions($dimensions, $width, $height, $upscale)) { + if (!ImageUtility::scaleDimensions($dimensions, $width, $height, $upscale)) { return TRUE; } @@ -321,53 +322,4 @@ public function rotate($degrees, $background = NULL) { return $this->toolkit->rotate($this, $degrees, $background); } - /** - * 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 image_scale() - */ - public static function scaleDimensions(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; - } - } diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 6cc2113..ea6682d 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -244,30 +244,4 @@ public function desaturate(); */ public function rotate($degrees, $background = NULL); - /** - * 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 image_scale() - */ - public static function scaleDimensions(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE); - } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php index 1c314b2..c7b2f53 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Component\Utility\Image; use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; diff --git a/core/tests/Drupal/Tests/Component/Image/ImageTest.php b/core/tests/Drupal/Tests/Component/Image/ImageTest.php index a26d440..3129fa5 100644 --- a/core/tests/Drupal/Tests/Component/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Component/Image/ImageTest.php @@ -7,19 +7,19 @@ namespace Drupal\Tests\Component\Image; -use Drupal\Core\Image\Image; +use Drupal\Component\Utility\Image; use Drupal\Tests\UnitTestCase; /** * Tests the Image component. * - * @see \Drupal\Core\Image\Image + * @see \Drupal\Component\Utility\Image */ class ImageTest extends UnitTestCase { public static function getInfo() { return array( 'name' => 'Tests for the Image component', - 'description' => 'Tests all control flow branches in \Drupal\Core\Image\Image.', + 'description' => 'Tests all control flow branches in \Drupal\Component\Utility\Image.', 'group' => 'Image', ); }