HEX
Server: LiteSpeed
System: Linux d8 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: wbwebdes (3015)
PHP: 8.1.31
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/wbwebdes/domains/helpdesk.wb-webdesign.com/private_html/src/Service/UrlImageCacheService.php
<?php

namespace App\Service;

use App\Controller\ImageCache\ImageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UrlImageCacheService
{
    protected $cacheDir;
    protected $container;
    private $imageManager;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->cacheDir = $this->container->getParameter('kernel.project_dir') . '/public/cache/images';
        $this->imageManager = new ImageManager($this->container);
    }

    public function getCachedImage(string $url, string $domain): string
    {
        $cacheKey = md5($url);
        $cachePath = $this->cacheDir . '/' . $cacheKey . '.png';

        // Ensure the cache directory exists
        if (! is_dir($this->cacheDir)) {
            mkdir($this->cacheDir, 0775, true);
        }

        if ($this->isCacheExpired($cachePath)) {
            if (file_exists($cachePath)) {
                unlink($cachePath); // Delete the file
            }

            $this->cacheImage($url, $cachePath, $domain);
        }

        return $cachePath;
    }

    private function isCacheExpired(string $cachePath): bool
    {
        if (!file_exists($cachePath)) {
            return true;
        }

        $cacheLifetime = 7 * 24 * 60 * 60; // 1 week in seconds

        return (time() - filemtime($cachePath)) > $cacheLifetime;
    }

    private function cacheImage(string $url, string $cachePath, string $domain): void
    {
        $image = $this->imageManager->make([
            'imageUrl' => $url,
            'siteUrl'  => $domain
        ]);
        $image->save($cachePath);
    }
}