summaryrefslogtreecommitdiffstats
path: root/core/modules/system/lib/Drupal/system/Plugin/system/imagetoolkit/GDToolkit.php
blob: 1c513cb92de167d25fbc26e63ec286c32c1b8f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php

/**
 * @file
 * Contains \Drupal\system\Plugin\system\imagetoolkit\GDToolkit;.
 */

namespace Drupal\system\Plugin\system\imagetoolkit;

use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\system\Plugin\ImageToolkitInterface;

/**
 * Defines the GD2 toolkit for image manipulation within Drupal.
 *
 * @Plugin(
 *   id = "gd",
 *   title = @Translation("GD2 image manipulation toolkit")
 * )
 */
class GDToolkit extends PluginBase implements ImageToolkitInterface {

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsForm().
   */
  public function settingsForm() {
    $form['image_jpeg_quality'] = array(
      '#type' => 'number',
      '#title' => t('JPEG quality'),
      '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
      '#min' => 0,
      '#max' => 100,
      '#default_value' => config('system.image.gd')->get('jpeg_quality'),
      '#field_suffix' => t('%'),
    );
    return $form;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsFormSubmit().
   */
  public function settingsFormSubmit($form, &$form_state) {
    config('system.image.gd')
      ->set('jpeg_quality', $form_state['values']['gd']['image_jpeg_quality'])
      ->save();
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::resize().
   */
  public function resize($image, $width, $height) {
    $res = $this->createTmp($image, $width, $height);

    if (!imagecopyresampled($res, $image->resource, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) {
      return FALSE;
    }

    imagedestroy($image->resource);
    // Update image object.
    $image->resource = $res;
    $image->info['width'] = $width;
    $image->info['height'] = $height;
    return TRUE;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::rotate().
   */
  public function rotate($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->source));
      return FALSE;
    }

    $width = $image->info['width'];
    $height = $image->info['height'];

    // 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->resource, $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->resource);

      // If no transparent colors, use white.
      if ($background == 0) {
        $background = imagecolorallocatealpha($image->resource, 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->info['extension'] == 'gif') {
      $transparent_index = imagecolortransparent($image->resource);
      if ($transparent_index != 0) {
        $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
      }
    }

    $image->resource = imagerotate($image->resource, 360 - $degrees, $background);

    // GIFs need to reassign the transparent color after performing the rotate.
    if (isset($transparent_gif_color)) {
      $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']);
      imagecolortransparent($image->resource, $background);
    }

    $image->info['width'] = imagesx($image->resource);
    $image->info['height'] = imagesy($image->resource);
    return TRUE;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::crop().
   */
  public function crop($image, $x, $y, $width, $height) {
    $res = $this->createTmp($image, $width, $height);

    if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) {
      return FALSE;
    }

    // Destroy the original image and return the modified image.
    imagedestroy($image->resource);
    $image->resource = $res;
    $image->info['width'] = $width;
    $image->info['height'] = $height;
    return TRUE;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::desaturate().
   */
  public function desaturate($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->source));
      return FALSE;
    }

    return imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::load().
   */
  public function load($image) {
    $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
    $function = 'imagecreatefrom' . $extension;
    if (function_exists($function) && $image->resource = $function($image->source)) {
      if (!imageistruecolor($image->resource)) {
        // Convert indexed images to true color, so that filters work
        // correctly and don't result in unnecessary dither.
        $new_image = $this->createTmp($image, $image->info['width'], $image->info['height']);
        imagecopy($new_image, $image->resource, 0, 0, 0, 0, $image->info['width'], $image->info['height']);
        imagedestroy($image->resource);
        $image->resource = $new_image;
      }
      return (bool) $image->resource;
    }

    return FALSE;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::save().
   */
  public function save($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)) {
      // If destination is not local, save image to temporary local file.
      $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
      if (!isset($local_wrappers[$scheme])) {
        $permanent_destination = $destination;
        $destination = drupal_tempnam('temporary://', 'gd_');
      }
      // Convert stream wrapper URI to normal path.
      $destination = drupal_realpath($destination);
    }

    $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
    $function = 'image' . $extension;
    if (!function_exists($function)) {
      return FALSE;
    }
    if ($extension == 'jpeg') {
      $success = $function($image->resource, $destination, config('system.image.gd')->get('jpeg_quality'));
    }
    else {
      // Always save PNG images with full transparency.
      if ($extension == 'png') {
        imagealphablending($image->resource, FALSE);
        imagesavealpha($image->resource, TRUE);
      }
      $success = $function($image->resource, $destination);
    }
    // Move temporary local file to remote destination.
    if (isset($permanent_destination) && $success) {
      return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
    }
    return $success;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::getInfo().
   */
  public function getInfo($image) {
    $details = FALSE;
    $data = getimagesize($image->source);

    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;
  }

  /**
   * Creates a truecolor image preserving transparency from a provided image.
   *
   * @param object $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($image, $width, $height) {
    $res = imagecreatetruecolor($width, $height);

    if ($image->info['extension'] == 'gif') {
      // Grab transparent color index from image resource.
      $transparent = imagecolortransparent($image->resource);

      if ($transparent >= 0) {
        // The original must have a transparent color, allocate to the new image.
        $transparent_color = imagecolorsforindex($image->resource, $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->info['extension'] == '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;
  }

  /**
   * Implements \Drupal\system\Plugin\ImageToolkitInterface::isAvailable().
   */
  public static function isAvailable() {
    if ($check = get_extension_funcs('gd')) {
      if (in_array('imagegd2', $check)) {
        // GD2 support is available.
        return TRUE;
      }
    }
    return FALSE;
  }
}