Skip to content

Commit 386d21b

Browse files
committed
Add Image::isType method
1 parent f0e401d commit 386d21b

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/Image.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ public function setInstance(GdImage $instance) : static
128128
public function getQuality() : ?int
129129
{
130130
if ($this->quality === null) {
131-
if ($this->getType() === \IMAGETYPE_PNG) {
131+
if ($this->isType(\IMAGETYPE_PNG)) {
132132
$this->quality = 6;
133-
} elseif ($this->getType() === \IMAGETYPE_JPEG) {
133+
} elseif ($this->isType(\IMAGETYPE_JPEG)) {
134134
$this->quality = 75;
135-
} elseif ($this->getType() === \IMAGETYPE_AVIF) {
135+
} elseif ($this->isType(\IMAGETYPE_AVIF)) {
136136
$this->quality = 52;
137137
}
138138
}
@@ -154,22 +154,22 @@ public function getQuality() : ?int
154154
*/
155155
public function setQuality(int $quality) : static
156156
{
157-
if ($this->getType() === \IMAGETYPE_GIF) {
157+
if ($this->isType(\IMAGETYPE_GIF)) {
158158
throw new LogicException(
159159
'GIF images does not receive a quality value'
160160
);
161161
}
162-
if ($this->getType() === \IMAGETYPE_PNG && ($quality < 0 || $quality > 9)) {
162+
if ($this->isType(\IMAGETYPE_PNG) && ($quality < 0 || $quality > 9)) {
163163
throw new InvalidArgumentException(
164164
'PNG images must receive a quality value between 0 and 9, ' . $quality . ' given'
165165
);
166166
}
167-
if ($this->getType() === \IMAGETYPE_JPEG && ($quality < 0 || $quality > 100)) {
167+
if ($this->isType(\IMAGETYPE_JPEG) && ($quality < 0 || $quality > 100)) {
168168
throw new InvalidArgumentException(
169169
'JPEG images must receive a quality value between 0 and 100, ' . $quality . ' given'
170170
);
171171
}
172-
if ($this->getType() === \IMAGETYPE_AVIF && ($quality < 0 || $quality > 100)) {
172+
if ($this->isType(\IMAGETYPE_AVIF) && ($quality < 0 || $quality > 100)) {
173173
throw new InvalidArgumentException(
174174
'AVIF images must receive a quality value between 0 and 100, ' . $quality . ' given'
175175
);
@@ -286,6 +286,11 @@ protected function setType(int $type) : static
286286
return $this;
287287
}
288288

289+
public function isType(int $type) : bool
290+
{
291+
return $this->getType() === $type;
292+
}
293+
289294
/**
290295
* Creates a new Image based in the current instance.
291296
*

tests/ImageTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,17 @@ public function testCreate() : void
316316
{
317317
$filename = \sys_get_temp_dir() . '/aplus-image-test';
318318
$this->image->create('png', $filename);
319+
$img = new Image($filename);
320+
self::assertTrue($img->isType(\IMAGETYPE_PNG));
319321
$this->image->create('jpeg', $filename);
322+
$img = new Image($filename);
323+
self::assertTrue($img->isType(\IMAGETYPE_JPEG));
320324
$this->image->create('gif', $filename);
325+
$img = new Image($filename);
326+
self::assertTrue($img->isType(\IMAGETYPE_GIF));
321327
$this->image->create('avif', $filename);
328+
$img = new Image($filename);
329+
self::assertTrue($img->isType(\IMAGETYPE_AVIF));
322330
\unlink($filename);
323331
$this->expectException(RuntimeException::class);
324332
$this->expectExceptionMessage('Image could not be created');

0 commit comments

Comments
 (0)