8000 add image->hasAlpha() · libvips/php-vips@ed93d8c · GitHub
[go: up one dir, main page]

Skip to content

Commit ed93d8c

Browse files
committed
add image->hasAlpha()
plus some tests see #36
1 parent 7b4f940 commit ed93d8c

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to `:vips` will be documented in this file.
66
### Added
77
- fix minor formatting issues reported by phpcs [John Cupitt]
88
- regenerated autodocs from libvips 8.4 to reduce confusion [John Cupitt]
9+
- add Image::hasAlpha tester
910

1011
### Deprecated
1112
- Nothing

src/Image.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,11 @@ public function remove(string $name)
861861
public function __toString()
862862
{
863863
$array = [
864-
'width' => $this->get('width'),
865-
'height' => $this->get('height'),
866-
'bands' => $this->get('bands'),
867-
'format' => $this->get('format'),
868-
'interpretation' => $this->get('interpretation'),
864+
'width' => $this->width,
865+
'height' => $this->height,
866+
'bands' => $this->bands,
867+
'format' => $this->format,
868+
'interpretation' => $this->interpretation,
869869
];
870870

871871
return json_encode($array);
@@ -962,7 +962,6 @@ private function callEnum(
962962
string $op,
963963
array $options = []
964964
) {
965-
966965
if (self::isImageish($other)) {
967966
return self::call($base, $this, [$other, $op], $options);
968967
} else {
@@ -996,6 +995,24 @@ public static function __callStatic(string $name, array $arguments)
996995
return self::callBase($name, null, $arguments);
997996
}
998997

998+
/**
999+
* Does this image have an alpha channel?
1000+
*
1001+
* Uses colour space interpretation with number of channels to guess
1002+
* this.
1003+
*
1004+
* @param Image $image The source image.
1005+
*
1006+
* @return bool indicating if this image has an alpha channel.
1007+
*/
1008+
public function hasAlpha(): bool
1009+
{
1010+
return $this->bands === 2 ||
1011+
($this->bands === 4 &&
1012+
$this->interpretation !== Interpretation::CMYK) ||
1013+
$this->bands > 4;
1014+
}
1015+
9991016
/**
10001017
* Our ArrayAccess interface ... we allow [] to get band.
10011018
*
Loading

tests/meta.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ protected function setUp()
88
{
99
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
1010
$this->image = Vips\Image::newFromFile($filename);
11+
12+
$png_filename = dirname(__FILE__) . "/images/PNG_transparency_demonstration_1.png";
13+
$this->png_image = Vips\Image::newFromFile($png_filename);
1114
}
1215

1316
public function testVipsSetGet()
@@ -61,6 +64,12 @@ public function testVipsEnumString()
6164
$this->assertEquals($x, "srgb");
6265
}
6366

67+
public function testVipsHasAlpha()
68+
{
69+
$this->assertEquals($this->image->hasAlpha(), FALSE);
70+
$this->assertEquals($this->png_image->hasAlpha(), TRUE);
71+
}
72+
6473
}
6574

6675
/*

0 commit comments

Comments
 (0)
0