Resources encoded version

This commit is contained in:
Martijn de Boer 2024-03-23 14:36:10 +01:00
parent fd40c9cbed
commit fe2bc24465
2 changed files with 30 additions and 33 deletions

View file

@ -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) {

5
schema/resources-001.sql Normal file
View file

@ -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`);