diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 08ff4f6..5e8a0ab 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -381,7 +381,7 @@ public function extensionToImageType($extension) { * An array of available image types. An image type is represented by a PHP * IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.). */ - public static function supportedTypes() { + protected static function supportedTypes() { return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); } } 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 49f7fbe..51d79aa 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php @@ -47,19 +47,29 @@ protected function validateArguments(array $arguments) { * {@inheritdoc} */ protected function execute(array $arguments) { - $type = $this->getToolkit()->extensionToImageType($arguments['extension']); - - $res = $this->getToolkit()->createTmp($type, $this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); - if (!imagecopyresampled($res, $this->getToolkit()->getResource(), 0, 0, 0, 0, $this->getToolkit()->getWidth(), $this->getToolkit()->getHeight(), $this->getToolkit()->getWidth(), $this->getToolkit()->getHeight())) { - return FALSE; + // Create a new resource of the required dimensions and format, and copy + // the original resource on it with resampling. Destroy the original + // resource upon success. + $width = $this->getToolkit()->getWidth(); + $height = $this->getToolkit()->getHeight(); + $original_resource = $this->getToolkit()->getResource(); + $original_type = $this->getToolkit()->getType(); + $data = array( + 'width' => $width, + 'height' => $height, + 'extension' => $arguments['extension'], + 'transparent_color' => $this->getToolkit()->getTransparentColor() + ); + 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; + } + // In case of error, reset resource and type to as it was. + $this->getToolkit()->setResource($original_resource); + $this->getToolkit()->setType($original_type); } - imagedestroy($this->getToolkit()->getResource()); - - // Update the image object. - $this->getToolkit()->setType($type); - $this->getToolkit()->setResource($res); - - return TRUE; + return FALSE; } } 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 3a762e1..126f53c 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -81,12 +81,7 @@ protected function validateArguments(array $arguments) { */ protected function execute(array $arguments) { // Get the image type. - $type = FALSE; - foreach ($this->getToolkit()->supportedTypes() as $type) { - if (image_type_to_extension($type, FALSE) === $arguments['extension']) { - break; - } - } + $type = $this->getToolkit()->extensionToImageType($arguments['extension']); // Create the resource. if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) {