mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-30 03:58:28 +00:00
:octocat: optional color negation in the reader
This commit is contained in:
@@ -16,7 +16,7 @@ namespace chillerlan\QRCode\Decoder;
|
||||
use chillerlan\Settings\SettingsContainerInterface;
|
||||
use function file_get_contents, get_resource_type, imagecolorat, imagecolorsforindex,
|
||||
imagecreatefromstring, imagefilter, imagesx, imagesy, is_resource;
|
||||
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, PHP_MAJOR_VERSION;
|
||||
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, IMG_FILTER_NEGATE, PHP_MAJOR_VERSION;
|
||||
|
||||
/**
|
||||
* This class is used to help decode images from files which arrive as GD Resource
|
||||
@@ -41,7 +41,7 @@ class GDLuminanceSource extends LuminanceSourceAbstract{
|
||||
|
||||
/** @noinspection PhpFullyQualifiedNameUsageInspection */
|
||||
if(
|
||||
(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage)
|
||||
(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage) // @todo: remove version check in v6
|
||||
|| (PHP_MAJOR_VERSION < 8 && (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd'))
|
||||
){
|
||||
throw new QRCodeDecoderException('Invalid GD image source.'); // @codeCoverageIgnore
|
||||
@@ -51,6 +51,19 @@ class GDLuminanceSource extends LuminanceSourceAbstract{
|
||||
|
||||
$this->gdImage = $gdImage;
|
||||
|
||||
if($this->options->readerGrayscale){
|
||||
imagefilter($this->gdImage, IMG_FILTER_GRAYSCALE);
|
||||
}
|
||||
|
||||
if($this->options->readerInvertColors){
|
||||
imagefilter($this->gdImage, IMG_FILTER_NEGATE);
|
||||
}
|
||||
|
||||
if($this->options->readerIncreaseContrast){
|
||||
imagefilter($this->gdImage, IMG_FILTER_BRIGHTNESS, -100);
|
||||
imagefilter($this->gdImage, IMG_FILTER_CONTRAST, -100);
|
||||
}
|
||||
|
||||
$this->setLuminancePixels();
|
||||
}
|
||||
|
||||
@@ -59,15 +72,6 @@ class GDLuminanceSource extends LuminanceSourceAbstract{
|
||||
*/
|
||||
protected function setLuminancePixels():void{
|
||||
|
||||
if($this->options->readerGrayscale){
|
||||
imagefilter($this->gdImage, IMG_FILTER_GRAYSCALE);
|
||||
}
|
||||
|
||||
if($this->options->readerIncreaseContrast){
|
||||
imagefilter($this->gdImage, IMG_FILTER_BRIGHTNESS, -100);
|
||||
imagefilter($this->gdImage, IMG_FILTER_CONTRAST, -100);
|
||||
}
|
||||
|
||||
for($j = 0; $j < $this->height; $j++){
|
||||
for($i = 0; $i < $this->width; $i++){
|
||||
$argb = imagecolorat($this->gdImage, $i, $j);
|
||||
|
||||
@@ -33,24 +33,27 @@ class IMagickLuminanceSource extends LuminanceSourceAbstract{
|
||||
|
||||
$this->imagick = $imagick;
|
||||
|
||||
$this->setLuminancePixels();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function setLuminancePixels():void{
|
||||
|
||||
if($this->options->readerGrayscale){
|
||||
$this->imagick->setImageColorspace(Imagick::COLORSPACE_GRAY);
|
||||
}
|
||||
|
||||
if($this->options->readerInvertColors){
|
||||
$this->imagick->negateImage($this->options->readerGrayscale);
|
||||
}
|
||||
|
||||
if($this->options->readerIncreaseContrast){
|
||||
for($i = 0; $i < 10; $i++){
|
||||
$this->imagick->contrastImage(false); // misleading docs
|
||||
}
|
||||
}
|
||||
|
||||
$this->setLuminancePixels();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function setLuminancePixels():void{
|
||||
$pixels = $this->imagick->exportImagePixels(1, 1, $this->width, $this->height, 'RGB', Imagick::PIXEL_CHAR);
|
||||
$count = count($pixels);
|
||||
|
||||
|
||||
@@ -466,6 +466,11 @@ trait QROptionsTrait{
|
||||
*/
|
||||
protected bool $readerGrayscale = false;
|
||||
|
||||
/**
|
||||
* Invert the colors of the image
|
||||
*/
|
||||
protected bool $readerInvertColors = false;
|
||||
|
||||
/**
|
||||
* Increase the contrast before reading
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user