diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index b8ca6a8..e0b6bda 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -149,13 +149,16 @@ public function scale($width, $height = NULL, $upscale = FALSE); public function scaleAndCrop($width, $height); /** - * Converts an image to a specified format. + * Instructs the toolkit to save the image in the specified format. * * @param string $format * The format 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. + * + * @return bool + * TRUE on success, FALSE on failure. */ public function convert($format); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 8960504..bf219b1 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -159,6 +159,15 @@ public static function isAvailable(); public static function getSupportedExtensions(); /** + * 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(); + + /** * Applies a toolkit operation to an image. * * @param string $operation diff --git a/core/modules/image/src/Controller/ImageStyleDownloadController.php b/core/modules/image/src/Controller/ImageStyleDownloadController.php index 173114e..2b2d89f 100644 --- a/core/modules/image/src/Controller/ImageStyleDownloadController.php +++ b/core/modules/image/src/Controller/ImageStyleDownloadController.php @@ -135,11 +135,15 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st // the actual source image, we remove the extension and check if that // image exists. $path_info = pathinfo($image_uri); - $image_uri = $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename']; - if (!file_exists($image_uri)) { + $converted_image_uri = $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename']; + if (!file_exists($converted_image_uri)) { $this->logger->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array('%source_image_path' => $image_uri, '%derivative_path' => $derivative_uri)); return new Response($this->t('Error generating image, missing source file.'), 404); } + else { + // The converted file does exist, use it as the source. + $image_uri = $converted_image_uri; + } } // Don't start generating the image if the derivative already exists or if diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 98ae863..70aa477 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -369,13 +369,9 @@ public static function getSupportedExtensions() { } /** - * 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.). + * {@inheritdoc} */ - protected static function supportedTypes() { + public 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 38dfff8..562ac16 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("Converts an image to the given format.") + * description = @Translation("Instructs the toolkit to save the image in the specified format.") * ) */ class Convert extends GDImageToolkitOperationBase { @@ -47,24 +47,12 @@ protected function validateArguments(array $arguments) { * {@inheritdoc} */ protected function execute(array $arguments = array()) { + // Make sure we have an image type. $type = FALSE; - switch ($arguments['format']) { - case 'jpeg': - case 'jpg': - case 'jpe': - $type = IMAGETYPE_JPEG; + foreach ($this->getToolkit()->supportedTypes() as $type) { + if (image_type_to_extension($type) === $arguments['format']) { break; - - default: - $constant = 'IMAGETYPE_' . \Drupal\Component\Utility\Unicode::strtoupper($arguments['format']); - if (defined($constant)) { - $type = constant($constant); - } - break; - } - - if (!$type) { - return FALSE; + } } $res = $this->getToolkit()->createTmp($type, $this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); @@ -74,8 +62,8 @@ protected function execute(array $arguments = array()) { imagedestroy($this->getToolkit()->getResource()); + // Update the image object. $this->getToolkit()->setType($type); - // Update image object. $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 93cc6ae..91cb065 100644 --- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php @@ -8,7 +8,7 @@ namespace Drupal\system\Tests\Image; use Drupal\Core\Image\ImageInterface; -use Drupal\simpletest\DrupalUnitTestBase; +use \Drupal\simpletest\KernelTestBase; use Drupal\Component\Utility\String; /** @@ -17,7 +17,7 @@ * * @group Image */ -class ToolkitGdTest extends DrupalUnitTestBase { +class ToolkitGdTest extends KernelTestBase { /** * The image factory service. @@ -180,6 +180,18 @@ function testManipulations() { 'height' => 20, 'arguments' => array('format' => 'jpeg'), ), + 'convert' => array( + 'function' => 'convert', + 'width' => 40, + 'height' => 20, + 'arguments' => array('format' => 'gif'), + ), + 'convert' => array( + 'function' => 'convert', + 'width' => 40, + 'height' => 20, + 'arguments' => array('format' => 'png'), + ), ); // Systems using non-bundled GD2 don't have imagerotate. Test if available. 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 f72eaaa..b1b31a7 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.). */ - protected static function supportedTypes() { + public static function supportedTypes() { return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); }