Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 9 additions & 0 deletions src/Elements/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Objects/ObjectBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions tests/ConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function testLoadHtmlImages(): void
<meta property="og:image:width" content="300">
<meta property="og:image:height" content="300">
<meta property="og:image:type" content="image/jpg">
<meta property="og:image:alt" content="Picture of a rock">
</head>
<body></body>
</html>
Expand All @@ -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
Expand Down
Loading