]> 91.132.146.200 Git - bibliotheca-php.git/commitdiff
fixed #24 Image upload. Automatic resize?.
authorBanana <mail@bananas-playground.net>
Tue, 28 Nov 2023 12:20:18 +0000 (13:20 +0100)
committerBanana <mail@bananas-playground.net>
Tue, 28 Nov 2023 12:20:18 +0000 (13:20 +0100)
Special case for coverimage and theme dependend

CHANGELOG
upgrade/from-version-1.5.txt
webclient/config/config.php.default
webclient/lib/manageentry.class.php
webclient/lib/summoner.class.php

index bd5575e873d707071dc93d2495a2e6f8e64001a0..a72a05c4fa67be2fb7493f94d84b51df963d0dce 100644 (file)
--- 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.
index 2a1fe5d6ad8b37cf9f807dccf0a7ce6b9740d6fc..97d2344edcbbfb1a6c8786d044168d4c01831d77 100644 (file)
@@ -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
index 17b878b8281faa291382fc715e1d2ecdd727c15c..d8361d1d75121c2cfd22942bd267a0ccee324866 100644 (file)
@@ -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
index f1ea40684721d0ddd88445d0b45f88026aa266d6..a09542b85c638c4e51532a365d8f3b9708854fbf 100644 (file)
@@ -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;
index 72d92ba42b74fd65a785481891102d3f10a94be1..70b3a800582b432e053d14d759dd8ba279ec791c 100644 (file)
@@ -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