3 namespace Drupal\system\Plugin\ImageToolkit;
5 use Drupal\Component\Utility\Color;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\ImageToolkit\ImageToolkitBase;
9 use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface;
10 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
11 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
12 use Psr\Log\LoggerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Defines the GD2 toolkit for image manipulation within Drupal.
20 * title = @Translation("GD2 image manipulation toolkit")
23 class GDToolkit extends ImageToolkitBase {
26 * A GD image resource.
30 protected $resource = NULL;
33 * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
40 * Image information from a file, available prior to loading the GD resource.
42 * This contains a copy of the array returned by executing getimagesize()
43 * on the image file when the image object is instantiated. It gets reset
44 * to NULL as soon as the GD resource is loaded.
48 * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::parseFile()
49 * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::setResource()
50 * @see http://php.net/manual/function.getimagesize.php
52 protected $preLoadInfo = NULL;
55 * The StreamWrapper manager.
57 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
59 protected $streamWrapperManager;
62 * Constructs a GDToolkit object.
64 * @param array $configuration
65 * A configuration array containing information about the plugin instance.
66 * @param string $plugin_id
67 * The plugin_id for the plugin instance.
68 * @param array $plugin_definition
69 * The plugin implementation definition.
70 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
71 * The toolkit operation manager.
72 * @param \Psr\Log\LoggerInterface $logger
74 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
76 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
77 * The StreamWrapper manager.
79 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
80 parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
81 $this->streamWrapperManager = $stream_wrapper_manager;
85 * Destructs a GDToolkit object.
87 * Frees memory associated with a GD image resource.
89 public function __destruct() {
90 if (is_resource($this->resource)) {
91 imagedestroy($this->resource);
98 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
103 $container->get('image.toolkit.operation.manager'),
104 $container->get('logger.channel.image'),
105 $container->get('config.factory'),
106 $container->get('stream_wrapper_manager')
111 * Sets the GD image resource.
113 * @param resource $resource
114 * The GD image resource.
116 * @return \Drupal\system\Plugin\ImageToolkit\GDToolkit
117 * An instance of the current toolkit object.
119 public function setResource($resource) {
120 if (!is_resource($resource) || get_resource_type($resource) != 'gd') {
121 throw new \InvalidArgumentException('Invalid resource argument');
123 $this->preLoadInfo = NULL;
124 $this->resource = $resource;
129 * Retrieves the GD image resource.
131 * @return resource|null
132 * The GD image resource, or NULL if not available.
134 public function getResource() {
135 if (!is_resource($this->resource)) {
138 return $this->resource;
144 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
145 $form['image_jpeg_quality'] = [
147 '#title' => t('JPEG quality'),
148 '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
151 '#default_value' => $this->configFactory->getEditable('system.image.gd')->get('jpeg_quality', FALSE),
152 '#field_suffix' => t('%'),
160 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
161 $this->configFactory->getEditable('system.image.gd')
162 ->set('jpeg_quality', $form_state->getValue(['gd', 'image_jpeg_quality']))
167 * Loads a GD resource from a file.
170 * TRUE or FALSE, based on success.
172 protected function load() {
173 // Return immediately if the image file is not valid.
174 if (!$this->isValid()) {
178 $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
179 if (function_exists($function) && $resource = $function($this->getSource())) {
180 $this->setResource($resource);
181 if (imageistruecolor($resource)) {
185 // Convert indexed images to truecolor, copying the image to a new
186 // truecolor resource, so that filters work correctly and don't result
187 // in unnecessary dither.
189 'width' => imagesx($resource),
190 'height' => imagesy($resource),
191 'extension' => image_type_to_extension($this->getType(), FALSE),
192 'transparent_color' => $this->getTransparentColor(),
195 if ($this->apply('create_new', $data)) {
196 imagecopy($this->getResource(), $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
197 imagedestroy($resource);
200 return (bool) $this->getResource();
208 public function isValid() {
209 return ((bool) $this->preLoadInfo || (bool) $this->resource);
215 public function save($destination) {
216 $scheme = file_uri_scheme($destination);
217 // Work around lack of stream wrapper support in imagejpeg() and imagepng().
218 if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
219 // If destination is not local, save image to temporary local file.
220 $local_wrappers = $this->streamWrapperManager->getWrappers(StreamWrapperInterface::LOCAL);
221 if (!isset($local_wrappers[$scheme])) {
222 $permanent_destination = $destination;
223 $destination = drupal_tempnam('temporary://', 'gd_');
225 // Convert stream wrapper URI to normal path.
226 $destination = \Drupal::service('file_system')->realpath($destination);
229 $function = 'image' . image_type_to_extension($this->getType(), FALSE);
230 if (!function_exists($function)) {
233 if ($this->getType() == IMAGETYPE_JPEG) {
234 $success = $function($this->getResource(), $destination, $this->configFactory->get('system.image.gd')->get('jpeg_quality'));
237 // Always save PNG images with full transparency.
238 if ($this->getType() == IMAGETYPE_PNG) {
239 imagealphablending($this->getResource(), FALSE);
240 imagesavealpha($this->getResource(), TRUE);
242 $success = $function($this->getResource(), $destination);
244 // Move temporary local file to remote destination.
245 if (isset($permanent_destination) && $success) {
246 return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
254 public function parseFile() {
255 $data = @getimagesize($this->getSource());
256 if ($data && in_array($data[2], static::supportedTypes())) {
257 $this->setType($data[2]);
258 $this->preLoadInfo = $data;
265 * Gets the color set for transparency in GIF images.
267 * @return string|null
268 * A color string like '#rrggbb', or NULL if not set or not relevant.
270 public function getTransparentColor() {
271 if (!$this->getResource() || $this->getType() != IMAGETYPE_GIF) {
274 // Find out if a transparent color is set, will return -1 if no
275 // transparent color has been defined in the image.
276 $transparent = imagecolortransparent($this->getResource());
277 if ($transparent >= 0) {
278 // Find out the number of colors in the image palette. It will be 0 for
280 $palette_size = imagecolorstotal($this->getResource());
281 if ($palette_size == 0 || $transparent < $palette_size) {
282 // Return the transparent color, either if it is a truecolor image
283 // or if the transparent color is part of the palette.
284 // Since the index of the transparent color is a property of the
285 // image rather than of the palette, it is possible that an image
286 // could be created with this index set outside the palette size.
287 // (see http://stackoverflow.com/a/3898007).
288 $rgb = imagecolorsforindex($this->getResource(), $transparent);
289 unset($rgb['alpha']);
290 return Color::rgbToHex($rgb);
299 public function getWidth() {
300 if ($this->preLoadInfo) {
301 return $this->preLoadInfo[0];
303 elseif ($res = $this->getResource()) {
304 return imagesx($res);
314 public function getHeight() {
315 if ($this->preLoadInfo) {
316 return $this->preLoadInfo[1];
318 elseif ($res = $this->getResource()) {
319 return imagesy($res);
327 * Gets the PHP type of the image.
330 * The image type represented by a PHP IMAGETYPE_* constant (e.g.
333 public function getType() {
338 * Sets the PHP type of the image.
341 * The image type represented by a PHP IMAGETYPE_* constant (e.g.
346 public function setType($type) {
347 if (in_array($type, static::supportedTypes())) {
356 public function getMimeType() {
357 return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
363 public function getRequirements() {
367 $requirements['version'] = [
368 'title' => t('GD library'),
369 'value' => $info['GD Version'],
372 // Check for filter and rotate support.
373 if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
374 $requirements['version']['severity'] = REQUIREMENT_WARNING;
375 $requirements['version']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="http://php.net/manual/book.image.php">the PHP manual</a>.');
378 return $requirements;
384 public static function isAvailable() {
385 // GD2 support is available.
386 return function_exists('imagegd2');
392 public static function getSupportedExtensions() {
394 foreach (static::supportedTypes() as $image_type) {
395 // @todo Automatically fetch possible extensions for each mime type.
396 // @see https://www.drupal.org/node/2311679
397 $extension = mb_strtolower(image_type_to_extension($image_type, FALSE));
398 $extensions[] = $extension;
399 // Add some known similar extensions.
400 if ($extension === 'jpeg') {
401 $extensions[] = 'jpg';
402 $extensions[] = 'jpe';
409 * Returns the IMAGETYPE_xxx constant for the given extension.
411 * This is the reverse of the image_type_to_extension() function.
413 * @param string $extension
414 * The extension to get the IMAGETYPE_xxx constant for.
417 * The IMAGETYPE_xxx constant for the given extension, or IMAGETYPE_UNKNOWN
418 * for unsupported extensions.
420 * @see image_type_to_extension()
422 public function extensionToImageType($extension) {
423 if (in_array($extension, ['jpe', 'jpg'])) {
426 foreach ($this->supportedTypes() as $type) {
427 if (image_type_to_extension($type, FALSE) === $extension) {
431 return IMAGETYPE_UNKNOWN;
435 * Returns a list of image types supported by the toolkit.
438 * An array of available image types. An image type is represented by a PHP
439 * IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
441 protected static function supportedTypes() {
442 return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];