diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index ae07fe2..3094c9c 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -149,16 +149,17 @@ public function scale($width, $height = NULL, $upscale = FALSE); public function scaleAndCrop($width, $height); /** - * Instructs the toolkit to save the image with the specified extension. + * Instructs the toolkit to save the image in the format specified by the + * extension. * * @param string $extension - * The extension to convert to (e.g. 'jpeg', 'png'). Allowed values depend - * on the implementation of the convert operation of the image toolkit. - * For a working example, see - * \Drupal\system\Plugin\ImageToolkit\Operation\gd\Convert. + * The extension to convert to (e.g. 'jpeg' or 'png'). Allowed values depend + * on the current image toolkit. * * @return bool * TRUE on success, FALSE on failure. + * + * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions() */ public function convert($extension); diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 88cd42b..6dc7ada 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -310,19 +310,11 @@ public function transformDimensions(array &$dimensions) { /** * {@inheritdoc} */ - public function getDerivativeMimeType(&$mime_type) { - foreach ($this->getEffects() as $effect) { - $effect->getDerivativeMimeType($mime_type); - } - } - - /** - * {@inheritdoc} - */ - public function getDerivativeExtension(&$extension) { + public function getDerivativeExtension($extension) { foreach ($this->getEffects() as $effect) { $effect->getDerivativeExtension($extension); } + return $extension; } /** @@ -435,7 +427,7 @@ protected function getHashSalt() { * * If this image style changes the extension of the derivative, this method * adds the new extension to the given path. This way we avoid filename - * clashes and make it easy to find the source image. + * clashes while still allowing us to find the source image. * * @param string $path * The path to add the extension to. @@ -447,7 +439,7 @@ protected function getHashSalt() { protected function addExtension($path) { $extension = pathinfo($path, PATHINFO_EXTENSION); $original_extension = $extension; - $this->getDerivativeExtension($extension); + $extension = $this->getDerivativeExtension($extension); if ($original_extension !== $extension) { $path .= '.' . $extension; } @@ -467,8 +459,8 @@ protected function addExtension($path) { * @todo: Remove when https://www.drupal.org/node/2050759 is in. * * @return string - * A string containing the name of the scheme, or FALSE if none. For example, - * the URI "public://example.txt" would return "public". + * A string containing the name of the scheme, or FALSE if none. For + * example, the URI "public://example.txt" would return "public". */ protected function fileUriScheme($uri) { return file_uri_scheme($uri); @@ -502,7 +494,7 @@ protected function fileUriTarget($uri) { * * @todo: Convert file_default_scheme() into a proper injectable service. * - * @return + * @return string * 'public', 'private' or any other file scheme defined as the default. */ protected function fileDefaultScheme() { diff --git a/core/modules/image/src/ImageEffectBase.php b/core/modules/image/src/ImageEffectBase.php index 559a5a6..3b3bbb7 100644 --- a/core/modules/image/src/ImageEffectBase.php +++ b/core/modules/image/src/ImageEffectBase.php @@ -77,13 +77,11 @@ public function transformDimensions(array &$dimensions) { /** * {@inheritdoc} */ - public function getDerivativeMimeType(&$mime_type) { - } - - /** - * {@inheritdoc} - */ - public function getDerivativeExtension(&$extension) { + public function getDerivativeExtension($extension) { + // Most image effects will not change the extension. This base + // implementation represents this behavior. Override this method if your + // image effect does change the extension. + return $extension; } /** diff --git a/core/modules/image/src/ImageEffectInterface.php b/core/modules/image/src/ImageEffectInterface.php index 5b272f9..790d7b2 100644 --- a/core/modules/image/src/ImageEffectInterface.php +++ b/core/modules/image/src/ImageEffectInterface.php @@ -44,20 +44,16 @@ public function applyEffect(ImageInterface $image); public function transformDimensions(array &$dimensions); /** - * Determines the MIME type of the derivative without generating it. - * - * @param string $mime_type - * The MIME type to be set to the derivative's MIME type. - */ - public function getDerivativeMimeType(&$mime_type); - - /** - * Determines the extension of the derivative without generating it. + * Returns the extension the derivative would have have after applying this + * image effect. * * @param string $extension - * The file extension to be set to the derivative's file extension. + * The file extension the derivative has before applying. + * + * @return string + * The file extension after applying. */ - public function getDerivativeExtension(&$extension); + public function getDerivativeExtension($extension); /** * Returns a render array summarizing the configuration of the image effect. diff --git a/core/modules/image/src/ImageStyleInterface.php b/core/modules/image/src/ImageStyleInterface.php index 91a2e9c..94ef73c 100644 --- a/core/modules/image/src/ImageStyleInterface.php +++ b/core/modules/image/src/ImageStyleInterface.php @@ -132,20 +132,16 @@ public function createDerivative($original_uri, $derivative_uri); public function transformDimensions(array &$dimensions); /** - * Determines the MIME type of the derivative without generating it. - * - * @param string $mime_type - * The MIME type to be set to the derivative's MIME type. - */ - public function getDerivativeMimeType(&$mime_type); - - /** * Determines the extension of the derivative without generating it. * * @param string $extension - * The file extension to be set to the derivative's file extension. + * The file extension of the original image. + * + * @return string + * The extension the derivative image will have, given the extension of the + * original. */ - public function getDerivativeExtension(&$extension); + public function getDerivativeExtension($extension); /** * Returns a specific image effect. diff --git a/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php index 50ceed3..d88712c 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php @@ -43,17 +43,8 @@ public function applyEffect(ImageInterface $image) { /** * {@inheritdoc} */ - public function getDerivativeMimeType(&$mime_type) { - // The guess method on the MIME type guesser needs a full filename. It does - // not work with just an extension. - $mime_type = \Drupal::service('file.mime_type.guesser.extension')->guess('test.' . $this->configuration['extension']); - } - - /** - * {@inheritdoc} - */ - public function getDerivativeExtension(&$extension) { - $extension = $this->configuration['extension']; + public function getDerivativeExtension($extension) { + return $this->configuration['extension']; } /** diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index 1549d5b..0f19f8b 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -89,38 +89,18 @@ function testCropEffect() { } /** - * Test the image_convert_effect() function. + * Tests the ConvertImageEffect plugin. */ function testConvertEffect() { - // Test jpg. + // Test jpeg. $this->assertImageEffect('image_convert', array( - 'extension' => 'jpg', + 'extension' => 'jpeg', )); $this->assertToolkitOperationsCalled(array('convert')); // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['convert'][0][0], 'jpg', 'Extension was passed correctly'); - - // Test gif. - $this->assertImageEffect('image_convert', array( - 'extension' => 'gif', - )); - $this->assertToolkitOperationsCalled(array('convert')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['convert'][0][0], 'gif', 'Extension was passed correctly'); - - // Test png. - $this->assertImageEffect('image_convert', array( - 'extension' => 'png', - )); - $this->assertToolkitOperationsCalled(array('convert')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['convert'][0][0], 'png', 'Extension was passed correctly'); + $this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly'); } /** diff --git a/core/modules/image/src/Tests/ImageStyleTest.php b/core/modules/image/src/Tests/ImageStyleTest.php index 0a005cb..97095ba 100644 --- a/core/modules/image/src/Tests/ImageStyleTest.php +++ b/core/modules/image/src/Tests/ImageStyleTest.php @@ -105,28 +105,6 @@ public function setUp() { } /** - * @covers ::getDerivativeMimeType - */ - public function testGetDerivativeMimeType() { - $image_effect_id = $this->randomMachineName(); - $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); - $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') - ->setConstructorArgs(array(array(), $image_effect_id, array(), $logger)) - ->getMock(); - $image_effect->expects($this->any()) - ->method('getDerivativeMimeType') - ->will($this->returnCallback(function (&$mime_type) { $mime_type = 'image/webp';})); - - $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); - - $mime_types = array('image/jpeg', 'image/gif', 'image/png'); - foreach ($mime_types as $mime_type) { - $image_style->getDerivativeMimeType($mime_type); - $this->assertEquals($mime_type, 'image/webp'); - } - } - - /** * @covers ::getDerivativeExtension */ public function testGetDerivativeExtension() { @@ -137,14 +115,14 @@ public function testGetDerivativeExtension() { ->getMock(); $image_effect->expects($this->any()) ->method('getDerivativeExtension') - ->will($this->returnCallback(function (&$extension) { $extension = 'png';})); + ->will($this->returnValue('png')); $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); $extensions = array('jpeg', 'gif', 'png'); foreach ($extensions as $extension) { - $image_style->getDerivativeExtension($extension); - $this->assertEquals($extension, 'png'); + $extensionReturned = $image_style->getDerivativeExtension($extension); + $this->assertEquals($extensionReturned, 'png'); } } @@ -160,7 +138,7 @@ public function testBuildUri() { ->getMock(); $image_effect->expects($this->any()) ->method('getDerivativeExtension') - ->will($this->returnCallback(function (&$extension) { $extension = 'png';})); + ->will($this->returnValue('png')); $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); $this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg.png'); @@ -172,7 +150,7 @@ public function testBuildUri() { ->getMock(); $image_effect->expects($this->any()) ->method('getDerivativeExtension') - ->will($this->returnCallback(function (&$extension) {})); + ->will($this->returnArgument(0)); $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); $this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg'); @@ -193,7 +171,7 @@ public function testGetPathToken() { ->getMock(); $image_effect->expects($this->any()) ->method('getDerivativeExtension') - ->will($this->returnCallback(function (&$extension) { $extension = 'png';})); + ->will($this->returnValue('png')); $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, array('getPrivateKey', 'getHashSalt')); $image_style->expects($this->any()) @@ -215,7 +193,7 @@ public function testGetPathToken() { ->getMock(); $image_effect->expects($this->any()) ->method('getDerivativeExtension') - ->will($this->returnCallback(function (&$extension) {})); + ->will($this->returnArgument(0)); $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, array('getPrivateKey', 'getHashSalt')); $image_style->expects($this->any()) diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 14ee446..052bad9 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -369,13 +369,34 @@ public static function getSupportedExtensions() { } /** + * Returns the IMAGETYPE_xxx constant for the given extension. + * + * This is the reverse of the @see image_type_to_extension() function. + * + * @param string $extension + * The extension to get the IMAGETYPE_xxx constant for. + * + * @return int + * The IMAGETYPE_xxx constant for the given extension, or IMAGETYPE_UNKNOWN + * for unsupported extensions. + */ + public function extensionToImageType($extension) { + foreach ($this->supportedTypes() as $type) { + if (image_type_to_extension($type) === $extension) { + return $type; + } + } + return IMAGETYPE_UNKNOWN; + } + + /** * Returns a list of image types supported by the toolkit. * * @return array * 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 d9c591e..49f7fbe 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php @@ -17,7 +17,7 @@ * toolkit = "gd", * operation = "convert", * label = @Translation("Convert"), - * description = @Translation("Instructs the toolkit to save the image with the specified extension.") + * description = @Translation("Instructs the toolkit to save the image with a specified extension.") * ) */ class Convert extends GDImageToolkitOperationBase { @@ -46,20 +46,13 @@ protected function validateArguments(array $arguments) { /** * {@inheritdoc} */ - protected function execute(array $arguments = array()) { - // Make sure we have an image type. - $type = FALSE; - foreach ($this->getToolkit()->supportedTypes() as $type) { - if (image_type_to_extension($type) === $arguments['extension']) { - break; - } - } + 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; } - imagedestroy($this->getToolkit()->getResource()); // Update the image object. diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php index b1b31a7..f72eaaa 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php @@ -255,7 +255,7 @@ public static function getSupportedExtensions() { * 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); }