From 7f132638d09ba233469f01ac184f9e8fb1eb6f99 Mon Sep 17 00:00:00 2001 From: horst-n Date: Wed, 24 Apr 2019 23:23:05 +0200 Subject: [PATCH] added properties for webp support with the property $image->hasWebp we can detect, for example in a template file, if an image variation has a dependant webp file, so we can render conditional markup with srcset or picture elements. And urlWebp and srcWebp for returning the URL. --- wire/core/Pageimage.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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); }