diff --git a/src/Mike42/GfxPhp/Codec/Gif/GifGraphicsBlock.php b/src/Mike42/GfxPhp/Codec/Gif/GifGraphicsBlock.php index e2b3b5d..6a53523 100644 --- a/src/Mike42/GfxPhp/Codec/Gif/GifGraphicsBlock.php +++ b/src/Mike42/GfxPhp/Codec/Gif/GifGraphicsBlock.php @@ -48,10 +48,18 @@ public static function fromBin(DataInputStream $in) : GifGraphicsBlock $blockId = $peek[0]; $extensionId = $peek[1]; } - if ($blockId == GifData::GIF_EXTENSION && $extensionId == GifData::GIF_EXTENSION_APPLICATION) { - // ImageMagick drops an 'application' block here, which we can discard. - // We would need a slight re-structure to record this correctly. - $application = GifApplicationExt::fromBin($in); + while ($blockId == GifData::GIF_EXTENSION && $extensionId != GifData::GIF_EXTENSION_PLAINTEXT) { + // We would need a slight re-structure to record these blocks correctly. + if ($extensionId == GifData::GIF_EXTENSION_APPLICATION) { + // ImageMagick drops an 'application' block here, which we can discard (gfx-php does not use it at this stage) + GifApplicationExt::fromBin($in); + } else if ($extensionId == GifData::GIF_EXTENSION_COMMENT) { + // Also GIMP places a 'comment' block here. + GifCommentExt::fromBin($in); + } else { + // And who knows what else we will have to skip. This should cover it anyway. + GifUnknownExt::fromBin($in); + } $peek = $in -> peek(2); $blockId = $peek[0]; $extensionId = $peek[1]; diff --git a/src/Mike42/GfxPhp/Codec/GifCodec.php b/src/Mike42/GfxPhp/Codec/GifCodec.php index 26bf912..a45d736 100644 --- a/src/Mike42/GfxPhp/Codec/GifCodec.php +++ b/src/Mike42/GfxPhp/Codec/GifCodec.php @@ -50,7 +50,7 @@ public function encode(RasterImage $image, string $format): string // Transparent color for graphic control $transparentColorFlag = 0x00; $transparentColor = 0; - if ($image -> getTransparentColor() !== null) { + if ($image -> getTransparentColor() >= 0) { $transparentColor = $image -> getTransparentColor() & 0xFF; $transparentColorFlag = 0x01; } diff --git a/src/Mike42/GfxPhp/Image.php b/src/Mike42/GfxPhp/Image.php index 9360810..7af248f 100644 --- a/src/Mike42/GfxPhp/Image.php +++ b/src/Mike42/GfxPhp/Image.php @@ -16,9 +16,13 @@ class Image public static function fromFile(string $filename) : RasterImage { - $blob = file_get_contents($filename); + // Attempt to catch the cause of any errors + error_clear_last(); + $blob = @file_get_contents($filename); if ($blob === false) { - throw new \Exception("Could not retrieve image data from '$filename'. Check that the file exists and can be read."); + $e = error_get_last(); + $error = (isset($e) && isset($e['message']) && $e['message'] != "") ? $e['message'] : "Check that the file exists and can be read."; + throw new \Exception("Could not retrieve image data from '$filename'. $error"); } return self::fromBlob($blob, $filename); } diff --git a/src/Mike42/GfxPhp/IndexedRasterImage.php b/src/Mike42/GfxPhp/IndexedRasterImage.php index 43508e2..99a4365 100644 --- a/src/Mike42/GfxPhp/IndexedRasterImage.php +++ b/src/Mike42/GfxPhp/IndexedRasterImage.php @@ -13,7 +13,7 @@ class IndexedRasterImage extends AbstractRasterImage protected $palette; - protected $transparentColor = null; + protected $transparentColor = -1; protected function __construct($width, $height, array $data, int $maxVal, array $palette) { @@ -127,7 +127,7 @@ public function toIndexed(): IndexedRasterImage public function indexToRgb(int $index) { - if ($index == $this -> transparentColor) { + if ($index === $this -> transparentColor) { // White return [255, 255, 255]; } @@ -150,12 +150,12 @@ public function rgbToIndex(array $rgb) return 0; } - public function getTransparentColor() + public function getTransparentColor() : int { return $this -> transparentColor; } - public function setTransparentColor(int $color = null) + public function setTransparentColor(int $color) { $this -> transparentColor = $color; } diff --git a/test/integration/PngsuiteTest.php b/test/integration/PngsuiteTest.php index f9104dd..3200f4f 100644 --- a/test/integration/PngsuiteTest.php +++ b/test/integration/PngsuiteTest.php @@ -10,7 +10,7 @@ * Simple check that all files in pngsuite are correctly accepted or rejected, and contain * correct width. */ -class Pngsuite extends TestCase +class PngsuiteTest extends TestCase { function loadImage(string $filename) { return Image::fromFile(__DIR__ . "/../resources/pngsuite/$filename"); diff --git a/test/integration/SmallFileTest.php b/test/integration/SmallFileTest.php new file mode 100644 index 0000000..06554ca --- /dev/null +++ b/test/integration/SmallFileTest.php @@ -0,0 +1,77 @@ + toBlackAndWhite(); + } + + function testWhite() { + $expected = " \n"; + $width = 1; + $height = 1; + foreach(['canvas_white.gif', 'canvas_white.png'] as $fn) { // Also jpg and bmp + $img = $this -> loadImage($fn); + $this -> assertEquals($expected, $img -> toString()); + $this -> assertEquals($height, $img -> getHeight()); + $this -> assertEquals($width, $img -> getWidth()); + } + } + + function testBlack() { + $expected = "▀\n"; + $width = 1; + $height = 1; + foreach(['canvas_black.gif', 'canvas_black.png'] as $fn) { // Also jpg and bmp + $img = $this -> loadImage($fn); + $this -> assertEquals($expected, $img -> toString()); + $this -> assertEquals($height, $img -> getHeight()); + $this -> assertEquals($width, $img -> getWidth()); + } + } + + function testBlackWhite() { + $expected = "▀▀\n"; + $width = 2; + $height = 2; + foreach(['black_white.gif', 'black_white.png'] as $fn) { // Also jpg and bmp + $img = $this -> loadImage($fn); + $this -> assertEquals($expected, $img -> toString()); + $this -> assertEquals($height, $img -> getHeight()); + $this -> assertEquals($width, $img -> getWidth()); + } + } + + function testBlackTransparent() { + $expected = "▀▀\n"; + $width = 2; + $height = 2; + foreach(['black_transparent.gif', 'black_transparent.png'] as $fn) { // Also jpg and bmp + $img = $this -> loadImage($fn); + $this -> assertEquals($expected, $img -> toString()); + $this -> assertEquals($height, $img -> getHeight()); + $this -> assertEquals($width, $img -> getWidth()); + } + } + + function testBlackWhiteTall() { + $expected = "██\n" . + "██\n" . + "██\n" . + "██\n" . + " \n" . + " \n" . + " \n" . + " \n"; + $width = 2; + $height = 16; + $img = $this -> loadImage('black_white_tall.png'); + $this -> assertEquals($expected, $img -> toString()); + $this -> assertEquals($height, $img -> getHeight()); + $this -> assertEquals($width, $img -> getWidth()); + } +} \ No newline at end of file diff --git a/test/resources/small-files/README.md b/test/resources/small-files/README.md new file mode 100644 index 0000000..6031c14 --- /dev/null +++ b/test/resources/small-files/README.md @@ -0,0 +1,6 @@ +# Small files + +These test files were produded for `escpos-php`. + +The same file should have the same representation in black & white from any source format. + diff --git a/test/resources/small-files/black_transparent.gif b/test/resources/small-files/black_transparent.gif new file mode 100644 index 0000000..6c54bad Binary files /dev/null and b/test/resources/small-files/black_transparent.gif differ diff --git a/test/resources/small-files/black_transparent.png b/test/resources/small-files/black_transparent.png new file mode 100644 index 0000000..b43bbb8 Binary files /dev/null and b/test/resources/small-files/black_transparent.png differ diff --git a/test/resources/small-files/black_white.bmp b/test/resources/small-files/black_white.bmp new file mode 100644 index 0000000..0acbf66 Binary files /dev/null and b/test/resources/small-files/black_white.bmp differ diff --git a/test/resources/small-files/black_white.gif b/test/resources/small-files/black_white.gif new file mode 100644 index 0000000..0a044a6 Binary files /dev/null and b/test/resources/small-files/black_white.gif differ diff --git a/test/resources/small-files/black_white.jpg b/test/resources/small-files/black_white.jpg new file mode 100644 index 0000000..6539cec Binary files /dev/null and b/test/resources/small-files/black_white.jpg differ diff --git a/test/resources/small-files/black_white.png b/test/resources/small-files/black_white.png new file mode 100644 index 0000000..33ba331 Binary files /dev/null and b/test/resources/small-files/black_white.png differ diff --git a/test/resources/small-files/black_white_tall.png b/test/resources/small-files/black_white_tall.png new file mode 100644 index 0000000..7e0869d Binary files /dev/null and b/test/resources/small-files/black_white_tall.png differ diff --git a/test/resources/small-files/canvas_black.bmp b/test/resources/small-files/canvas_black.bmp new file mode 100644 index 0000000..2ee723c Binary files /dev/null and b/test/resources/small-files/canvas_black.bmp differ diff --git a/test/resources/small-files/canvas_black.gif b/test/resources/small-files/canvas_black.gif new file mode 100644 index 0000000..49b19db Binary files /dev/null and b/test/resources/small-files/canvas_black.gif differ diff --git a/test/resources/small-files/canvas_black.jpg b/test/resources/small-files/canvas_black.jpg new file mode 100644 index 0000000..d059f43 Binary files /dev/null and b/test/resources/small-files/canvas_black.jpg differ diff --git a/test/resources/small-files/canvas_black.png b/test/resources/small-files/canvas_black.png new file mode 100644 index 0000000..52e6ba9 Binary files /dev/null and b/test/resources/small-files/canvas_black.png differ diff --git a/test/resources/small-files/canvas_white.bmp b/test/resources/small-files/canvas_white.bmp new file mode 100644 index 0000000..e47e0ef Binary files /dev/null and b/test/resources/small-files/canvas_white.bmp differ diff --git a/test/resources/small-files/canvas_white.gif b/test/resources/small-files/canvas_white.gif new file mode 100644 index 0000000..7881ce6 Binary files /dev/null and b/test/resources/small-files/canvas_white.gif differ diff --git a/test/resources/small-files/canvas_white.jpg b/test/resources/small-files/canvas_white.jpg new file mode 100644 index 0000000..516a965 Binary files /dev/null and b/test/resources/small-files/canvas_white.jpg differ diff --git a/test/resources/small-files/canvas_white.png b/test/resources/small-files/canvas_white.png new file mode 100644 index 0000000..4231a4b Binary files /dev/null and b/test/resources/small-files/canvas_white.png differ diff --git a/test/unit/ImageTest.php b/test/unit/ImageTest.php new file mode 100644 index 0000000..7a9a75d --- /dev/null +++ b/test/unit/ImageTest.php @@ -0,0 +1,12 @@ + expectException(Exception::class); + Image::fromFile("not a real file"); + } +} \ No newline at end of file