<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ImageRenderExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('responsive_src', [$this, 'generateSrc']),
new TwigFunction('responsive_background', [$this, 'generateBackground']),
new TwigFunction('get_assets', [$this, 'getFiles']),
];
}
public static function acceptWebp(): bool
{
return (bool)strstr($_SERVER['HTTP_ACCEPT'], 'image/webp');
}
public function generateSrc(string $imagePath)
{
[$fileName, $extension, $basePath] = $this->extractImageParameters($imagePath);
if (file_exists(".$basePath/$fileName/3x.$extension")) {
[$s, $m, $l] = $this->getFileSizes($imagePath);
$fileName = str_replace(' ', '%20', $fileName);
return "
src='$basePath/$fileName/3x.$extension'
srcset='$basePath/$fileName/1x.$extension ${s}w,$basePath/$fileName/2x.$extension ${m}w,$basePath/$fileName/3x.$extension ${l}w'
sizes='100vw'
";
}
return "src='$imagePath'";
}
private function getFileSizes($imagePath): array
{
[$fileName, $extension, $basePath] = $this->extractImageParameters($imagePath);
if(file_exists(".$imagePath")){
return [
getimagesize(".$basePath/$fileName/1x.$extension")[0],
getimagesize(".$basePath/$fileName/2x.$extension")[0],
getimagesize(".$basePath/$fileName/3x.$extension")[0]
];
}
else{
return [0,0,0];
}
}
public function generateBackground(string $imagePath,string $additionnalStyle = '',bool $addGradient = false)
{
[$fileName, $extension, $basePath] = $this->extractImageParameters($imagePath);
if (file_exists(".$basePath/$fileName/3x.$extension")) {
[$s, $m, $l] = $this->getFileSizes($imagePath);
$addGradient = $addGradient ? '' : '';
$fileName = str_replace(' ', '%20', $fileName);
return "data-custom-bg-image=true style='
background-image: $addGradient url(\"$basePath/$fileName/3x.$extension\");
background-image : $addGradient -webkit-image-set(
url(\"$basePath/$fileName/1x.$extension\") 1x,
url(\"$basePath/$fileName/2x.$extension\") 2x,
url(\"$basePath/$fileName/3x.$extension\") 3x);
background-image : $addGradient image-set(
url(\"$basePath/$fileName/1x.$extension\") 1x,
url(\"$basePath/$fileName/2x.$extension\") 2x,
url(\"$basePath/$fileName/3x.$extension\") 3x);
background-image : $addGradient image-set(
url(\"$basePath/$fileName/1x.$extension\") ${s}w,
url(\"$basePath/$fileName/2x.$extension\") ${m}w,
url(\"$basePath/$fileName/3x.$extension\") ${l}w);
$additionnalStyle'";
}
return "data-bg-image='$imagePath' style='$additionnalStyle'";
}
public function getFiles(string $imagePath)
{
[$fileName, $extension, $basePath] = $this->extractImageParameters($imagePath);
return [
'1x' => "$basePath/$fileName/1x.$extension",
'2x' => "$basePath/$fileName/2x.$extension",
'3x' => "$basePath/$fileName/3x.$extension",
];
}/**
* @param string $imagePath
* @return array
*/
protected function extractImageParameters(string $imagePath): array
{
// dump("nom de l'image : $imagePath");
$fileName = pathinfo($imagePath, PATHINFO_FILENAME);
$extension = self::acceptWebp() ? 'webp' : pathinfo($imagePath, PATHINFO_EXTENSION);
$basePath = pathinfo($imagePath, PATHINFO_DIRNAME);
$basePath = str_replace('/images', '/image_build/images', $basePath);
$basePath = str_replace('/websiteImage', '/image_build/websiteImage', $basePath);
return [$fileName, $extension, $basePath];
}
}