diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 54e101e..9f9f8f2 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -151,8 +151,8 @@ public function apply($operation, array $arguments = array()) { /** * {@inheritdoc} */ - public function setNew($width, $height, $mime_type = 'image/png', $transparent_color = '#FFFFFF') { - return $this->apply('set_new', array('width' => $width, 'height' => $height, 'mime_type' => $mime_type, 'transparent_color' => $transparent_color)); + public function createNew($width, $height, $mime_type = 'image/png', $transparent_color = '#FFFFFF') { + return $this->apply('create_new', array('width' => $width, 'height' => $height, 'mime_type' => $mime_type, 'transparent_color' => $transparent_color)); } /** diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index beb332b..f60aaa8 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -126,7 +126,7 @@ public function save($destination = NULL); * @return bool * TRUE on success, FALSE on failure. */ - public function setNew($width, $height, $mime_type = 'image/png', $transparent_color = '#FFFFFF'); + public function createNew($width, $height, $mime_type = 'image/png', $transparent_color = '#FFFFFF'); /** * Scales an image while maintaining aspect ratio. diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php index 328c2be..e4e9b9f 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -71,15 +71,6 @@ protected function getToolkit() { } /** - * Returns the image instance for this operation. - * - * @return \Drupal\Core\Image\ImageInterface - */ - protected function getImage() { - return $this->getToolkit()->getImage(); - } - - /** * Returns the definition of the operation arguments. * * Image toolkit operation implementers must implement this method to diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index d98d843..56324f5 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -125,7 +125,13 @@ protected function load() { // Convert indexed images to truecolor, copying the image to a new // truecolor resource, so that filters work correctly and don't result // in unnecessary dither. - if ($this->getImage()->setNew(imagesx($resource), imagesy($resource), $this->getMimeType(), $this->getTransparentColor())) { + $data = array( + 'width' => imagesx($resource), + 'height' => imagesy($resource), + 'mime_type' => $this->getMimeType(), + 'transparent_color' => $this->getTransparentColor(), + ); + 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/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php new file mode 100644 index 0000000..1692cbb --- /dev/null +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -0,0 +1,94 @@ + array( + 'description' => 'The width of the image, in pixels', + ), + 'height' => array( + 'description' => 'The height of the image, in pixels', + ), + 'mime_type' => array( + 'description' => 'The MIME type of the image', + 'required' => FALSE, + 'default' => 'image/png', + ), + 'transparent_color' => array( + 'description' => 'The RGB hex color for GIF transparency', + 'required' => FALSE, + 'default' => '#FFFFFF', + ), + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(array $arguments) { + if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) { + return FALSE; + } + switch ($arguments['mime_type']) { + case 'image/png': + imagealphablending($res, FALSE); + $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); + imagefill($res, 0, 0, $transparency); + imagealphablending($res, TRUE); + imagesavealpha($res, TRUE); + $this->getToolkit()->setType(IMAGETYPE_PNG); + break; + + case 'image/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); + $this->getToolkit()->setType(IMAGETYPE_GIF); + break; + + case 'image/jpeg': + imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); + $this->getToolkit()->setType(IMAGETYPE_JPEG); + break; + + default: + return FALSE; + + } + + $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 2091cfd..5529129 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -84,7 +84,13 @@ protected function execute(array $arguments) { // the original resource on it with resampling. Destroy the original // resource upon success. $original_resource = $this->getToolkit()->getResource(); - if ($this->getImage()->setNew($arguments['width'], $arguments['height'], $this->getToolkit()->getMimeType(), $this->getToolkit()->getTransparentColor())) { + $data = array( + 'width' => $arguments['width'], + 'height' => $arguments['height'], + 'mime_type' => $this->getToolkit()->getMimeType(), + 'transparent_color' => $this->getToolkit()->getTransparentColor() + ); + 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'])) { return FALSE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php index 4384453..09457df 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php @@ -34,7 +34,7 @@ protected function arguments() { protected function execute(array $arguments) { // PHP installations using non-bundled GD do not have imagefilter. if (!function_exists('imagefilter')) { - $this->logger->notice("The image '@file' could not be desaturated because the imagefilter() function is not available in this PHP installation.", array('@file' => $this->getImage()->getSource())); + $this->logger->notice("The image '@file' could not be desaturated because the imagefilter() function is not available in this PHP installation.", array('@file' => $this->getToolkit()->getImage()->getSource())); return FALSE; } 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 90568ae..4bf7e65 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -63,7 +63,13 @@ protected function execute(array $arguments = array()) { // the original resource on it with resampling. Destroy the original // resource upon success. $original_resource = $this->getToolkit()->getResource(); - if ($this->getImage()->setNew($arguments['width'], $arguments['height'], $this->getToolkit()->getMimeType(), $this->getToolkit()->getTransparentColor())) { + $data = array( + 'width' => $arguments['width'], + 'height' => $arguments['height'], + 'mime_type' => $this->getToolkit()->getMimeType(), + 'transparent_color' => $this->getToolkit()->getTransparentColor() + ); + 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))) { return FALSE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php index 15a7293..48d2549 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -42,7 +42,7 @@ protected function arguments() { protected function execute(array $arguments) { // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { - $this->logger->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getImage()->getSource())); + $this->logger->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getToolkit()->getImage()->getSource())); return FALSE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/SetNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/SetNew.php deleted file mode 100644 index 197916b..0000000 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/SetNew.php +++ /dev/null @@ -1,94 +0,0 @@ - array( - 'description' => 'The width of the image, in pixels', - ), - 'height' => array( - 'description' => 'The height of the image, in pixels', - ), - 'mime_type' => array( - 'description' => 'The MIME type of the image', - 'required' => FALSE, - 'default' => 'image/png', - ), - 'transparent_color' => array( - 'description' => 'The RGB hex color for GIF transparency', - 'required' => FALSE, - 'default' => '#FFFFFF', - ), - ); - } - - /** - * {@inheritdoc} - */ - protected function execute(array $arguments) { - if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) { - return FALSE; - } - switch ($arguments['mime_type']) { - case 'image/png': - imagealphablending($res, FALSE); - $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); - imagefill($res, 0, 0, $transparency); - imagealphablending($res, TRUE); - imagesavealpha($res, TRUE); - $this->getToolkit()->setType(IMAGETYPE_PNG); - break; - - case 'image/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); - $this->getToolkit()->setType(IMAGETYPE_GIF); - break; - - case 'image/jpeg': - imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); - $this->getToolkit()->setType(IMAGETYPE_JPEG); - break; - - default: - return FALSE; - - } - - $this->getToolkit()->setResource($res); - return TRUE; - } - -} diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php index 6a8b2f0..70e6273 100644 --- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php @@ -122,8 +122,8 @@ function testManipulations() { // Setup a list of tests to perform on each type. $operations = array( - 'set_new' => array( - 'function' => 'set_new', + 'create_new' => array( + 'function' => 'create_new', 'arguments' => array('width' => 20, 'height' => 10, 'transparent_color' => '#ffff00'), // Yellow color for transparency of GIF file. 'width' => 20, 'height' => 10, @@ -259,9 +259,9 @@ function testManipulations() { } } - // SetNew operation requires specifying MIME type, and color at corners + // CreateNew operation requires specifying MIME type, and color at corners // is dependent on that. - if ($op == 'set_new') { + if ($op == 'create_new') { $values['arguments']['mime_type'] = $image->getMimeType(); if ($image->getToolkit()->getType() != IMAGETYPE_JPEG) { $values['corners'] = array_fill(0, 4, $this->transparent); @@ -296,7 +296,7 @@ function testManipulations() { $this->assertTrue($correct_dimensions_object, String::format('Image %file object after %action action is reporting the proper height and width values.', array('%file' => $file, '%action' => $op))); // JPEG colors will always be messed up due to compression. - if ($image->getToolkit()->getType() != IMAGETYPE_JPEG || $op == 'set_new') { + if ($image->getToolkit()->getType() != IMAGETYPE_JPEG || $op == 'create_new') { // Now check each of the corners to ensure color correctness. foreach ($values['corners'] as $key => $corner) { // Get the location of the corner. @@ -329,7 +329,7 @@ function testManipulations() { // Check that expected color (yellow) was saved as transparent // in GIF file. - if ($image_reloaded->getToolkit()->getType() == IMAGETYPE_GIF && $op == 'set_new') { + if ($image_reloaded->getToolkit()->getType() == IMAGETYPE_GIF && $op == 'create_new') { $this->assertEqual('#ffff00', $image_reloaded->getToolkit()->getTransparentColor(), String::format('Image file %file has the correct transparent color channel set.', array('%file' => $op . image_type_to_extension($image->getToolkit()->getType())))); } } @@ -338,7 +338,7 @@ function testManipulations() { // Test creation of image from scratch, and saving to storage. foreach (array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG) as $type) { $image = $this->imageFactory->get(); - $image->setNew(50, 20, image_type_to_mime_type($type), '#ffff00'); + $image->createNew(50, 20, image_type_to_mime_type($type), '#ffff00'); $file = 'from_null' . image_type_to_extension($type); $file_path = $directory . '/' . $file ; $this->assertEqual(50, $image->getWidth(), String::format('Image file %file has the correct width.', array('%file' => $file)));