From ff43f65926f73f43ec6d99c47456b27d69a69444 Mon Sep 17 00:00:00 2001 From: wangyidong Date: Mon, 14 Mar 2016 14:03:27 +0800 Subject: [PATCH] * adjust var name and adjust compressImage function. --- module/file/config.php | 2 +- module/file/control.php | 8 +------- module/file/model.php | 40 +++++++++++++++------------------------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/module/file/config.php b/module/file/config.php index e05ead0dab..ec03161183 100644 --- a/module/file/config.php +++ b/module/file/config.php @@ -5,4 +5,4 @@ $config->file->mimes['csv'] = 'text/csv'; $config->file->mimes['default'] = 'application/octet-stream'; $config->file->imageExtensions = array('jpeg', 'jpg', 'gif', 'png'); -$config->file->imageFmt = array('.jpg', '.bmp', '.jpeg'); +$config->file->image2Compress = array('.jpg', '.bmp', '.jpeg'); diff --git a/module/file/control.php b/module/file/control.php index 3b8299f3ed..86b0db5381 100644 --- a/module/file/control.php +++ b/module/file/control.php @@ -52,13 +52,7 @@ class file extends control if(@move_uploaded_file($file['tmpname'], $this->file->savePath . $file['pathname'])) { /* Compress image for jpg and bmp. */ - $compressedImage = $this->file->compressImage($file['pathname']); - if($compressedImage) - { - $file['pathname'] = $compressedImage['pathname']; - $file['extension'] = $compressedImage['extension']; - $file['size'] = $compressedImage['size']; - } + $file = $this->file->compressImage($file); $file['addedBy'] = $this->app->user->account; $file['addedDate'] = helper::today(); diff --git a/module/file/model.php b/module/file/model.php index 9e1c046e8c..e5bcc93562 100644 --- a/module/file/model.php +++ b/module/file/model.php @@ -78,13 +78,7 @@ class fileModel extends model { if($file['size'] == 0) continue; move_uploaded_file($file['tmpname'], $this->savePath . $file['pathname']); - $compressedImage = $this->compressImage($file['pathname']); - if($compressedImage) - { - $file['pathname'] = $compressedImage['pathname']; - $file['extension'] = $compressedImage['extension']; - $file['size'] = $compressedImage['size']; - } + $file = $this->compressImage($file); $file['objectType'] = $objectType; $file['objectID'] = $objectID; @@ -246,9 +240,11 @@ class fileModel extends model $realPathName= $this->savePath . $pathName; if(!is_dir(dirname($realPathName)))mkdir(dirname($realPathName)); move_uploaded_file($file['tmpname'], $realPathName); - $compressImage = $this->compressImage($pathName); - if($compressImage) $file['size'] = $compressedImage['size']; + $file['pathname'] = $pathName; + $file = $this->compressImage($file); + + $fileInfo = new stdclass(); $fileInfo->addedBy = $this->app->user->account; $fileInfo->addedDate = helper::now(); $fileInfo->size = $file['size']; @@ -483,39 +479,33 @@ class fileModel extends model /** * Compress image * - * @param string $pathName + * @param array $file * @access public * @return array */ - public function compressImage($pathName) + public function compressImage($file) { - if(!extension_loaded('gd')) return false; + if(!extension_loaded('gd')) return $file; + $pathName = $file['pathname']; $fileName = $this->savePath . $pathName; $suffix = strrchr($fileName, '.'); $lowerSuffix = strtolower($suffix); - if(!in_array($lowerSuffix, $this->config->file->imageFmt)) return false; + if(!in_array($lowerSuffix, $this->config->file->image2Compress)) return $file; $quality = 85; $newSuffix = '.jpg'; $compressedName = str_replace($suffix, $newSuffix, $pathName); - $res = ''; - if($lowerSuffix == '.bmp') - { - $res = $this->imagecreatefrombmp($fileName); - } - else - { - $res = imagecreatefromjpeg($fileName); - } - + $res = $lowerSuffix == '.bmp' ? $this->imagecreatefrombmp($fileName) : imagecreatefromjpeg($fileName); imagejpeg($res, $this->savePath . $compressedName, $quality); if($fileName != $this->savePath . $compressedName) unlink($fileName); - - return array('pathname' => $compressedName, 'extension' => ltrim($newSuffix, '.'), 'size' => filesize($this->savePath . $compressedName)); + $file['pathname'] = $compressedName; + $file['extension'] = ltrim($newSuffix, '.'); + $file['size'] = filesize($this->savePath . $compressedName); + return $file; } /**