diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 1905887..83a7381 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -181,8 +181,9 @@ protected function load() { 'height' => imagesy($resource), 'extension' => image_type_to_extension($this->getType(), FALSE), 'transparent_color' => $this->getTransparentColor(), + 'is_temp' => TRUE, ); - if ($this->apply('create_temp', $data)) { + if ($this->apply('create_new', $data)) { imagecopy($this->getResource(), $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource)); imagedestroy($resource); } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php index a20137a..bacd44f 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php @@ -58,9 +58,10 @@ protected function execute(array $arguments) { 'width' => $width, 'height' => $height, 'extension' => $arguments['extension'], - 'transparent_color' => $this->getToolkit()->getTransparentColor() + 'transparent_color' => $this->getToolkit()->getTransparentColor(), + 'is_temp' => TRUE, ); - if ($this->getToolkit()->apply('create_temp', $data)) { + if ($this->getToolkit()->apply('create_new', $data)) { if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $width, $height, $width, $height)) { imagedestroy($original_resource); return TRUE; diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php index 0c69841..f13b163 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -7,6 +7,9 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; +use Drupal\Component\Utility\Color; +use Drupal\Component\Utility\SafeMarkup; + /** * Defines GD2 create_new image operation. * @@ -41,27 +44,97 @@ protected function arguments() { 'required' => FALSE, 'default' => '#ffffff', ), + 'is_temp' => array( + 'description' => 'GD internal - indicates that the original GD resource should not be destroyed as a calling operation relies on it.', + 'required' => FALSE, + 'default' => FALSE, + ), ); } /** * {@inheritdoc} */ + protected function validateArguments(array $arguments) { + // Assure extension is supported. + if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) { + throw new \InvalidArgumentException(SafeMarkup::format("Invalid extension (@value) specified for the image 'convert' operation", array('@value' => $arguments['extension']))); + } + + // Assure integers for width and height. + $arguments['width'] = (int) round($arguments['width']); + $arguments['height'] = (int) round($arguments['height']); + + // Fail when width or height are 0 or negative. + if ($arguments['width'] <= 0) { + throw new \InvalidArgumentException(SafeMarkup::format("Invalid width (@value) specified for the image 'create_new' operation", array('@value' => $arguments['width']))); + } + if ($arguments['height'] <= 0) { + throw new \InvalidArgumentException(SafeMarkup::format("Invalid height (@value) specified for the image 'create_new' operation", array('@value' => $arguments['height']))); + } + + // Assure transparent color is a valid hex string. + if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) { + throw new \InvalidArgumentException(SafeMarkup::format("Invalid transparent color (@value) specified for the image 'create_new' operation", array('@value' => $arguments['transparent_color']))); + } + + return $arguments; + } + + /** + * {@inheritdoc} + */ protected function execute(array $arguments) { - // Stores the original GD resource. + // Get the image type. + $type = $this->getToolkit()->extensionToImageType($arguments['extension']); + + // Store the original GD resource. $original_res = $this->getToolkit()->getResource(); - // Applies the 'create_temp' operation and retrieves the current GD - // resource. - $result = $this->getToolkit()->apply('create_temp', $arguments); - $current_res = $this->getToolkit()->getResource(); + // Create the resource. + if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) { + return FALSE; + } + + // Fill the resource with transparency as possible. + switch ($type) { + case IMAGETYPE_PNG: + imagealphablending($res, FALSE); + $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); + imagefill($res, 0, 0, $transparency); + imagealphablending($res, TRUE); + imagesavealpha($res, TRUE); + break; + + case IMAGETYPE_GIF: + if (empty($arguments['transparent_color'])) { + // No transparency color specified, fill white. + $fill_color = imagecolorallocate($res, 255, 255, 255); + } + else { + $fill_rgb = Color::hexToRgb($arguments['transparent_color']); + $fill_color = imagecolorallocate($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue']); + imagecolortransparent($res, $fill_color); + } + imagefill($res, 0, 0, $fill_color); + break; + + case IMAGETYPE_JPEG: + imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); + break; + + } + + // Update the toolkit properties. + $this->getToolkit()->setType($type); + $this->getToolkit()->setResource($res); - // Destroys the original resource if the new image is correct. - if (is_resource($original_res) && is_resource($current_res) && $original_res !== $current_res) { + // Destroy the original resource if it is not needed by other operations. + if (!$arguments['is_temp'] && is_resource($original_res)) { imagedestroy($original_res); } - return $result; + return TRUE; } } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateTemp.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateTemp.php deleted file mode 100644 index 5d36e32..0000000 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateTemp.php +++ /dev/null @@ -1,126 +0,0 @@ - array( - 'description' => 'The width of the image, in pixels', - ), - 'height' => array( - 'description' => 'The height of the image, in pixels', - ), - 'extension' => array( - 'description' => 'The extension of the image file (e.g. png, gif, etc.)', - 'required' => FALSE, - 'default' => 'png', - ), - 'transparent_color' => array( - 'description' => 'The RGB hex color for GIF transparency', - 'required' => FALSE, - 'default' => '#ffffff', - ), - ); - } - - /** - * {@inheritdoc} - */ - protected function validateArguments(array $arguments) { - // Assure extension is supported. - if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) { - throw new \InvalidArgumentException(SafeMarkup::format("Invalid extension (@value) specified for the image 'create_temp' operation", array('@value' => $arguments['extension']))); - } - - // Assure integers for width and height. - $arguments['width'] = (int) round($arguments['width']); - $arguments['height'] = (int) round($arguments['height']); - - // Fail when width or height are 0 or negative. - if ($arguments['width'] <= 0) { - throw new \InvalidArgumentException(SafeMarkup::format("Invalid width (@value) specified for the image 'create_temp' operation", array('@value' => $arguments['width']))); - } - if ($arguments['height'] <= 0) { - throw new \InvalidArgumentException(SafeMarkup::format("Invalid height (@value) specified for the image 'create_temp' operation", array('@value' => $arguments['height']))); - } - - // Assure transparent color is a valid hex string. - if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) { - throw new \InvalidArgumentException(SafeMarkup::format("Invalid transparent color (@value) specified for the image 'create_temp' operation", array('@value' => $arguments['transparent_color']))); - } - - return $arguments; - } - - /** - * {@inheritdoc} - */ - protected function execute(array $arguments) { - // Get the image type. - $type = $this->getToolkit()->extensionToImageType($arguments['extension']); - - // Create the resource. - if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) { - return FALSE; - } - - // Fill the resource with transparency as possible. - switch ($type) { - case IMAGETYPE_PNG: - imagealphablending($res, FALSE); - $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); - imagefill($res, 0, 0, $transparency); - imagealphablending($res, TRUE); - imagesavealpha($res, TRUE); - break; - - case IMAGETYPE_GIF: - if (empty($arguments['transparent_color'])) { - // No transparency color specified, fill white. - $fill_color = imagecolorallocate($res, 255, 255, 255); - } - else { - $fill_rgb = Color::hexToRgb($arguments['transparent_color']); - $fill_color = imagecolorallocate($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue']); - imagecolortransparent($res, $fill_color); - } - imagefill($res, 0, 0, $fill_color); - break; - - case IMAGETYPE_JPEG: - imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); - break; - - } - - // Update the toolkit properties. - $this->getToolkit()->setType($type); - $this->getToolkit()->setResource($res); - return TRUE; - } - -} diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php index 45e611b..49c71d3 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -88,9 +88,10 @@ protected function execute(array $arguments) { 'width' => $arguments['width'], 'height' => $arguments['height'], 'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE), - 'transparent_color' => $this->getToolkit()->getTransparentColor() + 'transparent_color' => $this->getToolkit()->getTransparentColor(), + 'is_temp' => TRUE, ); - if ($this->getToolkit()->apply('create_temp', $data)) { + if ($this->getToolkit()->apply('create_new', $data)) { if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) { imagedestroy($original_resource); return TRUE; diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php index 77b608f..acccd37 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -67,9 +67,10 @@ protected function execute(array $arguments = array()) { 'width' => $arguments['width'], 'height' => $arguments['height'], 'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE), - 'transparent_color' => $this->getToolkit()->getTransparentColor() + 'transparent_color' => $this->getToolkit()->getTransparentColor(), + 'is_temp' => TRUE, ); - if ($this->getToolkit()->apply('create_temp', $data)) { + if ($this->getToolkit()->apply('create_new', $data)) { if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) { imagedestroy($original_resource); return TRUE;