Skip to content

Commit 199fc2f

Browse files
authored
feature(#41): Added support for the image alt text attribute
1 parent 2daa6dc commit 199fc2f

5 files changed

Lines changed: 17 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ $image = $object->images[0];
6565
echo "Image[0] URL: " . $image->url; // https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg
6666
echo "Image[0] height: " . $image->height; // null (May return height in pixels on other pages)
6767
echo "Image[0] width: " . $image->width; // null (May return width in pixels on other pages)
68+
echo "Image[0] alt: " . $image->alt; // The alt text
6869

6970
// Videos
7071
$video = $object->videos[0];

src/Elements/Image.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Image extends ElementBase
4646
*/
4747
public ?bool $userGenerated = null;
4848

49+
/**
50+
* Alternative text for the image.
51+
*/
52+
public ?string $alt = null;
53+
4954
/**
5055
* @param string $url URL to the image file
5156
*/
@@ -84,6 +89,10 @@ public function getProperties(): array
8489
$properties[] = new Property(Property::IMAGE_WIDTH, $this->width);
8590
}
8691

92+
if (null !== $this->alt) {
93+
$properties[] = new Property(Property::IMAGE_ALT, $this->alt);
94+
}
95+
8796
if (null !== $this->userGenerated) {
8897
$properties[] = new Property(Property::IMAGE_USER_GENERATED, $this->userGenerated);
8998
}

src/Objects/ObjectBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public function assignProperties(array $properties, bool $debug = false): void
148148
case Property::IMAGE_SECURE_URL:
149149
case Property::IMAGE_TYPE:
150150
case Property::IMAGE_WIDTH:
151+
case Property::IMAGE_ALT:
151152
case Property::IMAGE_USER_GENERATED:
152153
if (\count($this->images) > 0) {
153154
$this->handleImageAttribute($this->images[\count($this->images) - 1], $name, $value);
@@ -223,6 +224,9 @@ private function handleImageAttribute(Image $element, string $name, string $valu
223224
case Property::IMAGE_WIDTH:
224225
$element->width = (int) $value;
225226
break;
227+
case Property::IMAGE_ALT:
228+
$element->alt = $value;
229+
break;
226230
case Property::IMAGE_TYPE:
227231
$element->type = $value;
228232
break;

src/Property.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Property
2626
public const IMAGE_TYPE = 'og:image:type';
2727
public const IMAGE_URL = 'og:image:url';
2828
public const IMAGE_WIDTH = 'og:image:width';
29+
public const IMAGE_ALT = 'og:image:alt';
2930
public const IMAGE_USER_GENERATED = 'og:image:user_generated';
3031
public const LOCALE = 'og:locale';
3132
public const LOCALE_ALTERNATE = 'og:locale:alternate';

tests/ConsumerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public function testLoadHtmlImages(): void
194194
<meta property="og:image:width" content="300">
195195
<meta property="og:image:height" content="300">
196196
<meta property="og:image:type" content="image/jpg">
197+
<meta property="og:image:alt" content="Picture of a rock">
197198
</head>
198199
<body></body>
199200
</html>
@@ -211,6 +212,7 @@ public function testLoadHtmlImages(): void
211212
self::assertSame(300, $result->images[0]->width);
212213
self::assertSame(300, $result->images[0]->height);
213214
self::assertSame('image/jpg', $result->images[0]->type);
215+
self::assertSame('Picture of a rock', $result->images[0]->alt);
214216
}
215217

216218
public function testLoadHtmlVideos(): void

0 commit comments

Comments
 (0)