From e8fed693e9568e2e71d6a9de61309cd9b895b602 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 21 Mar 2019 22:09:01 -0400 Subject: Update bg image functions to implement an image thumbnail When setting the bg image for an object, create a copy of it (square-cropped) in dynmic/thumbs/ for display in the settings modal. This patch provides an additional function for retriving the thumbnail's URL as well. This thumbnail is desirable for the sake of the UI. If we cannot make a guarantee as to the aspect ratio of the preview images shown on the user tab of the settings modal, these widgets may appear wrong or go off the screen a bit on smaller devices. Signed-off-by: Malf Furious --- app/class/obj.class.php | 57 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 353d617..9d3ac9a 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -277,26 +277,63 @@ class obj extends table } /* - * Set the background image for this object, overwriting any - * existing image. $image should be the name of the file - * formctrl field. + * Get the URL to the background image thumbnail resource for + * this object. If no image is set, NULL is returned. + */ + public function getThumbImg() : ?string + { + if (!is_file("dynmic/thumbs/" . $this->guid)) + return NULL; + + return ar() . "/df.php?d=thumbs&f=" . $this->guid; + } + + /* + * Set the background image and its thumbnail for this object, + * overwriting any existing images. $image should be the name + * of the file formctrl field. */ public function setBgImg(string $image) : bool { - $path = "dynmic/bgs/" . $this->guid; - return saveIfFile($image, $path, self::BG_MAXSIZE, self::IMAGE_MIME); + /* bgs image */ + $bgs = "dynmic/bgs/" . $this->guid; + if (!saveIfFile($image, $bgs, self::BG_MAXSIZE, self::IMAGE_MIME)) + goto fail; + + /* thumbs image */ + $thumbs = "dynmic/thumbs/" . $this->guid; + if (!copy($bgs, $thumbs)) + goto fail; + + if (!imageSquareCrop($thumbs)) + goto fail; + + return true; + +fail: + $this->rmBgImg(); + return false; } /* - * Remove the background image for this object. This deletes - * the image on disk. + * Remove the background image and thumbnail for this object. + * This deletes the images on disk. */ public function rmBgImg() : bool { - if (!is_file("dynmic/bgs/" . $this->guid)) - return true; + $ret = true; + + /* bgs */ + $bgs = "dynmic/bgs/" . $this->guid; + if (is_file($bgs) && !unlink($bgs)) + $ret = false; - return unlink("dynmic/bgs/" . $this->guid); + /* thumbs */ + $thumbs = "dynmic/thumbs/" . $this->guid; + if (is_file($thumbs) && !unlink($thumbs)) + $ret = false; + + return $ret; } } -- cgit v1.2.3 From 098720c46e351ab41295ad451b8d70e7f794ee4d Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 21 Mar 2019 23:50:40 -0400 Subject: Don't remove bg image if it fails to set This behavior was causing issues in the UI. We normally "try" to set a new asset by default, in case one was provided by the user, and just soft-fail if one wasn't. This "soft-failing" is now resulting in the images being removed if the user just goes in to change his alias (for example). Signed-off-by: Malf Furious --- app/class/obj.class.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 9d3ac9a..bd546b2 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -298,21 +298,17 @@ class obj extends table /* bgs image */ $bgs = "dynmic/bgs/" . $this->guid; if (!saveIfFile($image, $bgs, self::BG_MAXSIZE, self::IMAGE_MIME)) - goto fail; + return false; /* thumbs image */ $thumbs = "dynmic/thumbs/" . $this->guid; if (!copy($bgs, $thumbs)) - goto fail; + return false; if (!imageSquareCrop($thumbs)) - goto fail; + return false; return true; - -fail: - $this->rmBgImg(); - return false; } /* -- cgit v1.2.3