diff --git a/README.md b/README.md index 255ef40..fceade6 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ $image = $object->images[0]; echo "Image[0] URL: " . $image->url; // https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg echo "Image[0] height: " . $image->height; // null (May return height in pixels on other pages) echo "Image[0] width: " . $image->width; // null (May return width in pixels on other pages) +echo "Image[0] alt: " . $image->alt; // The alt text // Videos $video = $object->videos[0]; diff --git a/src/Elements/Image.php b/src/Elements/Image.php index 9087336..a319c62 100644 --- a/src/Elements/Image.php +++ b/src/Elements/Image.php @@ -46,6 +46,11 @@ class Image extends ElementBase */ public ?bool $userGenerated = null; + /** + * Alternative text for the image. + */ + public ?string $alt = null; + /** * @param string $url URL to the image file */ @@ -84,6 +89,10 @@ public function getProperties(): array $properties[] = new Property(Property::IMAGE_WIDTH, $this->width); } + if (null !== $this->alt) { + $properties[] = new Property(Property::IMAGE_ALT, $this->alt); + } + if (null !== $this->userGenerated) { $properties[] = new Property(Property::IMAGE_USER_GENERATED, $this->userGenerated); } diff --git a/src/Objects/ObjectBase.php b/src/Objects/ObjectBase.php index 40e3bf9..cd4e1da 100644 --- a/src/Objects/ObjectBase.php +++ b/src/Objects/ObjectBase.php @@ -148,6 +148,7 @@ public function assignProperties(array $properties, bool $debug = false): void case Property::IMAGE_SECURE_URL: case Property::IMAGE_TYPE: case Property::IMAGE_WIDTH: + case Property::IMAGE_ALT: case Property::IMAGE_USER_GENERATED: if (\count($this->images) > 0) { $this->handleImageAttribute($this->images[\count($this->images) - 1], $name, $value); @@ -223,6 +224,9 @@ private function handleImageAttribute(Image $element, string $name, string $valu case Property::IMAGE_WIDTH: $element->width = (int) $value; break; + case Property::IMAGE_ALT: + $element->alt = $value; + break; case Property::IMAGE_TYPE: $element->type = $value; break; diff --git a/src/Property.php b/src/Property.php index 00fe7be..1df9ba7 100644 --- a/src/Property.php +++ b/src/Property.php @@ -26,6 +26,7 @@ class Property public const IMAGE_TYPE = 'og:image:type'; public const IMAGE_URL = 'og:image:url'; public const IMAGE_WIDTH = 'og:image:width'; + public const IMAGE_ALT = 'og:image:alt'; public const IMAGE_USER_GENERATED = 'og:image:user_generated'; public const LOCALE = 'og:locale'; public const LOCALE_ALTERNATE = 'og:locale:alternate'; diff --git a/tests/ConsumerTest.php b/tests/ConsumerTest.php index 652d124..c82020d 100644 --- a/tests/ConsumerTest.php +++ b/tests/ConsumerTest.php @@ -194,6 +194,7 @@ public function testLoadHtmlImages(): void + @@ -211,6 +212,7 @@ public function testLoadHtmlImages(): void self::assertSame(300, $result->images[0]->width); self::assertSame(300, $result->images[0]->height); self::assertSame('image/jpg', $result->images[0]->type); + self::assertSame('Picture of a rock', $result->images[0]->alt); } public function testLoadHtmlVideos(): void