Skip to content

Commit

Permalink
Implement Driver::version() (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel authored Jan 11, 2025
1 parent 72daad8 commit f6e00ab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Drivers/Gd/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,14 @@ public function supports(string|Format|FileExtension|MediaType $identifier): boo
default => false,
};
}

/**
* Return version of GD library
*
* @return string
*/
public static function version(): string
{
return gd_info()['GD Version'];
}
}
20 changes: 20 additions & 0 deletions src/Drivers/Imagick/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __construct(
protected DriverInterface $driver,
public Imagick $imagick
) {
//
}

/**
Expand Down Expand Up @@ -151,4 +152,23 @@ public function supports(string|Format|FileExtension|MediaType $identifier): boo

return count(Imagick::queryFormats($format->name)) >= 1;
}

/**
* Return version of ImageMagick library
*
* @throws DriverException
* @return string
*/
public static function version(): string
{
$pattern = '/^ImageMagick (?P<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)' .
'(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?' .
'(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)/';

if (preg_match($pattern, Imagick::getVersion()['versionString'], $matches) !== 1) {
throw new DriverException('Unable to read ImageMagick version number.');
}

return $matches['version'];
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Drivers/Gd/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,9 @@ public static function supportsDataProvider(): Generator
yield [false, 'foo'];
yield [false, ''];
}

public function testVersion(): void
{
$this->assertTrue(is_string($this->driver->version()));
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Drivers/Imagick/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,9 @@ public static function supportsDataProvider(): Generator
yield [false, 'foo'];
yield [false, ''];
}

public function testVersion(): void
{
$this->assertTrue(is_string($this->driver->version()));
}
}

0 comments on commit f6e00ab

Please sign in to comment.