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/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/image/src/Plugin/ImageEffect/ConvertImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php index 5091e51..50ceed3 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php @@ -18,7 +18,7 @@ * @ImageEffect( * id = "image_convert", * label = @Translation("Convert"), - * description = @Translation("Converts an image between formats (e.g. from PNG to JPEG).") + * description = @Translation("Converts an image between extensions (e.g. from PNG to JPEG).") * ) */ class ConvertImageEffect extends ConfigurableImageEffectBase { @@ -33,7 +33,7 @@ public function transformDimensions(array &$dimensions) { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->convert($this->configuration['format'])) { + if (!$image->convert($this->configuration['extension'])) { $this->logger->error('Image convert failed using the %toolkit toolkit on %path (%mimetype)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType())); return FALSE; } @@ -46,14 +46,14 @@ public function applyEffect(ImageInterface $image) { 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['format']); + $mime_type = \Drupal::service('file.mime_type.guesser.extension')->guess('test.' . $this->configuration['extension']); } /** * {@inheritdoc} */ public function getDerivativeExtension(&$extension) { - $extension = $this->configuration['format']; + $extension = $this->configuration['extension']; } /** @@ -61,7 +61,7 @@ public function getDerivativeExtension(&$extension) { */ public function getSummary() { $summary = array( - '#markup' => Unicode::strtoupper($this->configuration['format']), + '#markup' => Unicode::strtoupper($this->configuration['extension']), ); $summary += parent::getSummary(); @@ -73,7 +73,7 @@ public function getSummary() { */ public function defaultConfiguration() { return array( - 'format' => NULL, + 'extension' => NULL, ); } @@ -86,10 +86,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $extensions, array_map(array('\Drupal\Component\Utility\Unicode', 'strtoupper'), $extensions) ); - $form['format'] = array( + $form['extension'] = array( '#type' => 'select', - '#title' => t('Format'), - '#default_value' => $this->configuration['format'], + '#title' => t('Extension'), + '#default_value' => $this->configuration['extension'], '#required' => TRUE, '#options' => $options, ); @@ -101,7 +101,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); - $this->configuration['format'] = $form_state->getValue('format'); + $this->configuration['extension'] = $form_state->getValue('extension'); } } diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index 609c356..1549d5b 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -92,14 +92,35 @@ function testCropEffect() { * Test the image_convert_effect() function. */ function testConvertEffect() { + // Test jpg. $this->assertImageEffect('image_convert', array( - 'format' => 'png', + 'extension' => 'jpg', )); $this->assertToolkitOperationsCalled(array('convert')); // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['convert'][0][0], 'png', 'Format was passed correctly'); + $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'); } /** diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 98ae863..14ee446 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -375,7 +375,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); } } 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..d9c591e 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 with the specified extension.") * ) */ class Convert extends GDImageToolkitOperationBase { @@ -27,8 +27,8 @@ class Convert extends GDImageToolkitOperationBase { */ protected function arguments() { return array( - 'format' => array( - 'description' => 'The new format of the converted image', + 'extension' => array( + 'description' => 'The new extension of the converted image', ), ); } @@ -37,8 +37,8 @@ protected function arguments() { * {@inheritdoc} */ protected function validateArguments(array $arguments) { - if (!in_array($arguments['format'], $this->getToolkit()->getSupportedExtensions())) { - throw new \InvalidArgumentException(String::format("Invalid format (@value) specified for the image 'convert' operation", array('@value' => $arguments['format']))); + if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) { + throw new \InvalidArgumentException(String::format("Invalid extension (@value) specified for the image 'convert' operation", array('@value' => $arguments['extension']))); } return $arguments; } @@ -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['extension']) { 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..40a8691 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. @@ -174,11 +174,26 @@ function testManipulations() { 'height' => 8, 'corners' => array_fill(0, 4, $this->black), ), - 'convert' => array( + 'convert_jpg' => array( 'function' => 'convert', 'width' => 40, 'height' => 20, - 'arguments' => array('format' => 'jpeg'), + 'arguments' => array('extension' => 'jpeg'), + 'corners' => $default_corners, + ), + 'convert_gif' => array( + 'function' => 'convert', + 'width' => 40, + 'height' => 20, + 'arguments' => array('extension' => 'gif'), + 'corners' => $default_corners, + ), + 'convert_png' => array( + 'function' => 'convert', + 'width' => 40, + 'height' => 20, + 'arguments' => array('extension' => 'png'), + 'corners' => $default_corners, ), ); 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); }