From 6f4bbf1fe98334173ac022e09dcb5ffb9fcb51bd Mon Sep 17 00:00:00 2001 From: Banana Date: Tue, 28 Nov 2023 13:20:18 +0100 Subject: [PATCH] fixed #24 Image upload. Automatic resize?. Special case for coverimage and theme dependend --- CHANGELOG | 4 +- upgrade/from-version-1.5.txt | 12 ++++- webclient/config/config.php.default | 7 +++ webclient/lib/manageentry.class.php | 83 +++++++++++++++++++++++++---- webclient/lib/summoner.class.php | 18 +++++++ 5 files changed, 112 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bd5575e..a72a05c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ 1.x - Chizra - * Config change. Added new entry. See upgrade/from-version-1.5.txt. It won't work if it is missing. + * Config change: Added new entry. See upgrade/from-version-1.5.txt. It won't work if it is missing. + * Config change: Added new theme config. See upgrade/from-version-1.5.txt. It won't work if it is missing. * Licence change to GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 * Cleanups * Fixed: #21 Debug? string in error log @@ -8,6 +9,7 @@ * Fixed: #25 98 design. Sort direction select does not show the correct selected option * Fixed: #23 basic search result order * Fixed: Bulkedit in 98 theme and default + * Fixed: #24 Image upload. Automatic resize? 1.5 - Sacred Passage 2023-05-01 * Added google books parser. See upgrade file for more infos. diff --git a/upgrade/from-version-1.5.txt b/upgrade/from-version-1.5.txt index 2a1fe5d..97d2344 100644 --- a/upgrade/from-version-1.5.txt +++ b/upgrade/from-version-1.5.txt @@ -1,3 +1,13 @@ # Added new const to config.php file. Use config.php.default as a help. The new line is: -const LOGFILE = PATH_SYSTEMOUT.'/bibliotheca.log'; \ No newline at end of file +const LOGFILE = PATH_SYSTEMOUT.'/bibliotheca.log'; + +# Added new theme config to config.php file +Use config.php.default as a help. The new setting is: +# additional config for each theme with fallback +const UI_THEME_CONFIG = array( + 'default' => array( + 'coverImageMaxWidth' => 260 // in pixel. Supports image/jpeg, image/png, image/webp + ), + '98' => array() +); \ No newline at end of file diff --git a/webclient/config/config.php.default b/webclient/config/config.php.default index 17b878b..d8361d1 100644 --- a/webclient/config/config.php.default +++ b/webclient/config/config.php.default @@ -44,6 +44,13 @@ const DB_PREFIX = '~dbprefix~'; # a _ is added automatically as separation # available values are: default, 98 # fallback is default theme const UI_THEME = 'default'; +# additional config for each theme with fallback +const UI_THEME_CONFIG = array( + 'default' => array( + 'coverImageMaxWidth' => 260 // in pixel. Supports image/jpeg, image/png, image/webp + ), + '98' => array() +); # session const SESSION_LIFETIME = 43200; // 8 hours diff --git a/webclient/lib/manageentry.class.php b/webclient/lib/manageentry.class.php index f1ea406..a09542b 100644 --- a/webclient/lib/manageentry.class.php +++ b/webclient/lib/manageentry.class.php @@ -100,9 +100,9 @@ class Manageentry { $query = $this->_DB->query($queryStr); if($query !== false && $query->num_rows > 0) { while(($result = $query->fetch_assoc()) != false) { - $_mn = '_loadField_'.$result['type']; - if(method_exists($this, $_mn)) { - $result = $this->$_mn($result); + $_methodName = '_loadField_'.$result['type']; + if(method_exists($this, $_methodName)) { + $result = $this->$_methodName($result); } $this->_cacheEditFields[$result['id']] = $result; } @@ -179,12 +179,18 @@ class Manageentry { $queryData['init'] = array(); $queryData['after'] = array(); foreach ($data as $i=>$d) { - $_mn = '_saveField_'.$d['type']; - if(method_exists($this, $_mn)) { - $queryData = $this->$_mn($d, $queryData); + $_methodName = '_saveField_'.$d['type']; + $_methodNameSpecial = $_methodName.'__'.$d['identifier']; + if(DEBUG) Summoner::sysLog("[DEBUG] ".__METHOD__." methodname: ".Summoner::cleanForLog($_methodName)); + if(DEBUG) Summoner::sysLog("[DEBUG] ".__METHOD__." methodnamespecial: ".Summoner::cleanForLog($_methodNameSpecial)); + if(method_exists($this, $_methodNameSpecial)) { + $queryData = $this->$_methodNameSpecial($d, $queryData); + } + elseif(method_exists($this, $_methodName)) { + $queryData = $this->$_methodName($d, $queryData); } else { - if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." Missing query function for: ".Summoner::cleanForLog($d)); + if(DEBUG) Summoner::sysLog("[DEBUG] ".__METHOD__." Missing query function for: ".Summoner::cleanForLog($d)); } } @@ -712,13 +718,70 @@ class Manageentry { return $queryData; } + /** + * Special for single upload and subtype coverimage. + * Uses the theme settings for image resize. Modifies the result from _saveField_upload if it is an image + * + * @param array $data + * @param array $queryData + * @return array + */ + private function _saveField_upload__coverimage(array $data, array $queryData): array { + $queryData = $this->_saveField_upload($data, $queryData); + + $workWith = $queryData['after']['upload'][0]['tmp_name']; + if(file_exists($workWith)) { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mime = finfo_file($finfo, $workWith); + finfo_close($finfo); + if(str_contains('image/jpeg, image/png, image/webp', $mime)) { + list($width, $height) = getimagesize($workWith); + $_maxThemeWidth = Summoner::themeConfig('coverImageMaxWidth', UI_THEME); + if(!empty($_maxThemeWidth) && ($width > $_maxThemeWidth)) { + $_ratio = $_maxThemeWidth/$width; + $newWidth = (int) $_maxThemeWidth; + $newHeight = (int) $height * $_ratio; + if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." image ratio: ".$_ratio); + if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." image width: ".$width); + if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." image height: ".$height); + if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." image new width: ".$newWidth); + if(DEBUG)Summoner::sysLog("[DEBUG] ".__METHOD__." image new height: ".$newHeight); + $_tmp_image = imagecreatetruecolor($newWidth, $newHeight); + switch($mime) { + case 'image/jpeg': + $src = imagecreatefromjpeg($workWith); + imagecopyresampled($_tmp_image, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); + imagejpeg($_tmp_image, $workWith, 100); + break; + + case 'image/png': + $src = imagecreatefrompng($workWith); + imagecopyresampled($_tmp_image, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); + imagepng($_tmp_image, $workWith, 0); + break; + + case 'image/webp': + $src = imagecreatefromwebp($workWith); + imagecopyresampled($_tmp_image, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); + imagewebp($_tmp_image, $workWith,100); + break; + } + imagedestroy($_tmp_image); + imagedestroy($src); + } + } + } + + return $queryData; + } + /** - * runs the query and throws query execption if false + * runs the query and throws query exception if false * * @param string $queryString * @param string $insertId Number */ - private function _runAfter_query(string $queryString, string $insertId) { + private function _runAfter_query(string $queryString, string $insertId): void { if(!empty($queryString) && !empty($insertId)) { // replace only once to avoid replacing actual data $queryStr = Summoner::replaceOnce($queryString,$this->_replaceEntryString, $insertId); @@ -743,7 +806,7 @@ class Manageentry { * @param string $insertId Number * @throws Exception */ - private function _runAfter_upload(array $uploadData, string $insertId) { + private function _runAfter_upload(array $uploadData, string $insertId): void { if(!empty($uploadData) && !empty($insertId)) { if(DEBUG) Summoner::sysLog("[DEBUG] ".__METHOD__." uploadata: ".Summoner::cleanForLog($uploadData)); $_path = PATH_STORAGE.'/'.$this->_collectionId.'/'.$insertId; diff --git a/webclient/lib/summoner.class.php b/webclient/lib/summoner.class.php index 72d92ba..70b3a80 100644 --- a/webclient/lib/summoner.class.php +++ b/webclient/lib/summoner.class.php @@ -44,6 +44,24 @@ class Summoner { return $ret; } + /** + * Return the current config for a theme based on UI_THEME + * + * @param string $configProperty The property to fetch + * @param String $theme Theme name + * @param string $defaultTheme Default theme name can be overwritten + * @return string + */ + static function themeConfig(string $configProperty, string $theme, string $defaultTheme = 'default'): string { + $ret = ''; + + if(defined('UI_THEME_CONFIG')) { + $ret = UI_THEME_CONFIG[$theme][$configProperty] ?? UI_THEME_CONFIG[$defaultTheme][$configProperty]; + } + + return $ret; + } + /** * validate the given string with the given type. Optional check the string * length -- 2.39.5