// ======================================== // ULTRA FAST CACHE + IMAGE OPTIMIZER // ======================================== $cacheDir = __DIR__ . '/cache/service/'; $imageCacheDir = __DIR__ . '/cache/images/'; if (!is_dir($cacheDir)) { mkdir($cacheDir, 0777, true); } if (!is_dir($imageCacheDir)) { mkdir($imageCacheDir, 0777, true); } $slugCache = $_GET['slug'] ?? 'default'; $cacheFile = $cacheDir . md5($slugCache) . '.html'; // 7 дни кеш $cacheLife = 604800; // ако има кеш if ( file_exists($cacheFile) && ( time() - filemtime($cacheFile) ) < $cacheLife ) { readfile($cacheFile); exit; } // старт на HTML cache ob_start(); // ======================================== // IMAGE OPTIMIZER // ======================================== function optimizeImage($imagePath) { if (!$imagePath) { return ''; } $root = __DIR__; $fullPath = $root . '/' . ltrim($imagePath, '/'); if (!file_exists($fullPath)) { return $imagePath; } $cacheDir = $root . '/cache/images/'; $ext = strtolower( pathinfo( $fullPath, PATHINFO_EXTENSION ) ); $hash = md5( $fullPath . filemtime($fullPath) ); $webpFile = $cacheDir . $hash . '.webp'; $webpUrl = '/cache/images/' . $hash . '.webp'; // вече кеширана if ( file_exists( $webpFile ) ) { return $webpUrl; } switch ($ext) { case 'jpg': case 'jpeg': $img = imagecreatefromjpeg( $fullPath ); break; case 'png': $img = imagecreatefrompng( $fullPath ); break; case 'webp': return $imagePath; default: return $imagePath; } $width = imagesx($img); $height = imagesy($img); // resize huge images $maxWidth = 1200; if ( $width > $maxWidth ) { $newHeight = intval( ( $height / $width ) * $maxWidth ); $tmp = imagecreatetruecolor( $maxWidth, $newHeight ); imagecopyresampled( $tmp, $img, 0, 0, 0, 0, $maxWidth, $newHeight, $width, $height ); imagedestroy($img); $img = $tmp; } imagewebp( $img, $webpFile, 80 ); imagedestroy($img); return $webpUrl; } Slug не е подаден!