:octocat: integrate reader into QRCode, extract LuminanceSourceInterface

This commit is contained in:
codemasher
2021-11-19 15:49:53 +01:00
parent 9bfe5e1880
commit 5e9cf0047e
9 changed files with 170 additions and 158 deletions
+16 -3
View File
@@ -14,14 +14,15 @@
namespace chillerlan\QRCode\Decoder;
use InvalidArgumentException;
use function get_resource_type, imagecolorat, imagecolorsforindex, imagesx, imagesy, is_resource;
use function file_get_contents, get_resource_type, imagecolorat, imagecolorsforindex,
imagecreatefromstring, imagesx, imagesy, is_resource;
use const PHP_MAJOR_VERSION;
/**
* This class is used to help decode images from files which arrive as GD Resource
* It does not support rotation.
*/
final class GDLuminanceSource extends LuminanceSource{
final class GDLuminanceSource extends LuminanceSourceAbstract{
/**
* @var resource|\GdImage
@@ -40,7 +41,7 @@ final class GDLuminanceSource extends LuminanceSource{
/** @noinspection PhpFullyQualifiedNameUsageInspection */
if(
(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage)
|| (PHP_MAJOR_VERSION < 8 && (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd'))
|| (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd')
){
throw new InvalidArgumentException('Invalid GD image source.');
}
@@ -66,4 +67,16 @@ final class GDLuminanceSource extends LuminanceSource{
}
}
/** @inheritDoc */
public static function fromFile(string $path):self{
$path = self::checkFile($path);
return new self(imagecreatefromstring(file_get_contents($path)));
}
/** @inheritDoc */
public static function fromBlob(string $blob):self{
return new self(imagecreatefromstring($blob));
}
}