From 7e195182e9fce03786362072a160ba9e3f20d060 Mon Sep 17 00:00:00 2001 From: Cauan Cabral Date: Thu, 23 Jun 2011 16:09:51 -0400 Subject: [PATCH 1/3] Add option to resize original file --- models/behaviors/meio_upload.php | 83 +++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/models/behaviors/meio_upload.php b/models/behaviors/meio_upload.php index 5545b6c..85eca52 100644 --- a/models/behaviors/meio_upload.php +++ b/models/behaviors/meio_upload.php @@ -43,6 +43,12 @@ class MeioUploadBehavior extends ModelBehavior { 'allowedExt' => array('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.ico'), 'default' => false, // Not sure what this does 'zoomCrop' => false, // Whether to use ZoomCrop or not with PHPThumb + 'resizeOriginal' => false, // If you want original image to be resized + 'resize' => array( // Settings used in resize of original image + 'maxWidth' => 0, // Target width for image + 'maxHeight' => 0, // Target height for image + 'quality' => 75, // Quality of resized image + ), 'thumbnails' => true, 'thumbsizes' => array( // Place any custom thumbsize in model config instead, @@ -732,10 +738,21 @@ function _uploadFile(&$model, $data = null) { continue; } - // If the file is an image, try to make the thumbnails - if ((count($options['thumbsizes']) > 0) && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) { - $this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options); + // If the file is an image + if (count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) { + + // Try to make the thumbnails + if((count($options['thumbsizes']) > 0)) { + $this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options); + } + + // Try resize original image + if($options['resizeOriginal'] === true && $options['resize']['width'] > 0 && $options['resize']['height'] > 0) { + $params = $options['resize']; + $this->_resize($file, $params); + } } + if ($options['removeOriginal']) { $this->_removeOriginal($saveAs); } @@ -795,11 +812,22 @@ function _uploadFile(&$model, $data = null) { $result = array('return' => false, 'reason' => 'validation', 'extra' => array('field' => $fieldName, 'error' => $copyResults)); continue; } - - // If the file is an image, try to make the thumbnails - if ((count($options['thumbsizes']) > 0) && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) { - $this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options); + + // If the file is an image + if (count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) { + + // Try to make the thumbnails + if((count($options['thumbsizes']) > 0)) { + $this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options); + } + + // Try resize original image + if($options['resizeOriginal'] === true && $options['resize']['maxWidth'] > 0 && $options['resize']['maxHeight'] > 0) { + $params = $options['resize']; + $this->_resize($saveAs, $params); + } } + // Update model data $data[$model->alias][$options['fields']['dir']] = $options['dir']; @@ -926,6 +954,47 @@ function _createThumbnail(&$model, $source, $target, $fieldName, $params = array } } } + +/** + * Function to resize images + * + * @param string $file File name (without path) + * @param array $params + */ + function _resize($file, $params) { + $params = array_merge( + $this->defaultOptions['resize'], + $params + ); + + if(!class_exists('phpthumb')) + { + // Import phpThumb class + App::import('Vendor','phpthumb', array('file' => 'phpThumb'.DS.'phpthumb.class.php')); + } + + // Configuring thumbnail settings + $phpThumb = new phpthumb; + $phpThumb->setSourceFilename($file); + + $phpThumb->w = $params['maxWidth']; + $phpThumb->h = $params['maxHeight']; + $phpThumb->q = $params['quality']; + + $imageArray = explode(".", $file); + $phpThumb->config_output_format = $imageArray[1]; + unset($imageArray); + + // Setting whether to die upon error + $phpThumb->config_error_die_on_error = true; + + // Creating thumbnail + if ($phpThumb->GenerateThumbnail()) { + if (!$phpThumb->RenderToFile($file)) { + $this->_addError('Could not render image to: '.$target); + } + } + } /** * Merges two arrays recursively From 1eb958c5b1a130e336d6963a84b0b0988a1cec2e Mon Sep 17 00:00:00 2001 From: Cauan Cabral Date: Thu, 23 Jun 2011 16:30:10 -0400 Subject: [PATCH 2/3] Fix passed parameter at call _resize method --- models/behaviors/meio_upload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/behaviors/meio_upload.php b/models/behaviors/meio_upload.php index 85eca52..2818de3 100644 --- a/models/behaviors/meio_upload.php +++ b/models/behaviors/meio_upload.php @@ -749,7 +749,7 @@ function _uploadFile(&$model, $data = null) { // Try resize original image if($options['resizeOriginal'] === true && $options['resize']['width'] > 0 && $options['resize']['height'] > 0) { $params = $options['resize']; - $this->_resize($file, $params); + $this->_resize($saveAs, $params); } } From cc7b63041f32e706cc1a8b2c217fb72c87042575 Mon Sep 17 00:00:00 2001 From: Cauan Cabral Date: Wed, 27 Jul 2011 11:46:35 -0400 Subject: [PATCH 3/3] Minor changes in resize method signature --- models/behaviors/meio_upload.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/models/behaviors/meio_upload.php b/models/behaviors/meio_upload.php index 2818de3..79e8aae 100644 --- a/models/behaviors/meio_upload.php +++ b/models/behaviors/meio_upload.php @@ -749,7 +749,7 @@ function _uploadFile(&$model, $data = null) { // Try resize original image if($options['resizeOriginal'] === true && $options['resize']['width'] > 0 && $options['resize']['height'] > 0) { $params = $options['resize']; - $this->_resize($saveAs, $params); + $this->_resize($model, $saveAs, $params); } } @@ -824,7 +824,7 @@ function _uploadFile(&$model, $data = null) { // Try resize original image if($options['resizeOriginal'] === true && $options['resize']['maxWidth'] > 0 && $options['resize']['maxHeight'] > 0) { $params = $options['resize']; - $this->_resize($saveAs, $params); + $this->_resize($model, $saveAs, $params); } } @@ -958,10 +958,11 @@ function _createThumbnail(&$model, $source, $target, $fieldName, $params = array /** * Function to resize images * + * @param object $model Reference to model * @param string $file File name (without path) * @param array $params */ - function _resize($file, $params) { + function _resize(&$model, $file, $params) { $params = array_merge( $this->defaultOptions['resize'], $params @@ -974,7 +975,7 @@ function _resize($file, $params) { } // Configuring thumbnail settings - $phpThumb = new phpthumb; + $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($file); $phpThumb->w = $params['maxWidth']; @@ -986,14 +987,18 @@ function _resize($file, $params) { unset($imageArray); // Setting whether to die upon error - $phpThumb->config_error_die_on_error = true; + $phpThumb->config_error_die_on_error = false; - // Creating thumbnail + // Creating resized image if ($phpThumb->GenerateThumbnail()) { if (!$phpThumb->RenderToFile($file)) { - $this->_addError('Could not render image to: '.$target); + $this->_addError('Could not render image to: ' . $file); } } + else + { + $this->_addError('Could not resize image: ' . $file); + } } /**