<?php
namespace App\Twig;
use App\Entity\Post;
use Twig\Environment;
use Twig\TwigFunction;
use App\Entity\Category;
use App\Entity\Language;
use App\Entity\TypePost;
use App\Entity\CpPostField;
use App\Entity\PostCategory;
use App\Entity\ParametrePost;
use App\Entity\ParamPublicite;
use Symfony\Component\Intl\Locale;
use Symfony\Component\Intl\Languages;
use Twig\Extension\AbstractExtension;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;
use MobileDetectBundle\DeviceDetector\MobileDetectorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ImagesExtenstion extends AbstractExtension
{
protected $em;
private $theme;
private $twig;
private $mobileDetector;
private $tokenStorage;
public function __construct(EntityManagerInterface $em,
string $theme,Environment $twig,
MobileDetectorInterface $mobileDetector,
TokenStorageInterface $tokenStorage) {
$this->em = $em;
$this->theme = $theme;
$this->twig = $twig;
$this->mobileDetector = $mobileDetector;
$this->tokenStorage = $tokenStorage;
}
public function getFunctions()
{
return array(
new TwigFunction('asset_image', array($this, 'AssetImageFunction')),
new TwigFunction('asset_image_gallery', array($this, 'AssetImageGalleryFunction')),
new TwigFunction('asset_image_affichage', array($this, 'AssetImageAffichageFunction')),
);
}
public function AssetImageFunction($filePath,$size = 'medium',$array_path = []){
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$extension_file = pathinfo($filePath, PATHINFO_EXTENSION);
if (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Chrome') === false) {
$chemin_webp = $filePath;
}else{
$chemin_webp = str_replace('.' . $extension_file, '.webp', $filePath);
}
$cdn_path = '';
//$cdn_path = "https://assets.webspirit.ovh";
if(!(is_array($array_path) and count($array_path) >= 1)){
if ($this->mobileDetector->isMobile()) {
$file = str_replace("/files/",$cdn_path."/thumbs/files380/",$chemin_webp);
}elseif($this->mobileDetector->isTablet()){
$file = str_replace("/files/",$cdn_path."/thumbs/files800/",$chemin_webp);
}else{
if ($size == 'large') {
$file = str_replace("/files/",$cdn_path."/thumbs/files1920/",$chemin_webp);
}elseif($size == 'medium'){
$file = str_replace("/files/",$cdn_path."/thumbs/files1024/",$chemin_webp);
}elseif($size == 'medium2'){
$file = str_replace("/files/",$cdn_path."/thumbs/files480/",$chemin_webp);
}
}
}else{
$pc_path = "files1920";
$tablet_path = "files800";
$mobile_path = "files480";
if (is_array($array_path)) {
if (count($array_path) >= 3) {
$pc_path = $array_path[0];
$tablet_path = $array_path[1];
$mobile_path = $array_path[2];
}elseif (count($array_path) == 2) {
$pc_path = $array_path[0];
$tablet_path = $array_path[1];
$mobile_path = $array_path[1];
}elseif (count($array_path) == 1) {
$pc_path = $array_path[0];
$tablet_path = $array_path[0];
$mobile_path = $array_path[0];
}
}
if ($this->mobileDetector->isMobile()) {
$file = str_replace("/files/",$cdn_path."/thumbs/" . $mobile_path ."/",$chemin_webp);
}elseif($this->mobileDetector->isTablet()){
$file = str_replace("/files/",$cdn_path."/thumbs/" . $tablet_path ."/",$chemin_webp);
}else{
$file = str_replace("/files/",$cdn_path."/thumbs/" . $pc_path ."/",$chemin_webp);
}
}
return $file;
}
public function AssetImageGalleryFunction($filePath,$array_path = []){
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$extension_file = pathinfo($filePath, PATHINFO_EXTENSION);
if (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Chrome') === false) {
$chemin_webp = $filePath;
}else{
$chemin_webp = str_replace('.' . $extension_file, '.webp', $filePath);
}
$cdn_path = '';
//$cdn_path = "https://assets.webspirit.ovh";
if(!(is_array($array_path) and count($array_path) >= 1)){
if ($this->mobileDetector->isMobile()) {
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/files380/",$chemin_webp);
}elseif($this->mobileDetector->isTablet()){
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/files800/",$chemin_webp);
}else{
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/files1920/",$chemin_webp);
}
}else{
$pc_path = "files1920";
$tablet_path = "files800";
$mobile_path = "files480";
if (is_array($array_path)) {
if (count($array_path) >= 3) {
$pc_path = $array_path[0];
$tablet_path = $array_path[1];
$mobile_path = $array_path[2];
}elseif (count($array_path) == 2) {
$pc_path = $array_path[0];
$tablet_path = $array_path[1];
$mobile_path = $array_path[1];
}elseif (count($array_path) == 1) {
$pc_path = $array_path[0];
$tablet_path = $array_path[0];
$mobile_path = $array_path[0];
}
}
if ($this->mobileDetector->isMobile()) {
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/" . $mobile_path."/" ,$chemin_webp);
}elseif($this->mobileDetector->isTablet()){
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/" . $tablet_path."/" ,$chemin_webp);
}else{
$file = str_replace("storage/galleries/",$cdn_path."/thumbs/" . $pc_path."/" ,$chemin_webp);
}
}
return $file;
}
public function AssetImageAffichageFunction($filePath,$type_affichage = 'affichage3'){
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$extension_file = pathinfo($filePath, PATHINFO_EXTENSION);
if (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Chrome') === false) {
$chemin_webp = $filePath;
}else{
$chemin_webp = str_replace('.' . $extension_file, '.webp', $filePath);
}
$array_path = ['files1024','files800','files480'];
switch ($type_affichage) {
case 'affichage1':
$array_path = ['files800','files800','files480'];
break;
case 'affichage2':
$array_path = ['files1024','files1024','files800'];
break;
case 'affichage3':
$array_path = ['files800','files800','files480'];
break;
case 'affichage4':
$array_path = ['files1024','files1024','files480'];
break;
case 'affichage5':
$array_path = ['files1920','files800','files480'];
break;
case 'affichage6':
$array_path = ['files1024','files1024','files480'];
break;
case 'affichage7':
$array_path = ['files800','files800','files480'];
break;
case 'affichage8':
$array_path = ['files800','files800','files480'];
break;
case 'affichage9':
$array_path = ['files800','files800','files480'];
break;
case 'affichage10':
$array_path = ['files1920','files1024','files480'];
break;
case 'affichage11':
$array_path = ['files800','files800','files480'];
break;
default:
$array_path = ['files1920','files800','files480'];
break;
}
$cdn_path = '';
//$cdn_path = "https://assets.webspirit.ovh";
if (is_array($array_path)) {
$pc_path = $array_path[0];
$tablet_path = $array_path[1];
$mobile_path = $array_path[2];
}
if ($this->mobileDetector->isMobile()) {
$file = str_replace("/files/",$cdn_path."/thumbs/" . $mobile_path ."/",$chemin_webp);
}elseif($this->mobileDetector->isTablet()){
$file = str_replace("/files/",$cdn_path."/thumbs/" . $tablet_path ."/",$chemin_webp);
}else{
$file = str_replace("/files/",$cdn_path."/thumbs/" . $pc_path ."/",$chemin_webp);
}
return $file;
}
public function getUser()
{
return $this->tokenStorage->getToken()->getUser();
}
}