diff --git a/wire/core/Pageimage.php b/wire/core/Pageimage.php index 5d0883f6..4a0b64f2 100644 --- a/wire/core/Pageimage.php +++ b/wire/core/Pageimage.php @@ -175,6 +175,33 @@ class Pageimage extends Pagefile { } } + /** + * Return the web accessible URL to this image files webP dependency + * + * @return string + * + */ + public function urlWebp() { + if(!$this->hasWebp()) { + return '#'; // @Ryan: what should be returned for none existing webp variations here? + } + $path_parts = pathinfo($this->url); + $webpUrl = $path_parts['dirname'] . '/' . $path_parts['filename'] . '.webp'; + return $webpUrl; + } + + /** + * Return if this image file has a webP dependency file + * + * @return boolean + * + */ + public function hasWebp() { + $path_parts = pathinfo($this->filename()); + $webpFilename = $path_parts['dirname'] . '/' . $path_parts['filename'] . '.webp'; + return is_readable($webpFilename); + } + /** * Returns the full disk path to the image file * @@ -391,6 +418,13 @@ class Pageimage extends Pagefile { $value = parent::get('src'); if($value === null) $value = $this->url(); break; + case 'hasWebp': + $value = $this->hasWebp(); + break; + case 'urlWebp': + case 'srcWebp': + $value = $this->urlWebp(); + break; default: $value = parent::get($key); }