diff --git a/core/core.services.yml b/core/core.services.yml index 4bf1f7a..aaa5592 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -522,8 +522,8 @@ services: image.toolkit.manager: class: Drupal\system\Plugin\ImageToolkitManager arguments: ['@container.namespaces', '@cache.cache', '@language_manager'] - image.toolkit.operations.manager: - class: Drupal\system\Plugin\ImageToolkitOperationsManager + image.toolkit.operation.manager: + class: Drupal\system\Plugin\ImageToolkitOperationManager arguments: ['@container.namespaces', '@cache.cache', '@language_manager'] image.toolkit: class: Drupal\system\Plugin\ImageToolkitInterface diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 8996f50..3bb9a7d 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -326,7 +326,7 @@ public function crop($x, $y, $width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->toolkit->core('crop', array($this, $x, $y, $width, $height)); + return $this->toolkit->operation('crop', array($this, $x, $y, $width, $height)); } /** @@ -336,21 +336,21 @@ public function resize($width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->toolkit->core('resize', array($this, $width, $height)); + return $this->toolkit->operation('resize', array($this, $width, $height)); } /** * {@inheritdoc} */ public function desaturate() { - return $this->toolkit->core('desaturate', array($this)); + return $this->toolkit->operation('desaturate', array($this)); } /** * {@inheritdoc} */ public function rotate($degrees, $background = NULL) { - return $this->toolkit->core('rotate', array($this, $degrees, $background)); + return $this->toolkit->operation('rotate', array($this, $degrees, $background)); } /** diff --git a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php new file mode 100644 index 0000000..3498251 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -0,0 +1,53 @@ +core('createTmp', array($image, $image->getWidth(), $image->getHeight())); + $new_image = $this->operation('createTmp', array($image, $image->getWidth(), $image->getHeight())); imagecopy($new_image, $resource, 0, 0, 0, 0, $image->getWidth(), $image->getHeight()); imagedestroy($resource); $image->setResource($new_image); @@ -150,10 +150,11 @@ public static function supportedTypes() { /** * @todo */ - public function __call($name, $arguments) { - if ($name == 'core' && !isset($this->core)) { - $this->core = \Drupal::service('image.toolkit.operations.manager')->createInstance('gd_core_operations'); - } - return call_user_func_array(array($this->core, $arguments[0]), $arguments[1]); + public function operation($name, $arguments) { + $plugin_id = ($name == 'createTmp') ? 'gd_create_tmp' : 'gd_' . $name; + $operation = \Drupal::service('image.toolkit.operation.manager')->createInstance($plugin_id); + $args = array($this); + $args = array_merge($args, $arguments); + return call_user_func_array(array($operation, $name), $args); } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitCoreOperationsInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitCoreOperationsInterface.php deleted file mode 100644 index 1d58c9d..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitCoreOperationsInterface.php +++ /dev/null @@ -1,97 +0,0 @@ -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); - -} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php new file mode 100644 index 0000000..e371ddd --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php @@ -0,0 +1,71 @@ +getType() == IMAGETYPE_GIF) { + // Grab transparent color index from image resource. + $transparent = imagecolortransparent($image->getResource()); + + if ($transparent >= 0) { + // The original must have a transparent color, allocate to the new image. + $transparent_color = imagecolorsforindex($image->getResource(), $transparent); + $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + + // Flood with our new transparent color. + imagefill($res, 0, 0, $transparent); + imagecolortransparent($res, $transparent); + } + } + elseif ($image->getType() == IMAGETYPE_PNG) { + imagealphablending($res, FALSE); + $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); + imagefill($res, 0, 0, $transparency); + imagealphablending($res, TRUE); + imagesavealpha($res, TRUE); + } + else { + imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); + } + + return $res; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php new file mode 100644 index 0000000..bf82e2a --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php @@ -0,0 +1,46 @@ +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; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php new file mode 100644 index 0000000..0121ee3 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php @@ -0,0 +1,40 @@ + $image->getSource())); + return FALSE; + } + + return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php new file mode 100644 index 0000000..c119c09 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php @@ -0,0 +1,47 @@ +operation('createTmp', array($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; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php new file mode 100644 index 0000000..82c2cf9 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php @@ -0,0 +1,80 @@ + $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; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php new file mode 100644 index 0000000..7d35011 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php @@ -0,0 +1,35 @@ +setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operation'); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperations/GDToolkitOperations.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperations/GDToolkitOperations.php deleted file mode 100644 index d7a52de..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperations/GDToolkitOperations.php +++ /dev/null @@ -1,175 +0,0 @@ -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) { - $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); - } - - /** - * Creates a truecolor image preserving transparency from a provided image. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * @param int $width - * The new width of the new image, in pixels. - * @param int $height - * The new height of the new image, in pixels. - * - * @return resource - * A GD image handle. - */ - public function createTmp(ImageInterface $image, $width, $height) { - $res = imagecreatetruecolor($width, $height); - - if ($image->getType() == IMAGETYPE_GIF) { - // Grab transparent color index from image resource. - $transparent = imagecolortransparent($image->getResource()); - - if ($transparent >= 0) { - // The original must have a transparent color, allocate to the new image. - $transparent_color = imagecolorsforindex($image->getResource(), $transparent); - $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); - - // Flood with our new transparent color. - imagefill($res, 0, 0, $transparent); - imagecolortransparent($res, $transparent); - } - } - elseif ($image->getType() == IMAGETYPE_PNG) { - imagealphablending($res, FALSE); - $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); - imagefill($res, 0, 0, $transparency); - imagealphablending($res, TRUE); - imagesavealpha($res, TRUE); - } - else { - imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); - } - - return $res; - } - -} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationsManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationsManager.php deleted file mode 100644 index 7c45fe0..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationsManager.php +++ /dev/null @@ -1,35 +0,0 @@ -setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operations'); - } - -}