diff --git a/core/core.services.yml b/core/core.services.yml index 6f8cce6..8cfa7fb 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -492,6 +492,9 @@ services: class: Drupal\system\Plugin\ImageToolkitInterface factory_method: getDefaultToolkit factory_service: image.toolkit.manager + image.factory: + class: Drupal\Core\Image\ImageFactory + arguments: ['@image.toolkit'] breadcrumb: class: Drupal\Core\Breadcrumb\BreadcrumbManager token: diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index db2552b..eaef644 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -5,6 +5,7 @@ * Contains Drupal. */ +use Drupal\system\Plugin\ImageToolkitInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -391,4 +392,18 @@ public static function languageManager() { return static::$container->get('language_manager'); } + /** + * @param string $source + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * + * @return \Drupal\Core\Image\ImageInterface + */ + public static function image($source, ImageToolkitInterface $toolkit = NULL) { + $factory = static::$container->get('image.factory'); + if ($toolkit) { + $factory->setToolkit($toolkit); + } + return $factory->get($source); + } + } diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 3d64c35..db48af1 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -17,7 +17,7 @@ * * @ingroup image */ -class Image { +class Image implements ImageInterface { /** * String specifying the path of the image file. @@ -87,16 +87,16 @@ class Image { * * @param string $source * The path to an image file. + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * The image toolkit. */ - public function __construct($source) { + public function __construct($source, ImageToolkitInterface $toolkit) { $this->source = $source; + $this->toolkit = $toolkit; } /** - * Returns the extension of the image file. - * - * @return string - * The extension of the file, or an empty string if the file is invalid. + * {@inheritdoc} */ public function getExtension() { $this->processInfo(); @@ -104,10 +104,7 @@ public function getExtension() { } /** - * Returns the height of the image file. - * - * @return int - * The height of the file, or 0 if the file is invalid. + * {@inheritdoc} */ public function getHeight() { $this->processInfo(); @@ -115,12 +112,7 @@ public function getHeight() { } /** - * Sets the height of the image file. - * - * @param int $height - * - * @return self - * Returns this image file. + * {@inheritdoc} */ public function setHeight($height) { $this->height = $height; @@ -128,10 +120,7 @@ public function setHeight($height) { } /** - * Returns the width of the image file. - * - * @return int - * The width of the file, or 0 if the file is invalid. + * {@inheritdoc} */ public function getWidth() { $this->processInfo(); @@ -139,12 +128,7 @@ public function getWidth() { } /** - * Sets the width of the image file. - * - * @param int $width - * - * @return self - * Returns this image file. + * {@inheritdoc} */ public function setWidth($width) { $this->width = $width; @@ -152,10 +136,7 @@ public function setWidth($width) { } /** - * Returns the size of the image file. - * - * @return int - * The size of the file in bytes, or 0 if the file is invalid. + * {@inheritdoc} */ public function getFileSize() { $this->processInfo(); @@ -163,10 +144,7 @@ public function getFileSize() { } /** - * Returns the MIME type of the image file. - * - * @return string - * The MIME type of the file, or an empty string if the file is invalid. + * {@inheritdoc} */ public function getMimeType() { $this->processInfo(); @@ -174,13 +152,7 @@ public function getMimeType() { } /** - * Sets the image file resource. - * - * @param resource $resource - * The image file handle. - * - * @return self - * Returns this image file. + * {@inheritdoc} */ public function setResource($resource) { $this->resource = $resource; @@ -188,37 +160,25 @@ public function setResource($resource) { } /** - * Determines if this image file has a resource set. - * - * @return bool - * TRUE if this image file has a resource set, FALSE otherwise. + * {@inheritdoc} */ public function hasResource() { return (bool) $this->resource; } /** - * Retrieves the image file resource. - * - * @return resource - * The image file handle. + * {@inheritdoc} */ public function getResource() { if (!$this->hasResource()) { $this->processInfo(); - $this->getToolkit()->load($this); + $this->toolkit->load($this); } return $this->resource; } /** - * Sets the source path of the image file. - * - * @param string $source - * A string specifying the path of the image file. - * - * @return self - * Returns this image file. + * {@inheritdoc} */ public function setSource($source) { $this->source = $source; @@ -226,71 +186,27 @@ public function setSource($source) { } /** - * Retrieves the source path of the image file. - * - * @return string - * The source path of the image file. + * {@inheritdoc} */ public function getSource() { return $this->source; } /** - * Sets a custom image toolkit. - * - * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit - * The image toolkit to use for this image file. - * - * @return self - * Returns this image file. - */ - public function setToolkit(ImageToolkitInterface $toolkit) { - $this->toolkit = $toolkit; - return $this; - } - - /** - * Returns the image toolkit being used for this image file. - * - * If a custom toolkit was not specified, this will fallback to the default. - * - * @return \Drupal\system\Plugin\ImageToolkitInterface - * The image toolkit used for this image file. - */ - protected function getToolkit() { - if (!$this->toolkit) { - $this->toolkit = \Drupal::service('image.toolkit'); - } - return $this->toolkit; - } - - /** - * Returns the ID of the image toolkit used for this image file. - * - * @return string - * The ID of the image toolkit. + * {@inheritdoc} */ public function getToolkitId() { - return $this->getToolkit()->getPluginId(); + return $this->toolkit->getPluginId(); } /** - * Closes the image and saves the changes to a file. - * - * @param string|null $destination - * (optional) Destination path where the image should be saved. If it is empty - * the original image file will be overwritten. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::save() + * {@inheritdoc} */ public function save($destination = NULL) { if (empty($destination)) { $destination = $this->getSource(); } - if ($return = $this->getToolkit()->save($this, $destination)) { + if ($return = $this->toolkit->save($this, $destination)) { // Clear the cached file size and refresh the image information. clearstatcache(TRUE, $destination); $this->setSource($destination); @@ -324,7 +240,7 @@ protected function processInfo() { return FALSE; } - if ($details = $this->getToolkit()->getInfo($this)) { + if ($details = $this->toolkit->getInfo($this)) { $this->height = $details['height']; $this->width = $details['width']; $this->extension = $details['extension']; @@ -336,22 +252,7 @@ protected function processInfo() { } /** - * Scales an image while maintaining aspect ratio. - * - * The resulting image can be smaller for one or both target dimensions. - * - * @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. + * {@inheritdoc} */ public function scale($width = NULL, $height = NULL, $upscale = FALSE) { $dimensions = array( @@ -369,21 +270,7 @@ public function scale($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 int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. + * {@inheritdoc} */ public function scaleAndCrop($width, $height) { $scale = max($width / $this->getWidth(), $height / $this->getHeight()); @@ -397,23 +284,9 @@ public function scaleAndCrop($width, $height) { } /** - * Crops an image to a rectangle specified by the given dimensions. - * - * @param int $x - * The top left coordinate, in pixels, of the crop area (x axis value). - * @param int $y - * The top left coordinate, in pixels, of the crop area (y axis value). - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::crop() + * {@inheritdoc} */ - function crop($x, $y, $width, $height) { + public function crop($x, $y, $width, $height) { $aspect = $this->getHeight() / $this->getWidth(); if (empty($height)) $height = $width / $aspect; if (empty($width)) $width = $height * $aspect; @@ -421,60 +294,31 @@ function crop($x, $y, $width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->getToolkit()->crop($this, $x, $y, $width, $height); + return $this->toolkit->crop($this, $x, $y, $width, $height); } /** - * Resizes an image to the given dimensions (ignoring aspect ratio). - * - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::resize() + * {@inheritdoc} */ public function resize($width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->getToolkit()->resize($this, $width, $height); + return $this->toolkit->resize($this, $width, $height); } /** - * Converts an image to grayscale. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::desaturate() + * {@inheritdoc} */ public function desaturate() { - return $this->getToolkit()->desaturate($this); + return $this->toolkit->desaturate($this); } /** - * Rotates an image by the given number of degrees. - * - * @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 on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::rotate() + * {@inheritdoc} */ public function rotate($degrees, $background = NULL) { - return $this->getToolkit()->rotate($this, $degrees, $background); + return $this->toolkit->rotate($this, $degrees, $background); } /** diff --git a/core/lib/Drupal/Core/Image/ImageFactory.php b/core/lib/Drupal/Core/Image/ImageFactory.php new file mode 100644 index 0000000..06d827e --- /dev/null +++ b/core/lib/Drupal/Core/Image/ImageFactory.php @@ -0,0 +1,57 @@ +setToolkit($toolkit); + } + + /** + * Sets a custom image toolkit. + * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * The image toolkit to use for this image. + * + * @return self + * Returns this image. + */ + public function setToolkit(ImageToolkitInterface $toolkit) { + $this->toolkit = $toolkit; + return $this; + } + + /** + * Constructs a new Image object. + * + * @param string $source + * The path to an image file. + * + * @return \Drupal\Core\Image\ImageInterface + */ + public function get($source) { + return new Image($source, $this->toolkit); + } + +} diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php new file mode 100644 index 0000000..6cc2113 --- /dev/null +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -0,0 +1,273 @@ +getFileUri()); + $image = Drupal::image($file->getFileUri()); if (!$image->getExtension()) { $errors[] = t('Only JPEG, PNG and GIF images are allowed.'); } @@ -456,14 +455,14 @@ function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $mi $errors = array(); // Check first that the file is an image. - $image = new Image($file->getFileUri()); + $image = Drupal::image($file->getFileUri()); if ($image->getExtension()) { if ($maximum_dimensions) { // Check that it is smaller than the given dimensions. list($width, $height) = explode('x', $maximum_dimensions); if ($image->getWidth() > $width || $image->getHeight() > $height) { // Try to resize the image to fit the dimensions. - $image = new Image($file->getFileUri()); + $image = Drupal::image($file->getFileUri()); if ($image->getResource()) { $image->scale($width, $height); $image->save(); diff --git a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php index 97115d2..5214aa4 100644 --- a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php @@ -7,8 +7,6 @@ namespace Drupal\file\Tests; -use Drupal\Core\Image\Image; - /** * This will run tests against the file validation functions (file_validate_*). */ @@ -89,7 +87,7 @@ function testFileValidateImageResolution() { $errors = file_validate_image_resolution($this->image, '10x5'); $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File'); - $image = new Image($this->image->getFileUri()); + $image = $this->container->get('image.factory')->get($this->image->getFileUri()); $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File'); $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File'); diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index 37a8163..0a53fcc 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -6,7 +6,6 @@ */ use Drupal\Component\Utility\String; -use Drupal\Core\Image\Image; use Drupal\image\ConfigurableImageEffectInterface; use Drupal\image\ImageStyleInterface; @@ -380,7 +379,7 @@ function theme_image_style_preview($variables) { // Set up original file information. $original_path = $sample_image; - $original_image = new Image($original_path); + $original_image = Drupal::image($original_path); $original_image = array( 'width' => $original_image->getWidth(), 'height' => $original_image->getHeight(), @@ -400,7 +399,7 @@ function theme_image_style_preview($variables) { if (!file_exists($preview_file)) { $style->createDerivative($original_path, $preview_file); } - $preview_image = new Image($preview_file); + $preview_image = Drupal::image($preview_file); $preview_image = array( 'width' => $preview_image->getWidth(), 'height' => $preview_image->getHeight(), diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index 92ea0e5..c8bbedc 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -7,7 +7,6 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Image\Image; /** * Implements hook_field_info(). @@ -230,7 +229,7 @@ function image_field_presave(EntityInterface $entity, $field, $instance, $langco // Determine the dimensions if necessary. foreach ($items as &$item) { if (!isset($item['width']) || !isset($item['height'])) { - $image = new Image(file_load($item['target_id'])->getFileUri()); + $image = Drupal::image(file_load($item['target_id'])->getFileUri()); if ($image->getExtension()) { $item['width'] = $image->getWidth(); $item['height'] = $image->getHeight(); @@ -300,7 +299,7 @@ function image_field_widget_process($element, &$form_state, $form) { $variables['height'] = $element['#value']['height']; } else { - $image = new Image($file->getFileUri()); + $image = Drupal::image($file->getFileUri()); if ($image->getExtension()) { $variables['width'] = $image->getWidth(); $variables['height'] = $image->getHeight(); diff --git a/core/modules/image/image.module b/core/modules/image/image.module index e377a3c..bbab24a 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -6,11 +6,9 @@ */ use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Image\Image; use Drupal\field\Plugin\Core\Entity\Field; use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\file\Plugin\Core\Entity\File; -use Drupal\image\ImageStyleInterface; use Drupal\image\Plugin\Core\Entity\ImageStyle; use Drupal\field\FieldInterface; use Drupal\field\FieldInstanceInterface; @@ -277,7 +275,7 @@ function image_file_download($uri) { $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args); // Check that the file exists and is an image. - $image = new Image($uri); + $image = Drupal::image($uri); if ($image->getExtension()) { // Check the permissions of the original to grant access to this image. $headers = module_invoke_all('file_download', $original_uri); diff --git a/core/modules/image/lib/Drupal/image/Controller/ImageStyleDownloadController.php b/core/modules/image/lib/Drupal/image/Controller/ImageStyleDownloadController.php index a585a23..8013962 100644 --- a/core/modules/image/lib/Drupal/image/Controller/ImageStyleDownloadController.php +++ b/core/modules/image/lib/Drupal/image/Controller/ImageStyleDownloadController.php @@ -11,7 +11,7 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Controller\ControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageFactory; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; use Drupal\image\ImageStyleInterface; @@ -50,6 +50,13 @@ class ImageStyleDownloadController extends FileDownloadController implements Con protected $translator; /** + * The image factory. + * + * @var \Drupal\Core\Image\ImageFactory + */ + protected $imageFactory; + + /** * Constructs a ImageStyleDownloadController object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler @@ -60,12 +67,15 @@ class ImageStyleDownloadController extends FileDownloadController implements Con * The lock backend. * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator * The translator service. + * @param \Drupal\Core\Image\ImageFactory $image_factory + * The image factory. */ - public function __construct(ModuleHandlerInterface $module_handler, ConfigFactory $config_factory, LockBackendInterface $lock, TranslatorInterface $translator) { + public function __construct(ModuleHandlerInterface $module_handler, ConfigFactory $config_factory, LockBackendInterface $lock, TranslatorInterface $translator, ImageFactory $image_factory) { parent::__construct($module_handler); $this->configFactory = $config_factory; $this->lock = $lock; $this->translator = $translator; + $this->imageFactory = $image_factory; } /** @@ -76,7 +86,8 @@ public static function create(ContainerInterface $container) { $container->get('module_handler'), $container->get('config.factory'), $container->get('lock'), - $container->get('string_translation') + $container->get('string_translation'), + $container->get('image.factory') ); } @@ -160,7 +171,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st } if ($success) { - $image = new Image($derivative_uri); + $image = $this->imageFactory->get($derivative_uri); $uri = $image->getSource(); $headers += array( 'Content-Type' => $image->getMimeType(), diff --git a/core/modules/image/lib/Drupal/image/ImageEffectInterface.php b/core/modules/image/lib/Drupal/image/ImageEffectInterface.php index 8fc5c62..acda391 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectInterface.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectInterface.php @@ -8,7 +8,7 @@ namespace Drupal\image; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; /** * Defines the interface for image effects. @@ -18,13 +18,13 @@ /** * Applies an image effect to the image object. * - * @param \Drupal\Core\Image\Image $image + * @param \Drupal\Core\Image\ImageInterface $image * An image file object. * * @return bool * TRUE on success. FALSE if unable to perform the image effect on the image. */ - public function applyEffect(Image $image); + public function applyEffect(ImageInterface $image); /** * Determines the dimensions of the styled image. diff --git a/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php b/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php index 25e24e0..2e446ab 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php +++ b/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php @@ -11,7 +11,6 @@ use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\Core\Entity\EntityStorageControllerInterface; -use Drupal\Core\Image\Image; use Drupal\image\ImageEffectBag; use Drupal\image\ImageEffectInterface; use Drupal\image\ImageStyleInterface; @@ -278,7 +277,7 @@ public function createDerivative($original_uri, $derivative_uri) { return FALSE; } - $image = new Image($original_uri); + $image = \Drupal::image($original_uri); if (!$image->getResource()) { return FALSE; } 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 4cfba8c..e46d2fa 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; /** @@ -25,7 +25,7 @@ class CropImageEffect extends ResizeImageEffect { /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { // Set sane default values. $this->configuration += array( 'anchor' => 'center-center', 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 0a7c8aa..912bec9 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; use Drupal\image\ImageEffectBase; @@ -32,7 +32,7 @@ public function transformDimensions(array &$dimensions) { /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { if (!$image->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 9407a70..766e5a7 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; use Drupal\image\ConfigurableImageEffectInterface; use Drupal\image\ImageEffectBase; @@ -27,7 +27,7 @@ class ResizeImageEffect extends ImageEffectBase implements ConfigurableImageEffe /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { if (!$image->resize($this->configuration['width'], $this->configuration['height'])) { 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 cd488ee..fe563ae 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; use Drupal\image\ConfigurableImageEffectInterface; use Drupal\image\ImageEffectBase; @@ -27,7 +27,7 @@ class RotateImageEffect extends ImageEffectBase implements ConfigurableImageEffe /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { // Set sane default values. $this->configuration += array( 'degrees' => 0, 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 cd644ea..39bd2ec 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; /** @@ -25,7 +25,7 @@ class ScaleAndCropImageEffect extends ResizeImageEffect { /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { if (!$image->scaleAndCrop($this->configuration['width'], $this->configuration['height'])) { 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 1b7f0ec..6f0aacd 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\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; /** @@ -25,7 +25,7 @@ class ScaleImageEffect extends ResizeImageEffect { /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { // Set sane default values. $this->configuration += array( 'width' => NULL, diff --git a/core/modules/image/lib/Drupal/image/Tests/FileMoveTest.php b/core/modules/image/lib/Drupal/image/Tests/FileMoveTest.php index 2fc0b68..13518e0 100644 --- a/core/modules/image/lib/Drupal/image/Tests/FileMoveTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/FileMoveTest.php @@ -7,12 +7,12 @@ namespace Drupal\image\Tests; -use Drupal\system\Tests\Image\ToolkitTestBase; +use Drupal\simpletest\WebTestBase; /** * Tests the file move function for images and image styles. */ -class FileMoveTest extends ToolkitTestBase { +class FileMoveTest extends WebTestBase { /** * Modules to enable. @@ -38,7 +38,7 @@ function testNormal() { // Create derivative image. $styles = entity_load_multiple('image_style'); - $style = image_style_load(key($styles)); + $style = reset($styles); $original_uri = $file->getFileUri(); $derivative_uri = $style->buildUri($original_uri); $style->createDerivative($original_uri, $derivative_uri); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php index 76730df..c136fa6 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php @@ -7,7 +7,6 @@ namespace Drupal\image\Tests; -use Drupal\Core\Image\Image; use Drupal\simpletest\WebTestBase; /** @@ -36,6 +35,7 @@ public static function getInfo() { * Test styled image dimensions cumulatively. */ function testImageDimensions() { + $image_factory = $this->container->get('image.factory'); // Create a working copy of the file. $files = $this->drupalGetTestFiles('image'); $file = reset($files); @@ -54,7 +54,7 @@ function testImageDimensions() { 'height' => 20, ); // Verify that the original image matches the hard-coded values. - $image_file = new Image($original_uri); + $image_file = $image_factory->get($original_uri); $this->assertEqual($image_file->getWidth(), $variables['width']); $this->assertEqual($image_file->getHeight(), $variables['height']); @@ -76,7 +76,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 120); $this->assertEqual($image_file->getHeight(), 60); @@ -97,7 +97,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 60); $this->assertEqual($image_file->getHeight(), 120); @@ -119,7 +119,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); @@ -141,7 +141,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); @@ -159,7 +159,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); @@ -200,7 +200,7 @@ function testImageDimensions() { $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - $image_file = new Image($generated_uri); + $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 30); $this->assertEqual($image_file->getHeight(), 30); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldValidateTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldValidateTest.php index 995423e..e3d48bf 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldValidateTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldValidateTest.php @@ -7,8 +7,6 @@ namespace Drupal\image\Tests; -use Drupal\Core\Image\Image; - /** * Test class to check for various validations. */ @@ -38,8 +36,9 @@ function testResolution() { // big, so cycle through test image files until we have what we need. $image_that_is_too_big = FALSE; $image_that_is_too_small = FALSE; + $image_factory = $this->container->get('image.factory'); foreach ($this->drupalGetTestFiles('image') as $image) { - $image_file = new Image($image->uri); + $image_file = $image_factory->get($image->uri); if ($image_file->getWidth() > $max_resolution) { $image_that_is_too_big = $image; } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php index 061792f..fdd3686 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php @@ -9,7 +9,6 @@ use Drupal\Core\Entity\Field\FieldInterface; use Drupal\Core\Entity\Field\FieldItemInterface; -use Drupal\Core\Image\Image; use Drupal\field\Tests\FieldUnitTestBase; /** @@ -31,6 +30,11 @@ class ImageItemTest extends FieldUnitTestBase { */ protected $image; + /** + * @var \Drupal\Core\Image\ImageFactory + */ + protected $imageFactory; + public static function getInfo() { return array( 'name' => 'Image field item API', @@ -59,6 +63,7 @@ public function setUp() { 'uri' => 'public://example.jpg', )); $this->image->save(); + $this->imageFactory = $this->container->get('image.factory'); } /** @@ -79,7 +84,7 @@ public function testImageItem() { $this->assertEqual($entity->image_test->target_id, $this->image->id()); $this->assertEqual($entity->image_test->alt, $alt); $this->assertEqual($entity->image_test->title, $title); - $image = new Image('public://example.jpg'); + $image = $this->imageFactory->get('public://example.jpg'); $this->assertEqual($entity->image_test->width, $image->getWidth()); $this->assertEqual($entity->image_test->height, $image->getHeight()); $this->assertEqual($entity->image_test->entity->id(), $this->image->id()); @@ -99,7 +104,7 @@ public function testImageItem() { $entity->save(); $this->assertEqual($entity->image_test->entity->id(), $image2->id()); $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri()); - $image = new Image('public://example-2.jpg'); + $image = $this->imageFactory->get('public://example-2.jpg'); $this->assertEqual($entity->image_test->width, $image->getWidth()); $this->assertEqual($entity->image_test->height, $image->getHeight()); $this->assertEqual($entity->image_test->alt, $new_alt); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php index 5a7153c..4648701 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php @@ -7,7 +7,6 @@ namespace Drupal\image\Tests; -use Drupal\Core\Image\Image; use Drupal\simpletest\WebTestBase; use Symfony\Component\HttpFoundation\Request; @@ -154,7 +153,7 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $this->assertRaw(file_get_contents($generated_uri), 'URL returns expected file.'); - $image = new Image($generated_uri); + $image = $this->container->get('image.factory')->get($generated_uri); $this->assertEqual($this->drupalGetHeader('Content-Type'), $image->getMimeType(), 'Expected Content-Type was reported.'); $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.'); if ($scheme == 'private') { diff --git a/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php b/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php index a368faa..52731fe 100644 --- a/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php +++ b/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php @@ -8,7 +8,7 @@ namespace Drupal\image_module_test\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\image\Annotation\ImageEffect; use Drupal\image\ImageEffectBase; @@ -25,7 +25,7 @@ class NullTestImageEffect extends ImageEffectBase { /** * {@inheritdoc} */ - public function applyEffect(Image $image) { + public function applyEffect(ImageInterface $image) { return TRUE; } 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 8a68d09..611e19c 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -10,7 +10,7 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\system\Plugin\ImageToolkitInterface; /** @@ -51,7 +51,7 @@ public function settingsFormSubmit($form, &$form_state) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::resize(). */ - public function resize(Image $image, $width, $height) { + public function resize(ImageInterface $image, $width, $height) { $res = $this->createTmp($image, $width, $height); if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) { @@ -70,7 +70,7 @@ public function resize(Image $image, $width, $height) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::rotate(). */ - public function rotate(Image $image, $degrees, $background = NULL) { + 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())); @@ -125,7 +125,7 @@ public function rotate(Image $image, $degrees, $background = NULL) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::crop(). */ - public function crop(Image $image, $x, $y, $width, $height) { + public function crop(ImageInterface $image, $x, $y, $width, $height) { $res = $this->createTmp($image, $width, $height); if (!imagecopyresampled($res, $image->getResource(), 0, 0, $x, $y, $width, $height, $width, $height)) { @@ -144,7 +144,7 @@ public function crop(Image $image, $x, $y, $width, $height) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::desaturate(). */ - public function desaturate(Image $image) { + 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())); @@ -157,7 +157,7 @@ public function desaturate(Image $image) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::load(). */ - public function load(Image $image) { + public function load(ImageInterface $image) { $extension = str_replace('jpg', 'jpeg', $image->getExtension()); $function = 'imagecreatefrom' . $extension; if (function_exists($function) && $resource = $function($image->getSource())) { @@ -179,7 +179,7 @@ public function load(Image $image) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::save(). */ - public function save(Image $image, $destination) { + public function save(ImageInterface $image, $destination) { $scheme = file_uri_scheme($destination); // Work around lack of stream wrapper support in imagejpeg() and imagepng(). if ($scheme && file_stream_wrapper_valid_scheme($scheme)) { @@ -219,7 +219,7 @@ public function save(Image $image, $destination) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::getInfo(). */ - public function getInfo(Image $image) { + public function getInfo(ImageInterface $image) { $details = FALSE; $data = getimagesize($image->getSource()); @@ -240,7 +240,7 @@ public function getInfo(Image $image) { /** * Creates a truecolor image preserving transparency from a provided image. * - * @param \Drupal\Core\Image\Image $image + * @param \Drupal\Core\Image\ImageInterface $image * An image object. * @param int $width * The new width of the new image, in pixels. @@ -250,7 +250,7 @@ public function getInfo(Image $image) { * @return resource * A GD image handle. */ - public function createTmp(Image $image, $width, $height) { + public function createTmp(ImageInterface $image, $width, $height) { $res = imagecreatetruecolor($width, $height); if ($image->getExtension() == 'gif') { diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index dd62b65..774f050 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -8,7 +8,7 @@ namespace Drupal\system\Plugin; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; /** * @defgroup image Image toolkits @@ -62,7 +62,7 @@ function settingsFormSubmit($form, &$form_state); /** * Scales an image to the specified size. * - * @param \Drupal\Core\Image\Image $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 $width @@ -73,12 +73,12 @@ function settingsFormSubmit($form, &$form_state); * @return bool * TRUE or FALSE, based on success. */ - function resize(Image $image, $width, $height); + function resize(ImageInterface $image, $width, $height); /** * Rotates an image the given number of degrees. * - * @param \Drupal\Core\Image\Image $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 $degrees @@ -93,12 +93,12 @@ function resize(Image $image, $width, $height); * @return bool * TRUE or FALSE, based on success. */ - function rotate(Image $image, $degrees, $background = NULL); + function rotate(ImageInterface $image, $degrees, $background = NULL); /** * Crops an image. * - * @param \Drupal\Core\Image\Image $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 @@ -115,37 +115,37 @@ function rotate(Image $image, $degrees, $background = NULL); * * @see image_crop() */ - function crop(Image $image, $x, $y, $width, $height); + 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\Image $image + * @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. */ - function desaturate(Image $image); + function desaturate(ImageInterface $image); /** * Creates an image resource from a file. * - * @param \Drupal\Core\Image\Image $image + * @param \Drupal\Core\Image\ImageInterface $image * An image object. The $image->resource value will populated by this call. * * @return bool * TRUE or FALSE, based on success. */ - function load(Image $image); + function load(ImageInterface $image); /** * Writes an image resource to a destination file. * - * @param \Drupal\Core\Image\Image $image + * @param \Drupal\Core\Image\ImageInterface $image * An image object. * @param string $destination * A string file URI or path where the image should be saved. @@ -153,12 +153,12 @@ function load(Image $image); * @return bool * TRUE or FALSE, based on success. */ - function save(Image $image, $destination); + function save(ImageInterface $image, $destination); /** * Gets details about an image. * - * @param \Drupal\Core\Image\Image $image + * @param \Drupal\Core\Image\ImageInterface $image * An image object. * * @return array @@ -169,9 +169,9 @@ function save(Image $image, $destination); * - "extension": Commonly used file extension for the image. * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png'). * - * @see \Drupal\Core\Image\Image::processInfo() + * @see \Drupal\Core\Image\ImageInterface::processInfo() */ - function getInfo(Image $image); + function getInfo(ImageInterface $image); /** * Verifies Image Toolkit is set up correctly. 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 b40cab5..1797b78 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php @@ -7,9 +7,8 @@ namespace Drupal\system\Tests\Image; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\simpletest\DrupalUnitTestBase; -use Drupal\system\Plugin\ImageToolkitManager; /** * Test the core GD image manipulation functions. @@ -80,7 +79,7 @@ function colorsAreEqual($color_a, $color_b) { /** * Function for finding a pixel's RGBa values. */ - function getPixelColor(Image $image, $x, $y) { + function getPixelColor(ImageInterface $image, $x, $y) { $color_index = imagecolorat($image->getResource(), $x, $y); $transparent_index = imagecolortransparent($image->getResource()); @@ -216,11 +215,12 @@ function testManipulations() { ); } - $manager = new ImageToolkitManager($this->container->get('container.namespaces'), $this->container->get('cache.cache'), $this->container->get('language_manager')); + $toolkit = $this->container->get('image.toolkit.manager')->createInstance('gd'); + $image_factory = $this->container->get('image.factory')->setToolkit($toolkit); foreach ($files as $file) { foreach ($operations as $op => $values) { // Load up a fresh image. - $image = new Image(drupal_get_path('module', 'simpletest') . '/files/' . $file, $manager->createInstance('gd')); + $image = $image_factory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file); if (!$image) { $this->fail(t('Could not load image %file.', array('%file' => $file))); continue 2; 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 3de62cf..5f6c4dc 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php @@ -7,7 +7,6 @@ namespace Drupal\system\Tests\Image; -use Drupal\Core\Image\Image; use Drupal\system\Plugin\ImageToolkitManager; /** @@ -38,8 +37,7 @@ function testGetAvailableToolkits() { * Tests Image's methods. */ function testLoad() { - $image = new Image($this->file); - $image->setToolkit($this->toolkit)->getResource(); + $image = $this->getImage(); $this->assertTrue(is_object($image), 'Returned an object.'); $this->assertEqual($this->toolkit->getPluginId(), $image->getToolkitId(), 'Image had toolkit set.'); $this->assertToolkitOperationsCalled(array('load', 'get_info')); 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 c875bed..43cc2c2 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php @@ -7,10 +7,8 @@ namespace Drupal\system\Tests\Image; -use Drupal\Core\Image\Image; use Drupal\simpletest\WebTestBase; use Drupal\system\Plugin\ImageToolkitManager; -use stdClass; /** * Base class for image manipulation testing. @@ -35,7 +33,7 @@ protected $file; /** - * @var \Drupal\Core\Image\Image + * @var \Drupal\Core\Image\ImageInterface */ protected $image; @@ -51,17 +49,27 @@ function setUp() { $this->file = $file->uri; // Setup a dummy image to work with. - $this->image = new Image($this->file); - // Trigger processing with the default toolkit before setting a custom one. - $this->image->getExtension(); - // Set the custom toolkit. - $this->image->setToolkit($this->toolkit); + $this->image = $this->getImage(); // Clear out any hook calls. $this->imageTestReset(); } /** + * Sets up an image with the custom toolkit. + * + * @return \Drupal\Core\Image\ImageInterface + * The image object. + */ + protected function getImage() { + $image = $this->container->get('image.factory') + ->setToolkit($this->toolkit) + ->get($this->file); + $image->getResource(); + return $image; + } + + /** * Assert that all of the specified image toolkit operations were called * exactly once once, other values result in failure. * diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index ab6ae45..aac38bb 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -2149,7 +2149,7 @@ function hook_file_download($uri) { return -1; } else { - $image = new \Drupal\Core\Image\Image($uri); + $image = Drupal::image($uri); return array('Content-Type' => $image->getMimeType()); } } 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 1895729..64aad2d 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 @@ -10,7 +10,7 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Image\Image; +use Drupal\Core\Image\ImageInterface; use Drupal\system\Plugin\ImageToolkitInterface; /** @@ -39,15 +39,30 @@ public function settingsFormSubmit($form, &$form_state) {} /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::getInfo(). */ - public function getInfo(Image $image) { + public function getInfo(ImageInterface $image) { $this->logCall('get_info', array($image)); - return array(); + + $details = FALSE; + $data = getimagesize($image->getSource()); + + if (isset($data) && is_array($data)) { + $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png'); + $extension = isset($extensions[$data[2]]) ? $extensions[$data[2]] : ''; + $details = array( + 'width' => $data[0], + 'height' => $data[1], + 'extension' => $extension, + 'mime_type' => $data['mime'], + ); + } + + return $details; } /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::load(). */ - public function load(Image $image) { + public function load(ImageInterface $image) { $this->logCall('load', array($image)); return $image; } @@ -55,7 +70,7 @@ public function load(Image $image) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::save(). */ - public function save(Image $image, $destination) { + public function save(ImageInterface $image, $destination) { $this->logCall('save', array($image, $destination)); // Return false so that image_save() doesn't try to chmod the destination // file that we didn't bother to create. @@ -65,7 +80,7 @@ public function save(Image $image, $destination) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::crop(). */ - public function crop(Image $image, $x, $y, $width, $height) { + public function crop(ImageInterface $image, $x, $y, $width, $height) { $this->logCall('crop', array($image, $x, $y, $width, $height)); return TRUE; } @@ -73,7 +88,7 @@ public function crop(Image $image, $x, $y, $width, $height) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::resize(). */ - public function resize(Image $image, $width, $height) { + public function resize(ImageInterface $image, $width, $height) { $this->logCall('resize', array($image, $width, $height)); return TRUE; } @@ -81,7 +96,7 @@ public function resize(Image $image, $width, $height) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::rotate(). */ - public function rotate(Image $image, $degrees, $background = NULL) { + public function rotate(ImageInterface $image, $degrees, $background = NULL) { $this->logCall('rotate', array($image, $degrees, $background)); return TRUE; } @@ -89,7 +104,7 @@ public function rotate(Image $image, $degrees, $background = NULL) { /** * Implements \Drupal\system\Plugin\ImageToolkitInterface::desaturate(). */ - public function desaturate(Image $image) { + public function desaturate(ImageInterface $image) { $this->logCall('desaturate', array($image)); return TRUE; }