Skip to content

Commit

Permalink
Add the possibility to add images to sitemaps (#451)
Browse files Browse the repository at this point in the history
* Add the possibility to add images to sitemaps. See also https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps

* And an example how to add images to urls
  • Loading branch information
madman-81 authored Jun 13, 2022
1 parent 55f33c9 commit 0da6e85
Show file tree
Hide file tree
Showing 25 changed files with 164 additions and 19 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ SitemapGenerator::create('https://example.com')

Note the ```addAlternate``` function which takes an alternate URL and the locale it belongs to.

#### Adding images to links

Urls can also have images. See also https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps

```php
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;

Sitemap::create()
// here we add an image to a URL
->add(Url::create('https://example.com')->addImage('https://example.com/images/home.jpg', 'Home page image'))
->writeToFile($sitemapPath);
```

### Manually creating a sitemap

You can also create a sitemap fully manual:
Expand Down
17 changes: 17 additions & 0 deletions resources/views/image.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<image:image>
@if (! empty($image->url))
<image:loc>{{ url($image->url) }}</image:loc>
@endif
@if (! empty($image->caption))
<image:caption>{{ $image->caption }}</image:caption>
@endif
@if (! empty($image->geo_location))
<image:geo_location>{{ $image->geo_location }}</image:geo_location>
@endif
@if (! empty($image->title))
<image:title>{{ url($image->title) }}</image:title>
@endif
@if (! empty($image->license))
<image:license>{{ url($image->license) }}</image:license>
@endif
</image:image>
2 changes: 1 addition & 1 deletion resources/views/sitemap.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?= '<'.'?'.'xml version="1.0" encoding="UTF-8"?>'."\n"; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
@foreach($tags as $tag)
@include('sitemap::' . $tag->getType())
@endforeach
Expand Down
1 change: 1 addition & 0 deletions resources/views/url.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
@if (! empty($tag->priority))
<priority>{{ number_format($tag->priority,1) }}</priority>
@endif
@each('sitemap::image', $tag->images, 'image')
</url>
69 changes: 69 additions & 0 deletions src/Tags/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Spatie\Sitemap\Tags;

class Image
{
public string $url;

public string $caption;

public string $geo_location;

public string $title;

public string $license;

public static function create(string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = ''): static
{
return new static($url, $caption, $geo_location, $title, $license);
}

public function __construct(string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = '')
{
$this->setUrl($url);

$this->setCaption($caption);

$this->setGeoLocation($geo_location);

$this->setTitle($title);

$this->setLicense($license);
}

public function setUrl(string $url = ''): static
{
$this->url = $url;

return $this;
}

public function setCaption(string $caption = ''): static
{
$this->caption = $caption;

return $this;
}

public function setGeoLocation(string $geo_location = ''): static
{
$this->geo_location = $geo_location;

return $this;
}

public function setTitle(string $title = ''): static
{
$this->title = $title;

return $this;
}

public function setLicense(string $license = ''): static
{
$this->license = $license;

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Tags/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Url extends Tag
/** @var \Spatie\Sitemap\Tags\Alternate[] */
public array $alternates = [];

/** @var \Spatie\Sitemap\Tags\Image[] */
public array $images = [];

public static function create(string $url): static
{
return new static($url);
Expand Down Expand Up @@ -75,6 +78,13 @@ public function addAlternate(string $url, string $locale = ''): static
return $this;
}

public function addImage(string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = ''): static
{
$this->images[] = new Image($url, $caption, $geo_location, $title, $license);

return $this;
}

public function path(): string
{
return parse_url($this->url, PHP_URL_PATH) ?? '';
Expand Down
34 changes: 34 additions & 0 deletions tests/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\Sitemap\Test;

use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;

class ImageTest extends TestCase
{
public function testXmlHasImage()
{
$expected_xml = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>https://localhost</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
<image:image>
<image:loc>https://localhost/favicon.ico</image:loc>
<image:caption>Favicon</image:caption>
</image:image>
</url>
</urlset>';

$sitemap = Sitemap::create();
$url = Url::create('https://localhost')->addImage('https://localhost/favicon.ico', 'Favicon');
$sitemap->add($url);

$render_output = $sitemap->render();

$this->assertXmlStringEqualsXmlString($expected_xml, $render_output);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost:4020/</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost:4020/</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost:4020/</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost:4020/</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost:4020/</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/thuis"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
</urlset>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2015-12-31T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
</urlset>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
</urlset>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/home</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://localhost/blog/post-1</loc>
<lastmod>2016-01-01T00:00:00+00:00</lastmod>
Expand Down

0 comments on commit 0da6e85

Please sign in to comment.