diff --git a/lib/Cache.php b/lib/Cache.php index 9a87b54..950a12a 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -114,9 +114,10 @@ class Cache { $cacheFile = PUBLIC_CACHE_DIR . "/" . $name; $blurhash = self::generateBlurHash($cacheFile, $img, $skipExtensionCheck); + $blurhashImage = self::getBlurHashImage($blurhash); - $resourceSql = "INSERT OR REPLACE INTO `resources` (`name`, `type`, `blurhash`, `url`, `width`, `height`, `hasBeenProcessed`, `modified`) - VALUES (:name,:type,:blurhash,:url,:width,:height,1,strftime('%Y-%m-%d %H:%M:%S','now'))"; + $resourceSql = "INSERT OR REPLACE INTO `resources` (`name`, `type`, `blurhash`, `url`, `width`, `height`, `hasBeenProcessed`, `modified`, `hasEncodedVersion`, `encodedVersion`) + VALUES (:name,:type,:blurhash,:url,:width,:height,1,strftime('%Y-%m-%d %H:%M:%S','now'),1,:blurhashImage)"; $db = new Database(); $resourceStmt = $db::$handle->prepare($resourceSql); @@ -126,6 +127,7 @@ class Cache { $resourceStmt->bindValue(":url", PUBLIC_CACHE_URL . "/" . $name); $resourceStmt->bindValue(":width", $img->getImageWidth()); $resourceStmt->bindValue(":height", $img->getImageHeight()); + $resourceStmt->bindValue(":blurhashImage", $blurhashImage); $resourceStmt->execute(); // Append to the resource cache @@ -163,39 +165,29 @@ class Cache { } 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") { + $pixels = []; - $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; + 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; } public static function getBlurHashImage($blurhash,$width = 269, $height = 173) { diff --git a/schema/resources-001.sql b/schema/resources-001.sql new file mode 100644 index 0000000..d1c317a --- /dev/null +++ b/schema/resources-001.sql @@ -0,0 +1,5 @@ +ALTER TABLE `resources` ADD COLUMN `hasEncodedVersion` INTEGER DEFAULT 0; +ALTER TABLE `resources` ADD COLUMN `encodedVersion` TEXT DEFAULT NULL; + +CREATE INDEX IF NOT EXISTS `resources_hasEncodedVersion` ON `resources` (`hasEncodedVersion`); +CREATE INDEX IF NOT EXISTS `resources_blurhash_hasEncodedVersion` ON `resources` (`blurhash`, `hasEncodedVersion`);