diff --git a/core/core.services.yml b/core/core.services.yml index 6134359..e05c935 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -582,6 +582,9 @@ services: image.toolkit.manager: class: Drupal\Core\ImageToolkit\ImageToolkitManager arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@config.factory'] + image.toolkit.operation.manager: + class: Drupal\Core\ImageToolkit\ImageToolkitOperationManager + parent: default_plugin_manager image.toolkit: class: Drupal\Core\ImageToolkit\ImageToolkitInterface factory_method: getDefaultToolkit diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 136c81e..da5a660 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -283,31 +283,10 @@ protected function processInfo() { } /** - * Passes through calls that represent image toolkit operations onto the - * image toolkit. - * - * This is a temporary solution to keep patches reviewable. The __call() - * method will be replaced in https://drupal.org/node/2073759 with a new - * interface method ImageInterface::apply(). An image operation will be - * performed as in the next example: - * @code - * $image = new Image($path, $toolkit); - * $image->apply('scale', array('width' => 50, 'height' => 100)); - * @endcode - * Also in https://drupal.org/node/2073759 operation arguments sent to toolkit - * will be moved to a keyed array, unifying the interface of toolkit - * operations. - * - * @todo Drop this in https://drupal.org/node/2073759 in favor of new apply(). + * {@inheritdoc} */ - public function __call($method, $arguments) { - if (is_callable(array($this->toolkit, $method))) { - // @todo In https://drupal.org/node/2073759, call_user_func_array() will - // be replaced by $this->toolkit->apply($name, $this, $arguments). - array_unshift($arguments, $this); - return call_user_func_array(array($this->toolkit, $method), $arguments); - } - throw new \BadMethodCallException(); + public function apply($operation, array $arguments = array()) { + return $this->toolkit->apply($operation, $this, $arguments); } /** diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 411d39a..b01b093 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -144,6 +144,22 @@ public function getSource(); public function getToolkitId(); /** + * Applies a toolkit operation to the image. + * + * The operation is deferred to the active toolkit. + * + * @param string $operation + * The operation to be performed against the image. + * @param array $arguments + * An associative array with required arguments to be passed to the toolkit + * operation. E.g. array('width' => 50, 'height' => 100, 'upscale' => TRUE). + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function apply($operation, array $arguments = array()); + + /** * Closes the image and saves the changes to a file. * * @param string|null $destination diff --git a/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php b/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php new file mode 100644 index 0000000..0db12e8 --- /dev/null +++ b/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php @@ -0,0 +1,78 @@ +getToolkitOperationPluginId($this->getPluginId(), $operation); + $plugin = $manager->createInstance($plugin_id, array(), $this); + + // Validate and prepare arguments before applying the operation. The + // operation might be cancelled if prepare() decides that the final image + // will not change. + if ($plugin->prepare($image, $arguments) !== FALSE) { + return $plugin->apply($image, $arguments); + } + + // No operation is required because the target image will be the same as the + // original but the result is still successful. + return TRUE; + } + } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index a5f96f3..243c527 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -60,78 +60,6 @@ public function settingsForm(); public function settingsFormSubmit($form, &$form_state); /** - * Scales an image to the specified size. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource, $image->info['width'], and - * $image->info['height'] values will be modified by this call. - * @param int $width - * The new width of the resized image, in pixels. - * @param int $height - * The new height of the resized image, in pixels. - * - * @return bool - * TRUE or FALSE, based on success. - */ - public function resize(ImageInterface $image, $width, $height); - - /** - * Rotates an image the given number of degrees. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource, $image->info['width'], and - * $image->info['height'] values will be modified by this call. - * @param int $degrees - * The number of (clockwise) degrees to rotate the image. - * @param string $background - * (optional) An hexadecimal integer specifying the background color to use - * for the uncovered area of the image after the rotation. E.g. 0x000000 for - * black, 0xff00ff for magenta, and 0xffffff for white. For images that - * support transparency, this will default to transparent. Otherwise it will - * be white. - * - * @return bool - * TRUE or FALSE, based on success. - */ - public function rotate(ImageInterface $image, $degrees, $background = NULL); - - /** - * Crops an image. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource, $image->info['width'], and - * $image->info['height'] values will be modified by this call. - * @param int $x - * The starting x offset at which to start the crop, in pixels. - * @param int $y - * The starting y offset at which to start the crop, in pixels. - * @param int $width - * The width of the cropped area, in pixels. - * @param int $height - * The height of the cropped area, in pixels. - * - * @return bool - * TRUE or FALSE, based on success. - * - * @see image_crop() - */ - public function crop(ImageInterface $image, $x, $y, $width, $height); - - /** - * Converts an image resource to grayscale. - * - * Note that transparent GIFs loose transparency when desaturated. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource value will be modified by this - * call. - * - * @return bool - * TRUE or FALSE, based on success. - */ - public function desaturate(ImageInterface $image); - - /** * Creates an image resource from a file. * * @param \Drupal\Core\Image\ImageInterface $image @@ -156,49 +84,6 @@ public function load(ImageInterface $image); public function save(ImageInterface $image, $destination); /** - * Scales an image while maintaining aspect ratio. - * - * The resulting image can be smaller for one or both target dimensions. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * @param int $width - * (optional) The target width, in pixels. This value is omitted then the - * scaling will based only on the height value. - * @param int $height - * (optional) The target height, in pixels. This value is omitted then the - * scaling will based only on the width value. - * @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 scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE); - - /** - * Scales an image to the exact width and height given. - * - * This function achieves the target aspect ratio by cropping the original - * image equally on both sides, or equally on the top and bottom. This - * function is useful to create uniform sized avatars from larger images. - * - * The resulting image always has the exact target dimensions. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - */ - public function scaleAndCrop(ImageInterface $image, $width, $height); - - /** * Gets details about an image. * * @param \Drupal\Core\Image\ImageInterface $image @@ -247,4 +132,20 @@ public static function isAvailable(); */ public static function supportedTypes(); + /** + * Applies a toolkit specific operation to an image. + * + * @param string $operation + * The toolkit operation to be processed. + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $arguments + * An associative array with required arguments to be passed to the toolkit + * operation. E.g. array('width' => 50, 'height' => 100, 'upscale' => TRUE). + * + * @return mixed + * TRUE if the operation was performed successfully, FALSE otherwise. + */ + public function apply($operation, ImageInterface $image, array $arguments = array()); + } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php index 43686ff..6c76521 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\Component\Plugin\Factory\DefaultFactory; /** * Manages toolkit plugins. @@ -91,4 +92,14 @@ public function getAvailableToolkits() { return $output; } + + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array()) { + $plugin_definition = $this->getDefinition($plugin_id); + $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); + return new $plugin_class($configuration, $plugin_id, $plugin_definition); + } + } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php new file mode 100644 index 0000000..a4c6fce --- /dev/null +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -0,0 +1,92 @@ +toolkit = $toolkit; + } + + /** + * {@inheritdoc} + */ + public function prepare(ImageInterface $image, array &$arguments) { + $manager = \Drupal::typedDataManager(); + + // Get arguments definition. + $definition = $this->getPluginDefinition(); + $definition = $definition['arguments']; + + foreach ($definition as $id => $argument) { + + // The defined argument has been provided. + if (array_key_exists($id, $arguments)) { + $data_definition = DataDefinition::create($argument['type']); + $typed_data = $manager->create($data_definition, $arguments[$id]); + $violations = $typed_data->validate(); + + // There are violations against defined TypedData. + if ($violations->count()) { + $fail = TRUE; + + // One more try: Check if casted value passes. + if (method_exists($typed_data, 'getCastedValue')) { + $casted = $typed_data->getCastedValue(); + $typed_data->setValue($casted); + $fail = (bool) $typed_data->validate()->count(); + } + + if ($fail) { + $message_arguments = array('@value' => $arguments[$id], '@plugin' => $this->getPluginId(), '@arg' => $id, '@type' => $argument['type']); + $message = String::format("The value '@value' provided to '@plugin' plugin in '@arg' argument cannot be casted as '@type'.", $message_arguments); + throw new \InvalidArgumentException($message); + } + } + } + // A defined argument hasn't been passed. + else { + if ($argument['required']) { + // If the argument is required throw an exception. + $message = String::format("Argument '@arg' expected by plugin '@plugin' but not passed.", array('@arg' => $id, '@plugin' => $this->getPluginId())); + throw new \InvalidArgumentException($message); + } + else { + // Optional argument? Assign the default value. + $arguments[$id] = $argument['default']; + } + } + } + } + +} diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php new file mode 100644 index 0000000..e0b9714 --- /dev/null +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php @@ -0,0 +1,52 @@ +alterInfo($module_handler, 'image_toolkit_operation'); + $this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operation'); + } + + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + + // Get the toolkit ID from the image operation plugin class namespace. Each + // plugin is placed under Plugin\ImageToolkit\{plugin_id}. + $path = explode('\\', $definition['class']); + $definition['toolkit'] = $path[count($path) - 2]; + + foreach ($definition['arguments'] as $id => &$argument) { + // Set argument 'required' as TRUE if hasn't been specified in annotation. + $argument['required'] = isset($argument['required']) ? $argument['required'] : TRUE; + + // If the argument is optional a 'default' should be provided. + if (!$argument['required'] && !array_key_exists('default', $argument)) { + $message = String::format("Argument '@arg' in plugin '@plugin' is optional but doesn't provide a default value.", array('@arg' => $id, '@plugin' => $definition['id'])); + throw new PluginException($message); + } + } + } + + /** + * Returns the plugin ID for a given toolkit and operation. + * + * @param string $toolkit + * The toolkit ID. + * @param string $operation + * The operation (e.g. "crop"). + * + * @return string + * The plugin ID. + * + * @throws \Drupal\Component\Plugin\Exception\UnknownPluginException + */ + public function getToolkitOperationPluginId($toolkit, $operation) { + $definitions = $this->getDefinitions(); + + $definitions = array_filter($definitions, + function ($definition) use ($toolkit, $operation) { + return $definition['toolkit'] == $toolkit && $definition['operation'] == $operation; + } + ); + + if (!$definitions) { + $message = String::format("No image operation plugin for '@toolkit' toolkit and '@operation' operation.", array('@toolkit' => $toolkit, '@operation' => $operation)); + throw new UnknownPluginException('', $message); + } + else { + // Pickup the first plugin found. + // @todo In https://drupal.org/node/2110591 we'll return here the UI + // selected plugin or the first found if missed. + $definition = reset($definitions); + return $definition['id']; + } + } + + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array(), ImageToolkitInterface $toolkit = NULL, TypedDataManager $typed_data_manager = NULL) { + $plugin_definition = $this->getDefinition($plugin_id); + $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); + return new $plugin_class($configuration, $plugin_id, $plugin_definition, $toolkit); + } + +} diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 892b214..32df940 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -454,10 +454,14 @@ function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $mi // Try to resize the image to fit the dimensions. $image = $image_factory->get($file->getFileUri()); if ($image->getResource()) { - $image->scale($width, $height); - $image->save(); - $file->filesize = $image->getFileSize(); - drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); + if ($image->apply('scale', array('width' => $width, 'height' => $height))) { + $image->save(); + $file->filesize = $image->getFileSize(); + drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); + } + else { + $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); + } } else { $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions)); diff --git a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php index cfde9a3..85b15d6 100644 --- a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php @@ -91,6 +91,13 @@ function testFileValidateImageResolution() { $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File'); $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File'); + // Once again, now with negative width and height to force an error. + copy('core/misc/druplicon.png', 'temporary://druplicon.png'); + $this->image->setFileUri('temporary://druplicon.png'); + + $errors = file_validate_image_resolution($this->image, '-10x-5'); + $this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File'); + drupal_unlink('temporary://druplicon.png'); } else { diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php index 7996d04..2b1d8ce 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php @@ -27,7 +27,8 @@ public function applyEffect(ImageInterface $image) { list($x, $y) = explode('-', $this->configuration['anchor']); $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']); $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']); - if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) { + $data = array('x' => $x, 'y' => $y, 'width' => $this->configuration['width'], 'height' => $this->configuration['height']); + if (!$image->apply('crop', $data)) { watchdog('image', 'Image 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; } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php index 4d899c6..62311f3 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php @@ -31,7 +31,7 @@ public function transformDimensions(array &$dimensions) { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->desaturate()) { + if (!$image->apply('desaturate')) { watchdog('image', 'Image desaturate 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; } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php index da9b176..6c02ee1 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php @@ -26,7 +26,8 @@ class ResizeImageEffect extends ImageEffectBase implements ConfigurableImageEffe * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->resize($this->configuration['width'], $this->configuration['height'])) { + $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height']); + if (!$image->apply('resize', $data)) { watchdog('image', 'Image resize 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; } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php index b0e6bf3..d14d260 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php @@ -45,7 +45,8 @@ public function applyEffect(ImageInterface $image) { $this->configuration['degrees'] = rand(-1 * $degrees, $degrees); } - if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) { + $data = array('degrees' => $this->configuration['degrees'], 'background' => $this->configuration['bgcolor']); + if (!$image->apply('rotate', $data)) { watchdog('image', 'Image rotate 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; } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php index 7fed9b1..4aa77f4 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -24,7 +24,8 @@ class ScaleAndCropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->scaleAndCrop($this->configuration['width'], $this->configuration['height'])) { + $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height']); + if (!$image->apply('scale_and_crop', $data)) { 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; } 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 4316bd0..a8b0565 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php @@ -25,7 +25,8 @@ class ScaleImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->scale($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) { + $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height'], 'upscale' => $this->configuration['upscale']); + if (!$image->apply('scale', $data)) { watchdog('image', 'Image scale 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; } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php index 8afcb22..4c692fe 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php @@ -103,12 +103,12 @@ function testScaleAndCropEffect() { 'width' => 5, 'height' => 10, )); - $this->assertToolkitOperationsCalled(array('scaleAndCrop')); + $this->assertToolkitOperationsCalled(array('scale_and_crop')); // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][1], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][2], 10, 'Height was computed and passed correctly'); } /** diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php index e5c5528..bb79eec 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -2,14 +2,13 @@ /** * @file - * Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit;. + * Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit. */ namespace Drupal\system\Plugin\ImageToolkit; use Drupal\Core\Image\ImageInterface; use Drupal\Core\ImageToolkit\ImageToolkitBase; -use Drupal\Component\Utility\Image as ImageUtility; /** * Defines the GD2 toolkit for image manipulation within Drupal. @@ -49,158 +48,6 @@ public function settingsFormSubmit($form, &$form_state) { /** * {@inheritdoc} */ - public function resize(ImageInterface $image, $width, $height) { - // @todo Dimensions computation will be moved into a dedicated functionality - // in https://drupal.org/node/2108307. - $width = (int) round($width); - $height = (int) round($height); - - $res = $this->createTmp($image, $width, $height); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) { - return FALSE; - } - - imagedestroy($image->getResource()); - // Update image object. - $image - ->setResource($res) - ->setWidth($width) - ->setHeight($height); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function rotate(ImageInterface $image, $degrees, $background = NULL) { - // PHP installations using non-bundled GD do not have imagerotate. - if (!function_exists('imagerotate')) { - watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->getSource())); - return FALSE; - } - - // Convert the hexadecimal background value to a color index value. - if (isset($background)) { - $rgb = array(); - for ($i = 16; $i >= 0; $i -= 8) { - $rgb[] = (($background >> $i) & 0xFF); - } - $background = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); - } - // Set the background color as transparent if $background is NULL. - else { - // Get the current transparent color. - $background = imagecolortransparent($image->getResource()); - - // If no transparent colors, use white. - if ($background == 0) { - $background = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); - } - } - - // Images are assigned a new color palette when rotating, removing any - // transparency flags. For GIF images, keep a record of the transparent color. - if ($image->getType() == IMAGETYPE_GIF) { - $transparent_index = imagecolortransparent($image->getResource()); - if ($transparent_index != 0) { - $transparent_gif_color = imagecolorsforindex($image->getResource(), $transparent_index); - } - } - - $image->setResource(imagerotate($image->getResource(), 360 - $degrees, $background)); - - // GIFs need to reassign the transparent color after performing the rotate. - if (isset($transparent_gif_color)) { - $background = imagecolorexactalpha($image->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); - imagecolortransparent($image->getResource(), $background); - } - - $image - ->setWidth(imagesx($image->getResource())) - ->setHeight(imagesy($image->getResource())); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { - // @todo Dimensions computation will be moved into a dedicated functionality - // in https://drupal.org/node/2108307. - $aspect = $image->getHeight() / $image->getWidth(); - $height = empty($height) ? $width * $aspect : $height; - $width = empty($width) ? $height / $aspect : $width; - $width = (int) round($width); - $height = (int) round($height); - - $res = $this->createTmp($image, $width, $height); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, $x, $y, $width, $height, $width, $height)) { - return FALSE; - } - - // Destroy the original image and return the modified image. - imagedestroy($image->getResource()); - $image - ->setResource($res) - ->setWidth($width) - ->setHeight($height); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function desaturate(ImageInterface $image) { - // PHP installations using non-bundled GD do not have imagefilter. - if (!function_exists('imagefilter')) { - watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->getSource())); - return FALSE; - } - - return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); - } - - /** - * {@inheritdoc} - */ - public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { - // @todo Dimensions computation will be moved into a dedicated functionality - // in https://drupal.org/node/2108307. - $dimensions = array( - 'width' => $image->getWidth(), - 'height' => $image->getHeight(), - ); - - // Scale the dimensions - if they don't change then just return success. - if (!ImageUtility::scaleDimensions($dimensions, $width, $height, $upscale)) { - return TRUE; - } - - return $this->resize($image, $dimensions['width'], $dimensions['height']); - } - - /** - * {@inheritdoc} - */ - public function scaleAndCrop(ImageInterface $image, $width, $height) { - // @todo Dimensions computation will be moved into a dedicated functionality - // in https://drupal.org/node/2108307. - $scale = max($width / $image->getWidth(), $height / $image->getHeight()); - $x = ($image->getWidth() * $scale - $width) / 2; - $y = ($image->getHeight() * $scale - $height) / 2; - - if ($this->resize($image, $image->getWidth() * $scale, $image->getHeight() * $scale)) { - return $this->crop($image, $x, $y, $width, $height); - } - - return FALSE; - } - - /** - * {@inheritdoc} - */ public function load(ImageInterface $image) { $function = 'imagecreatefrom' . image_type_to_extension($image->getType(), FALSE); if (function_exists($function) && $resource = $function($image->getSource())) { diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Crop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Crop.php new file mode 100644 index 0000000..4a28628 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -0,0 +1,74 @@ +toolkit->createTmp($image, $arguments['width'], $arguments['height']); + + if (!imagecopyresampled($res, $image->getResource(), 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) { + return FALSE; + } + + // Destroy the original image and return the modified image. + imagedestroy($image->getResource()); + $image + ->setResource($res) + ->setWidth($arguments['width']) + ->setHeight($arguments['height']); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Desaturate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Desaturate.php new file mode 100644 index 0000000..8c321df --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Desaturate.php @@ -0,0 +1,39 @@ + $image->getSource())); + return FALSE; + } + + return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php new file mode 100644 index 0000000..a4ea521 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -0,0 +1,65 @@ +toolkit->createTmp($image, $arguments['width'], $arguments['height']); + + if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $arguments['width'], $arguments['height'], $image->getWidth(), $image->getHeight())) { + return FALSE; + } + + imagedestroy($image->getResource()); + // Update image object. + $image + ->setResource($res) + ->setWidth($arguments['width']) + ->setHeight($arguments['height']); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Rotate.php new file mode 100644 index 0000000..560a7a1 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -0,0 +1,89 @@ + $image->getSource())); + return FALSE; + } + + // Convert the hexadecimal background value to a color index value. + if (!empty($arguments['background'])) { + $rgb = array(); + for ($i = 16; $i >= 0; $i -= 8) { + $rgb[] = (($arguments['background'] >> $i) & 0xFF); + } + $arguments['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); + } + // Set background color as transparent if $arguments['background'] is NULL. + else { + // Get the current transparent color. + $arguments['background'] = imagecolortransparent($image->getResource()); + + // If no transparent colors, use white. + if ($arguments['background'] == 0) { + $arguments['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); + } + } + + // Images are assigned a new color palette when rotating, removing any + // transparency flags. For GIF images, keep a record of the transparent color. + if ($image->getType() == IMAGETYPE_GIF) { + $transparent_index = imagecolortransparent($image->getResource()); + if ($transparent_index != 0) { + $transparent_gif_color = imagecolorsforindex($image->getResource(), $transparent_index); + } + } + + $image->setResource(imagerotate($image->getResource(), 360 - $arguments['degrees'], $arguments['background'])); + + // GIFs need to reassign the transparent color after performing the rotate. + if (isset($transparent_gif_color)) { + $arguments['background'] = imagecolorexactalpha($image->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); + imagecolortransparent($image->getResource(), $arguments['background']); + } + + $image + ->setWidth(imagesx($image->getResource())) + ->setHeight(imagesy($image->getResource())); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Scale.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Scale.php new file mode 100644 index 0000000..fe414b1 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Scale.php @@ -0,0 +1,80 @@ + $this->getPluginId())); + throw new \InvalidArgumentException($message); + } + + // 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. + $scale = $image->getHeight() / $image->getWidth(); + if (($width && !$height) || ($width && $height && $scale < $height / $width)) { + $height = (int) round($width * $scale); + } + else { + $width = (int) round($height / $scale); + } + + // Call parent validation + parent::prepare($image, $arguments); + + // Don't upscale if the option isn't enabled. + if (!$arguments['upscale'] && ($width >= $image->getWidth() || $height >= $image->getHeight())) { + return FALSE; + } + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php new file mode 100644 index 0000000..7ce04e9 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php @@ -0,0 +1,65 @@ +getWidth(); + $height = $image->getHeight(); + + $scale = max($arguments['width'] / $width, $arguments['height'] / $height); + + $arguments['x'] = (int) round(($width * $scale - $arguments['width']) / 2); + $arguments['y'] = (int) round(($height * $scale - $arguments['height']) / 2); + $arguments['resize'] = array( + 'width' => (int) round($width * $scale), + 'height' => (int) round($height * $scale), + ); + } + + /** + * {@inheritdoc} + */ + public function apply(ImageInterface $image, array $arguments = array()) { + if ($this->toolkit->apply('resize', $image, $arguments['resize'])) { + return $this->toolkit->apply('crop', $image, $arguments); + } + + return FALSE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php index 805ee22..76c2de4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php @@ -106,49 +106,49 @@ function testManipulations() { $operations = array( 'resize' => array( 'function' => 'resize', - 'arguments' => array(20, 10), + 'arguments' => array('width' => 20, 'height' => 10), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'scale_x' => array( 'function' => 'scale', - 'arguments' => array(20, NULL), + 'arguments' => array('width' => 20), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'scale_y' => array( 'function' => 'scale', - 'arguments' => array(NULL, 10), + 'arguments' => array('height' => 10), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'upscale_x' => array( 'function' => 'scale', - 'arguments' => array(80, NULL, TRUE), + 'arguments' => array('width' => 80, 'upscale' => TRUE), 'width' => 80, 'height' => 40, 'corners' => $default_corners, ), 'upscale_y' => array( 'function' => 'scale', - 'arguments' => array(NULL, 40, TRUE), + 'arguments' => array('height' => 40, 'upscale' => TRUE), 'width' => 80, 'height' => 40, 'corners' => $default_corners, ), 'crop' => array( 'function' => 'crop', - 'arguments' => array(12, 4, 16, 12), + 'arguments' => array('x' => 12, 'y' => 4, 'width' => 16, 'height' => 12), 'width' => 16, 'height' => 12, 'corners' => array_fill(0, 4, $this->white), ), 'scale_and_crop' => array( - 'function' => 'scaleAndCrop', - 'arguments' => array(10, 8), + 'function' => 'scale_and_crop', + 'arguments' => array('width' => 10, 'height' => 8), 'width' => 10, 'height' => 8, 'corners' => array_fill(0, 4, $this->black), @@ -160,28 +160,28 @@ function testManipulations() { $operations += array( 'rotate_5' => array( 'function' => 'rotate', - 'arguments' => array(5, 0xFF00FF), // Fuchsia background. + 'arguments' => array('degrees' => 5, 'background' => 0xFF00FF), // Fuchsia background. 'width' => 42, 'height' => 24, 'corners' => array_fill(0, 4, $this->fuchsia), ), 'rotate_90' => array( 'function' => 'rotate', - 'arguments' => array(90, 0xFF00FF), // Fuchsia background. + 'arguments' => array('degrees' => 90, 'background' => 0xFF00FF), // Fuchsia background. 'width' => 20, 'height' => 40, 'corners' => array($this->transparent, $this->red, $this->green, $this->blue), ), 'rotate_transparent_5' => array( 'function' => 'rotate', - 'arguments' => array(5), + 'arguments' => array('degrees' => 5), 'width' => 42, 'height' => 24, 'corners' => array_fill(0, 4, $this->transparent), ), 'rotate_transparent_90' => array( 'function' => 'rotate', - 'arguments' => array(90), + 'arguments' => array('degrees' => 90), 'width' => 20, 'height' => 40, 'corners' => array($this->transparent, $this->red, $this->green, $this->blue), @@ -233,7 +233,7 @@ function testManipulations() { } // Perform our operation. - call_user_func_array(array($image, $values['function']), $values['arguments']); + $image->apply($values['function'], $values['arguments']); // To keep from flooding the test with assert values, make a general // value for whether each group of values fail. diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php index b9e0d32..c580421 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php @@ -53,7 +53,8 @@ function testSave() { * Test the image_resize() function. */ function testResize() { - $this->assertTrue($this->image->resize(1, 2), 'Function returned the expected value.'); + $data = array('width' => 1, 'height' => 2); + $this->assertTrue($this->image->apply('resize', $data), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('resize')); // Check the parameters. @@ -67,7 +68,8 @@ function testResize() { */ function testScale() { // TODO: need to test upscaling - $this->assertTrue($this->image->scale(10, 10), 'Function returned the expected value.'); + $data = array('width' => 10, 'height' => 10); + $this->assertTrue($this->image->apply('scale', $data), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('scale')); // Check the parameters. @@ -80,21 +82,23 @@ function testScale() { * Test the image_scale_and_crop() function. */ function testScaleAndCrop() { - $this->assertTrue($this->image->scaleAndCrop(5, 10), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('scaleAndCrop')); + $data = array('width' => 5, 'height' => 10); + $this->assertTrue($this->image->apply('scale_and_crop', $data), 'Function returned the expected value.'); + $this->assertToolkitOperationsCalled(array('scale_and_crop')); // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][1], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][2], 10, 'Height was computed and passed correctly'); } /** * Test the image_rotate() function. */ function testRotate() { - $this->assertTrue($this->image->rotate(90, 1), 'Function returned the expected value.'); + $data = array('degrees' => 90, 'background' => 1); + $this->assertTrue($this->image->apply('rotate', $data), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('rotate')); // Check the parameters. @@ -107,7 +111,8 @@ function testRotate() { * Test the image_crop() function. */ function testCrop() { - $this->assertTrue($this->image->crop(1, 2, 3, 4), 'Function returned the expected value.'); + $data = array('x' => 1, 'y' => 2, 'width' => 3, 'height' => 4); + $this->assertTrue($this->image->apply('crop', $data), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('crop')); // Check the parameters. @@ -122,7 +127,7 @@ function testCrop() { * Test the image_desaturate() function. */ function testDesaturate() { - $this->assertTrue($this->image->desaturate(), 'Function returned the expected value.'); + $this->assertTrue($this->image->apply('desaturate'), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('desaturate')); // Check the parameters. diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php index ce328aa..42e32e4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php @@ -119,6 +119,8 @@ function imageTestReset() { 'rotate' => array(), 'crop' => array(), 'desaturate' => array(), + 'scale' => array(), + 'scale_and_crop' => array(), ); \Drupal::state()->set('image_test.results', $results); } diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Crop.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Crop.php new file mode 100644 index 0000000..6e87309 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Crop.php @@ -0,0 +1,51 @@ +toolkit->logCall('crop', array($image, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Desaturate.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Desaturate.php new file mode 100644 index 0000000..e101965 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Desaturate.php @@ -0,0 +1,34 @@ +toolkit->logCall('desaturate', array($image)); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Resize.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Resize.php new file mode 100644 index 0000000..4c56480 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Resize.php @@ -0,0 +1,43 @@ +toolkit->logCall('resize', array($image, $arguments['width'], $arguments['height'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Rotate.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Rotate.php new file mode 100644 index 0000000..e2fb41a --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Rotate.php @@ -0,0 +1,45 @@ +toolkit->logCall('rotate', array($image, $arguments['degrees'], $arguments['background'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Scale.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Scale.php new file mode 100644 index 0000000..f6cd0c1 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/Scale.php @@ -0,0 +1,52 @@ +toolkit->logCall('scale', array($image, $arguments['width'], $arguments['height'], $arguments['upscale'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php new file mode 100644 index 0000000..f0c9a6c --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php @@ -0,0 +1,42 @@ +toolkit->logCall('scale_and_crop', array($image, $arguments['width'], $arguments['height'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php index 5d62a82..c812e9b 100644 --- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php @@ -85,54 +85,6 @@ public function save(ImageInterface $image, $destination) { } /** - * {@inheritdoc} - */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { - $this->logCall('crop', array($image, $x, $y, $width, $height)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function resize(ImageInterface $image, $width, $height) { - $this->logCall('resize', array($image, $width, $height)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function rotate(ImageInterface $image, $degrees, $background = NULL) { - $this->logCall('rotate', array($image, $degrees, $background)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function desaturate(ImageInterface $image) { - $this->logCall('desaturate', array($image)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { - $this->logCall('scale', array($image, $width, $height, $upscale)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function scaleAndCrop(ImageInterface $image, $width, $height) { - $this->logCall('scaleAndCrop', array($image, $width, $height)); - return TRUE; - } - - /** * Stores the values passed to a toolkit call. * * @param string $op @@ -144,7 +96,7 @@ public function scaleAndCrop(ImageInterface $image, $width, $height) { * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset() * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls() */ - protected function logCall($op, $args) { + public function logCall($op, $args) { $results = \Drupal::state()->get('image_test.results') ?: array(); $results[$op][] = $args; \Drupal::state()->set('image_test.results', $results); diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 728af5c..ec0c26d 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -16,6 +16,13 @@ class ImageTest extends UnitTestCase { /** + * Image source path. + * + * @var string + */ + protected $source; + + /** * Image object. * * @var \Drupal\Core\Image\Image @@ -239,8 +246,9 @@ public function testScaleWidth() { $toolkit->expects($this->any()) ->method('resize') - ->will($this->returnArgument(2)); - $height = $image->scale(44); + ->will($this->returnArgument(1)); + $ret = $image->apply('scale', array('width' => 44)); + $height = $ret['height']; $this->assertEquals($height, 50); } @@ -254,7 +262,8 @@ public function testScaleHeight() { $toolkit->expects($this->any()) ->method('resize') ->will($this->returnArgument(1)); - $width = $image->scale(NULL, 50); + $ret = $image->apply('scale', array('height' => 50)); + $width = $ret['width']; $this->assertEquals($width, 44); } @@ -267,11 +276,9 @@ public function testScaleSame() { // Dimensions are the same, resize should not be called. $toolkit->expects($this->never()) - ->method('resize') - ->will($this->returnArgument(1)); + ->method('resize'); - $width = $image->scale(88, 100); - $this->assertEquals($width, 88); + $image->apply('scale', array('width' => 88, 'height' => 100)); } /** @@ -289,7 +296,8 @@ public function testScaleAndCropWidth() { ->method('crop') ->will($this->returnArgument(1)); - $x = $image->scaleAndCrop(34, 50); + $ret = $image->apply('scaleAndCrop', array ('width' => 34, 'height' => 50)); + $x = $ret['x']; $this->assertEquals($x, 5); } @@ -306,9 +314,10 @@ public function testScaleAndCropHeight() { $toolkit->expects($this->once()) ->method('crop') - ->will($this->returnArgument(2)); + ->will($this->returnArgument(1)); - $y = $image->scaleAndCrop(44, 40); + $ret = $image->apply('scaleAndCrop', array ('width' => 44, 'height' => 40)); + $y = $ret['y']; $this->assertEquals($y, 5); } @@ -319,55 +328,88 @@ public function testScaleAndCropFails() { $toolkit = $this->getToolkitMock(array('resize', 'crop')); $image = new Image($this->source, $toolkit); - $toolkit->expects($this->once()) + $toolkit->expects($this->any()) ->method('resize') ->will($this->returnValue(FALSE)); $toolkit->expects($this->never()) ->method('crop'); - $image->scaleAndCrop(44, 40); + $image->apply('scaleAndCrop', array('width' => 44, 'height' => 40)); } /** * Tests \Drupal\Core\Image\Image::crop(). - * - * @todo Because \Drupal\Tests\Core\Image\ImageTest::testCropWidth() tests - * image geometry conversions (like dimensions, coordinates, etc) and has - * lost its scope in https://drupal.org/node/2103635, it was temporarily - * removed. The test will be added back when implementing the dedicated - * functionality from https://drupal.org/node/2108307. */ + public function testCropWidth() { + $toolkit = $this->getToolkitMock(array('getCroppedResource')); + $image = new Image($this->source, $toolkit); + + $toolkit->expects($this->once()) + ->method('getCroppedResource') + ->will($this->returnArgument(4)); + // Cropping with width only should preserve the aspect ratio. + $height = $image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44)); + $this->assertEquals($height, 50); + } /** * Tests \Drupal\Core\Image\Image::crop(). - * - * @todo Because \Drupal\Tests\Core\Image\ImageTest::testCropHeight() tests - * image geometry conversions (like dimensions, coordinates, etc) and has - * lost its scope in https://drupal.org/node/2103635, it was temporarily - * removed. The test will be added back when implementing the dedicated - * functionality from https://drupal.org/node/2108307. */ + public function testCropHeight() { + $toolkit = $this->getToolkitMock(array('getCroppedResource')); + $image = new Image($this->source, $toolkit); + + $toolkit->expects($this->once()) + ->method('getCroppedResource') + ->will($this->returnArgument(3)); + // Cropping with height only should preserve the aspect ratio. + $width = $image->apply('crop', array ('x' => 0, 'y' => 0, 'height' => 50)); + $this->assertEquals($width, 44); + } /** * Tests \Drupal\Core\Image\Image::crop(). */ public function testCrop() { - $this->toolkit->expects($this->once()) - ->method('crop') + $toolkit = $this->getToolkitMock(array('getCroppedResource')); + $image = new Image($this->source, $toolkit); + + $toolkit->expects($this->once()) + ->method('getCroppedResource') ->will($this->returnArgument(3)); - $width = $this->image->crop(0, 0, 44, 50); + $width = $image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44, 'height' => 50)); $this->assertEquals($width, 44); } /** * Tests \Drupal\Core\Image\Image::resize(). - * - * @todo Because \Drupal\Tests\Core\Image\ImageTest::testResize() tests image - * geometry conversions (like dimensions, coordinates, etc) and has lost its - * scope in https://drupal.org/node/2103635, it was temporarily removed. The - * test will be added back when implementing the dedicated functionality - * from https://drupal.org/node/2108307. */ + public function testResize() { + $toolkit = $this->getToolkitMock(array('getResizedResource')); + $image = new Image($this->source, $toolkit); + + $toolkit->expects($this->once()) + ->method('getResizedResource') + ->will($this->returnArgument(1)); + // Resize with integer for width and height. + $image->apply('resize', array('width' => 30, 'height' => 40)); + } + + /** + * Tests \Drupal\Core\Image\Image::resize(). + */ + public function testFloatResize() { + $toolkit = $this->getToolkitMock(array('getResizedResource')); + $image = new Image($this->source, $toolkit); + + $toolkit->expects($this->once()) + ->method('getResizedResource') + ->will($this->returnArgument(1)); + // Pass a float for width. + $width = $image->apply('resize', array('width' => 30.4, 'height' => 40)); + // Ensure that the float was rounded to an integer first. + $this->assertEquals($width, 30); + } /** * Tests \Drupal\Core\Image\Image::desaturate(). @@ -375,7 +417,7 @@ public function testCrop() { public function testDesaturate() { $this->toolkit->expects($this->once()) ->method('desaturate'); - $this->image->desaturate(); + $this->image->apply('desaturate'); } /** @@ -384,7 +426,7 @@ public function testDesaturate() { public function testRotate() { $this->toolkit->expects($this->once()) ->method('rotate'); - $this->image->rotate(90); + $this->image->apply('rotate', array('degrees' => 90)); } }