diff --git a/src/Plugin/ImageToolkit/Operation/imagemagick/TextToWrapper.php b/src/Plugin/ImageToolkit/Operation/imagemagick/TextToWrapper.php index 261c102..82c2b94 100644 --- a/src/Plugin/ImageToolkit/Operation/imagemagick/TextToWrapper.php +++ b/src/Plugin/ImageToolkit/Operation/imagemagick/TextToWrapper.php @@ -29,14 +29,31 @@ class TextToWrapper extends ImagemagickImageToolkitOperationBase { $gd_wrapper->apply('text_to_wrapper', $arguments); // Flush the temporary wrapper to disk, reopen via ImageMagick and return. if ($gd_wrapper) { - $tmp_file = \Drupal::service('file_system')->tempnam('temporary://', 'image_effects_'); + // Temporary file prefix is limited to 3 chars for Windows compatibility. + $tmp_file = \Drupal::service('file_system')->tempnam('temporary://', 'ifx'); $gd_wrapper_destination = $tmp_file . '.png'; file_unmanaged_move($tmp_file, $gd_wrapper_destination, FILE_CREATE_DIRECTORY); $gd_wrapper->save($gd_wrapper_destination); $tmp_wrapper = \Drupal::service('image.factory')->get($gd_wrapper_destination, 'imagemagick'); + // Remove temporary files after they've been processed + register_shutdown_function([ + '\Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\TextToWrapper', + 'delete', + ], $gd_wrapper_destination); return $this->getToolkit()->apply('replace_image', ['replacement_image' => $tmp_wrapper]); } return FALSE; } + /** + * Delete the image effect temporary file after it's been used + * + * @param string $file_path + * Path of the file that's about to be deleted + */ + public static function delete($file_path) { + if (file_exists($file_path)) { + file_unmanaged_delete($file_path); + } + } } diff --git a/tests/src/Functional/Effect/TextOverlayTest.php b/tests/src/Functional/Effect/TextOverlayTest.php index 6e6dfbc..c7a84d8 100644 --- a/tests/src/Functional/Effect/TextOverlayTest.php +++ b/tests/src/Functional/Effect/TextOverlayTest.php @@ -91,6 +91,12 @@ class TextOverlayTest extends ImageEffectsTestBase { ]; $this->addEffectToTestStyle($effect_config); + // Check that no temporary files are left in Imagemagick. + if ($toolkit_id === 'imagemagick') { + $directory_scan = file_scan_directory('temporary://', '/ifx.*/'); + $this->assertEquals(0, count($directory_scan)); + } + $test_data = [ [ 'test_file' => $this->getTestImageCopyUri('/files/image-test.png', 'simpletest'),