From f707c72f075f21ffcd7b4ea336e961d24f73d5e3 Mon Sep 17 00:00:00 2001 From: Celestora Date: Tue, 5 Apr 2022 11:38:19 +0300 Subject: [PATCH] Cache image resolution in database --- Web/Models/Entities/Photo.php | 22 +++++++++++++--------- install/sqls/00020-image-sizes.sql | 3 +++ 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 install/sqls/00020-image-sizes.sql diff --git a/Web/Models/Entities/Photo.php b/Web/Models/Entities/Photo.php index ed8dc7ba..8862f8a1 100644 --- a/Web/Models/Entities/Photo.php +++ b/Web/Models/Entities/Photo.php @@ -48,18 +48,22 @@ class Photo extends Media function getDimensions(): array { - $hash = $this->getRecord()->hash; + $x = $this->getRecord()->width; + $y = $this->getRecord()->height; + if(!$x) { # no sizes in database + $hash = $this->getRecord()->hash; + $image = new \Imagick($this->pathFromHash($hash)); - return array_slice(getimagesize($this->pathFromHash($hash)), 0, 2); + $x = $image->getImageWidth(); + $y = $image->getImageHeight(); + $this->stateChanges("width", $x); + $this->stateChanges("height", $y); + $this->save(); + } + + return [$x, $y]; } - function getDimentions(): array - { - trigger_error("getDimentions is deprecated, use Photo::getDimensions instead."); - - return $this->getDimensions(); - } - function getAlbum(): ?Album { return (new Albums)->getAlbumByPhotoId($this); diff --git a/install/sqls/00020-image-sizes.sql b/install/sqls/00020-image-sizes.sql new file mode 100644 index 00000000..e2838f05 --- /dev/null +++ b/install/sqls/00020-image-sizes.sql @@ -0,0 +1,3 @@ +ALTER TABLE `photos` ROW_FORMAT=COMPRESSED; +ALTER TABLE `photos` ADD COLUMN `sizes` VARBINARY(256) NULL DEFAULT NULL AFTER `hash`; +ALTER TABLE `photos` ADD COLUMN `width` SMALLINT UNSIGNED NULL DEFAULT NULL AFTER `sizes`, ADD COLUMN `height` SMALLINT UNSIGNED NULL DEFAULT NULL AFTER `sizes`;