mirror of
https://github.com/processwire/processwire.git
synced 2025-08-17 20:11:46 +02:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user