pohja/lib/Cache.php

120 lines
4.0 KiB
PHP

<?php
class Cache {
public static function getCacheFile(string $name, $maxAge = 3600) {
$cacheFile = CACHE_DIR . "/" . $name;
if (file_exists($cacheFile) && filemtime($cacheFile) > time() - $maxAge) {
Logger::log("Cache hit for " . $name, Logger::IOREAD, "Cache");
return COMPRESS_LOCAL_CACHE ? gzuncompress(file_get_contents($cacheFile)) : file_get_contents($cacheFile);
}
return null;
}
public static function saveCacheFile(string $name, string $data) {
$cacheFile = CACHE_DIR . "/" . $name;
file_put_contents($cacheFile, COMPRESS_LOCAL_CACHE ? gzcompress($data,9) : $data );
Logger::log("Saved cache file " . $name, Logger::IOWRITE, "Cache");
}
public static function publicCacheExists(string $name, bool $skipExtensionCheck = false) {
// For now cache is not reliable since we use a generated name.
return false;
/*if (!$skipExtensionCheck) {
$ext = substr($name, strrpos($name, ".") + 1);
if (strtolower($ext) == "jpg" || strtolower($ext) == "jpeg" || strtolower($ext) == "png") {
$name = str_replace("." . $ext, ".webp", strtolower($name));
}
}
$cacheFile = PUBLIC_CACHE_DIR . "/" . $name;
try {
$fe = file_exists($cacheFile);
} catch (Exception $e) {
$fe = false;
Logger::log("Could not check if public cache file " . $name . " exists: " . $e->getMessage(), Logger::ERROR, "Cache");
}
return $fe;*/
}
public static function savePublicCacheFile(string $name, string $data, int $cacheLevel = 70, bool $skipExtensionCheck = false) {
$name = strtolower($name);
$ext = substr($name, strrpos($name, ".") + 1);
if ($skipExtensionCheck || strtolower($ext) == "jpg" || strtolower($ext) == "jpeg" || strtolower($ext) == "png") {
$size = strlen($data);
$img = new Imagick();
$img->readImageBlob($data);
$img->setImageFormat('webp');
$img->setImageCompressionQuality($cacheLevel);
$img->stripImage();
$data = $img->getImageBlob();
$name = str_replace("." . $ext, ".webp", strtolower($name));
$cacheFile = PUBLIC_CACHE_DIR . "/" . $name;
$blurhash = self::generateBlurHash($cacheFile, $img, $skipExtensionCheck);
if (!empty($blurhash)) {
$name = "BH-" . bin2hex($blurhash) . "-W" . $img->getImageWidth() . "xH" . $img->getImageHeight() . ".webp";
$cacheFile = PUBLIC_CACHE_DIR . "/" . $name;
}
// For now don't transfer file if its generated name exists
if (file_exists($cacheFile)) {
Logger::log("Public cache file " . $name . " already exists", Logger::INFO, "Cache");
return PUBLIC_CACHE_URL . "/" . $name;
}
Logger::log("Compressed image from " . $size . " to " . strlen($data) . " bytes", Logger::METRICS, "Cache");
}
$context = stream_context_create([
's3' => [
'ACL' => 'public-read'
]
]);
file_put_contents($cacheFile, $data, 0, $context);
Logger::log("Saved public cache file " . $name, Logger::IOWRITE, "Cache");
return PUBLIC_CACHE_URL . "/" . $name;
}
public static function generateBlurHash(string $cacheFile, ?IMagick $img = null, bool $skipExtensionCheck = false):?string {
$ext = substr($cacheFile, strrpos($cacheFile, ".") + 1);
if ($skipExtensionCheck || strtolower($ext) == "jpg" || strtolower($ext) == "jpeg" || strtolower($ext) == "png" || strtolower($ext) == "webp") {
$hashFile = strtolower($cacheFile);
$hashFileName = substr($hashFile, strrpos($hashFile, "/") + 1);
if (file_exists($hashFile)) {
return null;
}
$pixels = [];
if ($img === null) {
$img = new Imagick();
$img->readImage($cacheFile);
}
$width = $img->getImageWidth();
$height = $img->getImageHeight();
for ($y = 0; $y < $height; $y++) {
$row = [];
for ($x = 0; $x < $width; $x++) {
$pixel = $img->getImagePixelColor($x, $y);
$color = $pixel->getColor();
$row[] = [$color["r"], $color["g"], $color["b"]];
}
$pixels[] = $row;
}
$hash = \kornrunner\Blurhash\Blurhash::encode($pixels, BLURHASH_X, BLURHASH_Y);
return $hash;
}
}
}