Skip to content
Snippets Groups Projects
Commit f1c39149 authored by catch's avatar catch
Browse files

Issue #3156887 by andypost, mondrake, ayushmishra206, alexpott, Ayesh,...

Issue #3156887 by andypost, mondrake, ayushmishra206, alexpott, Ayesh, longwave: \Drupal\system\Plugin\ImageToolkit\GDToolkit needs to support \GdImage objects for PHP 8 compatibility
parent d7758d65
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ class GDToolkit extends ImageToolkitBase {
/**
* A GD image resource.
*
* @var resource|null
* @var resource|\GdImage|null
*/
protected $resource = NULL;
......@@ -98,6 +98,8 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi
* 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)) {
......@@ -124,15 +126,20 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* Sets the GD image resource.
*
* @param resource $resource
* @param resource|\GdImage $resource
* The GD image resource.
*
* @return $this
* An instance of the current toolkit object.
*/
public function setResource($resource) {
if (!is_resource($resource) || get_resource_type($resource) != 'gd') {
throw new \InvalidArgumentException('Invalid resource argument');
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') {
throw new \InvalidArgumentException('Invalid resource argument');
}
}
$this->preLoadInfo = NULL;
$this->resource = $resource;
......@@ -142,11 +149,12 @@ public function setResource($resource) {
/**
* Retrieves the GD image resource.
*
* @return resource|null
* @return resource|\GdImage|null
* The GD image resource, or NULL if not available.
*/
public function getResource() {
if (!is_resource($this->resource)) {
// @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))) {
$this->load();
}
return $this->resource;
......
......@@ -428,8 +428,13 @@ public function testManipulations() {
/**
* 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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment