Verified Commit 54a3f555 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3173031 by Spokje, andypost, mondrake: Clean-up...

Issue #3173031 by Spokje, andypost, mondrake: Clean-up \Drupal\system\Plugin\ImageToolkit\GDToolkit when core require php 8.0

(cherry picked from commit 7f337ca7)
parent 88864fe0
Loading
Loading
Loading
Loading
+6 −25
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ class GDToolkit extends ImageToolkitBase {
  /**
   * A GD image resource.
   *
   * @var resource|\GdImage|null
   * @var \GdImage|null
   */
  protected $resource = NULL;

@@ -96,19 +96,6 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi
    $this->fileSystem = $file_system;
  }

  /**
   * Destructs a GDToolkit object.
   *
   * Frees memory associated with a GD image resource.
   *
   * @todo Remove the method for PHP 8.0+ https://www.drupal.org/node/3173031
   */
  public function __destruct() {
    if (is_resource($this->resource)) {
      imagedestroy($this->resource);
    }
  }

  /**
   * {@inheritdoc}
   */
@@ -128,21 +115,16 @@ public static function create(ContainerInterface $container, array $configuratio
  /**
   * Sets the GD image resource.
   *
   * @param resource|\GdImage $resource
   * @param \GdImage $resource
   *   The GD image resource.
   *
   * @return $this
   *   An instance of the current toolkit object.
   */
  public function setResource($resource) {
    if (!(is_object($resource) && $resource instanceof \GdImage)) {
      // Since PHP 8.0 resource should be \GdImage, for previous versions it
      // should be resource.
      // @TODO clean-up for PHP 8.0+ https://www.drupal.org/node/3173031
      if (!is_resource($resource) || get_resource_type($resource) != 'gd') {
    if (!$resource instanceof \GdImage) {
      throw new \InvalidArgumentException('Invalid resource argument');
    }
    }
    $this->preLoadInfo = NULL;
    $this->resource = $resource;
    return $this;
@@ -151,12 +133,11 @@ public function setResource($resource) {
  /**
   * Retrieves the GD image resource.
   *
   * @return resource|\GdImage|null
   * @return \GdImage|null
   *   The GD image resource, or NULL if not available.
   */
  public function getResource() {
    // @TODO clean-up for PHP 8.0+ https://www.drupal.org/node/3173031
    if (!(is_resource($this->resource) || (is_object($this->resource) && $this->resource instanceof \GdImage))) {
    if (!$this->resource) {
      $this->load();
    }
    return $this->resource;
+0 −33
Original line number Diff line number Diff line
@@ -436,39 +436,6 @@ public function testManipulations() {
    $this->assertTrue($image->isValid(), 'CreateNew with valid arguments validates the Image.');
  }

  /**
   * Tests that GD resources are freed from memory.
   *
   * @todo Remove the method for PHP 8.0+ https://www.drupal.org/node/3179058
   */
  public function testResourceDestruction() {
    if (PHP_VERSION_ID >= 80000) {
      $this->markTestSkipped('In PHP8 resources are no longer used. \GdImage objects are used instead. These will be garbage collected like the regular objects they are.');
    }
    // Test that an Image object going out of scope releases its GD resource.
    $image = $this->imageFactory->get('core/tests/fixtures/files/image-test.png');
    $res = $image->getToolkit()->getResource();
    $this->assertIsResource($res);
    $image = NULL;
    // @todo In https://www.drupal.org/node/3133236 convert this to
    //   $this->assertIsNotResource($res).
    $this->assertFalse(is_resource($res), 'Image resource was destroyed after losing scope.');

    // Test that 'create_new' operation does not leave orphaned GD resources.
    $image = $this->imageFactory->get('core/tests/fixtures/files/image-test.png');
    $old_res = $image->getToolkit()->getResource();
    // Check if resource has been created successfully.
    $this->assertIsResource($old_res);
    $image->createNew(20, 20);
    $new_res = $image->getToolkit()->getResource();
    // Check if the original resource has been destroyed.
    // @todo In https://www.drupal.org/node/3133236 convert this to
    //   $this->assertIsNotResource($old_res).
    $this->assertFalse(is_resource($old_res));
    // Check if a new resource has been created successfully.
    $this->assertIsResource($new_res);
  }

  /**
   * Tests for GIF images with transparency.
   */