Commit 3893f538 authored by mondrake's avatar mondrake Committed by mondrake
Browse files

Issue #3046522 by mondrake, fietserwin, y_h: Remove imagemagick texttowrapper...

Issue #3046522 by mondrake, fietserwin, y_h: Remove imagemagick texttowrapper temporary files on shutdown
parent c043825e
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -29,14 +29,29 @@ 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');
      // Defer removal of the temporary file to after it has been processed.
      drupal_register_shutdown_function([static::class, 'deleteTempFile'], $gd_wrapper_destination);
      return $this->getToolkit()->apply('replace_image', ['replacement_image' => $tmp_wrapper]);
    }
    return FALSE;
  }

  /**
   * Delete the image effect temporary file after it has been used.
   *
   * @param string $file_path
   *   Path of the file that is about to be deleted.
   */
  public static function deleteTempFile($file_path) {
    if (file_exists($file_path)) {
      file_unmanaged_delete($file_path);
    }
  }

}
+6 −0
Original line number Diff line number Diff line
@@ -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'),