From fd4c001137590af42bcc0c309f15a5eec9f57113 Mon Sep 17 00:00:00 2001 From: Kamoris Date: Sun, 20 Sep 2015 18:52:47 +0200 Subject: [PATCH 1/6] Functionality to set jpeg_quality and other options for thumbnails --- docs/configuration.md | 13 +++++++++++-- src/Lib/ImageTransform.php | 22 ++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 631df6f..ece3b61 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -15,8 +15,17 @@ $this->addBehavior('Proffer.Proffer', [ 'root' => WWW_ROOT . 'files', // Customise the root upload folder here, or omit to use the default 'dir' => 'photo_dir', // The name of the field to store the folder 'thumbnailSizes' => [ // Declare your thumbnails - 'square' => ['w' => 200, 'h' => 200], // Define the size and prefix of your thumbnails - 'portrait' => ['w' => 100, 'h' => 300, 'crop' => true], // Crop will crop the image as well as resize it + 'square' => [ // Define the prefix of your thumbnail + 'w' => 200, // Width + 'h' => 200, // Height + 'crop' => true // Crop will crop the image as well as resize it + 'jpeg_quality' => 100, + 'png_compression_level' => 9 + ], + 'portrait' => [ // Define a second thumbnail + 'w' => 100, + 'h' => 300 + ], ], 'thumbnailMethod' => 'imagick' // Options are Imagick, Gd or Gmagick ] diff --git a/src/Lib/ImageTransform.php b/src/Lib/ImageTransform.php index 276981c..9fdbe1d 100644 --- a/src/Lib/ImageTransform.php +++ b/src/Lib/ImageTransform.php @@ -94,13 +94,13 @@ public function processThumbnails(array $config) return; } - foreach ($config['thumbnailSizes'] as $prefix => $dimensions) { + foreach ($config['thumbnailSizes'] as $prefix => $thumbnailConfig) { $method = null; if (!empty($config['thumbnailMethod'])) { $method = $config['thumbnailMethod']; } - $this->makeThumbnail($prefix, $dimensions, $method); + $this->makeThumbnail($prefix, $thumbnailConfig, $method); } } @@ -108,23 +108,29 @@ public function processThumbnails(array $config) * Generate and save the thumbnail * * @param string $prefix The thumbnail prefix - * @param array $dimensions Array of thumbnail dimensions + * @param array $config Array of thumbnail config * @param string $thumbnailMethod Which engine to use to make thumbnails * @return void */ - public function makeThumbnail($prefix, array $dimensions, $thumbnailMethod = 'gd') + public function makeThumbnail($prefix, array $config, $thumbnailMethod = 'gd') { + $defaultConfig = [ + 'jpeg_quality' => 100, + 'png_compression_level' => 9 + ]; + $config = array_merge($defaultConfig, $config); $this->setImagine($thumbnailMethod); $image = $this->getImagine()->open($this->Path->fullPath()); - if (isset($dimensions['crop']) && $dimensions['crop'] === true) { - $image = $this->thumbnailCropScale($image, $dimensions['w'], $dimensions['h']); + if (isset($config['crop']) && $config['crop'] === true) { + $image = $this->thumbnailCropScale($image, $config['w'], $config['h']); } else { - $image = $this->thumbnailScale($image, $dimensions['w'], $dimensions['h']); + $image = $this->thumbnailScale($image, $config['w'], $config['h']); } + unset($config['crop'], $config['w'], $config['h']); - $image->save($this->Path->fullPath($prefix), ['jpeg_quality' => 100, 'png_compression_level' => 9]); + $image->save($this->Path->fullPath($prefix), $config); } /** From 16d53b00152b15d291fb6a41909f01351814e06f Mon Sep 17 00:00:00 2001 From: Hiroki Shimizu Date: Tue, 29 Sep 2015 19:09:39 +0900 Subject: [PATCH 2/6] Fix faq.md explains old version instructions --- docs/faq.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/faq.md b/docs/faq.md index 881d413..c1f7eab 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -27,6 +27,10 @@ your customised thumbnails. If you are experiencing any of these errors, here are your solutions. ### Unknown type "proffer.file" + +:warning: *Since Proffer 0.5.0 introduced configuring schema settings automatically, +the following instructions are no longer needed when you use Proffer >= 0.5.* + This has two primary causes. The first is that in your `config/boostrap.php` you might have forgotten to include the From ce5197e864553fa1dbacb6e2bc65f595093e0548 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 6 Oct 2015 19:32:58 +0100 Subject: [PATCH 3/6] Resolves #139 --- README.md | 3 +++ docs/faq.md | 28 +++------------------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c732b5c..5c5ee95 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ All the documentation can be found in the [docs](docs) folder. * [Examples](docs/examples.md) * [FAQ](docs/faq.md) +**NB** Please be aware of the change of configuration between versions `0.4.2` and `0.5.0`. +[See the FAQ](/docs/faq.md#bootstrap-file-is-missing). + ##Contribution Please open a pull request or submit an issue if there is anything you would like to contribute. Please write a test for any new functionality that you add and be sure to run the tests before you commit. Also don't forget to run PHPCS with diff --git a/docs/faq.md b/docs/faq.md index c1f7eab..17cad84 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -26,31 +26,9 @@ your customised thumbnails. ## Errors If you are experiencing any of these errors, here are your solutions. -### Unknown type "proffer.file" - -:warning: *Since Proffer 0.5.0 introduced configuring schema settings automatically, -the following instructions are no longer needed when you use Proffer >= 0.5.* - -This has two primary causes. - -The first is that in your `config/boostrap.php` you might have forgotten to include the -`'bootstrap' => true` when loading the plugin, which means the datatype isn't loaded. - -```php -// config/bootstrap.php -Plugin::load('Proffer', ['bootstrap' => true]); -``` - -The second thing is that you might have forgotten to include the `_initializeSchema` method in your table class. This -method bind the data type class to the field. - -```php -// src/Model/Table/Examples.php -protected function _initializeSchema(\Cake\Database\Schema\Table $table) { - $table->columnType('file','proffer.file'); - return $table; -} -``` +### Bootstrap file is missing +Proffer `0.5.0` introduced configuring schema settings automatically, so you no longer need to include the the +`['bootstrap' => true]` when loading the plugin. ### File name is written to the database as "Array" The thing to check is your form is using the file type, and your input is also a file type. From 82fc4f169373fdf2a2e4f73883f1f95834f4a486 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 6 Oct 2015 19:55:34 +0100 Subject: [PATCH 4/6] Extended the correct class for validation. Removed duplicated validation methods, and updated the tests to match. Relates to #143 --- src/Error/DisabledExtension.php | 16 ---- src/Model/Validation/ProfferRules.php | 65 +-------------- .../Model/Validation/ProfferRulesTest.php | 80 ------------------- 3 files changed, 2 insertions(+), 159 deletions(-) delete mode 100644 src/Error/DisabledExtension.php diff --git a/src/Error/DisabledExtension.php b/src/Error/DisabledExtension.php deleted file mode 100644 index fa9cb52..0000000 --- a/src/Error/DisabledExtension.php +++ /dev/null @@ -1,16 +0,0 @@ - - * @when 16/02/15 - * - */ -namespace Proffer\Error; - -use \Cake\Core\Exception\Exception; - -class DisabledExtension extends Exception -{ -} diff --git a/src/Model/Validation/ProfferRules.php b/src/Model/Validation/ProfferRules.php index 94dd07f..f8ac2a6 100644 --- a/src/Model/Validation/ProfferRules.php +++ b/src/Model/Validation/ProfferRules.php @@ -7,72 +7,11 @@ namespace Proffer\Model\Validation; -use Cake\Core\Exception\Exception; -use Cake\Validation\Validator; -use finfo; -use Proffer\Error\DisabledExtension; +use Cake\Validation\Validation; -class ProfferRules extends Validator +class ProfferRules extends Validation { - /** - * Check the size of the image - * - * @param array $value An array of the name and value of the field - * @param int $size Filesize in bytes - * @return bool - */ - public static function filesize($value, $size) - { - if ($value['size'] <= $size) { - return true; - } - - return false; - } - - /** - * Make sure the extension matches the allowed - * - * @param array $value An array of the name and value of the field - * @param array $extensions Array of file extensions to allow - * @return bool - */ - public static function extension($value, array $extensions) - { - $extension = pathinfo($value['name'], PATHINFO_EXTENSION); - - if (in_array($extension, $extensions)) { - return true; - } - - return false; - } - - /** - * Check the mimetype of the file - * - * For a full list of mime types - * http://www.sitepoint.com/web-foundations/mime-types-complete-list/ - * - * @param array $value An array of the name and value of the field - * @param array $types An array of mime type strings to match - * @return bool - * @throws DisabledExtension - * @see http://php.net/manual/en/fileinfo.installation.php - */ - public static function mimetype($value, array $types) - { - if (!class_exists('finfo')) { - throw new DisabledExtension('Please enable the File Info extension in your php.ini'); - } - - $finfo = new finfo(); - $type = $finfo->file($value['tmp_name'], FILEINFO_MIME_TYPE); - - return in_array($type, $types); - } - /** * Validate the dimensions of an image. If the file isn't an image then validation will fail * diff --git a/tests/TestCase/Model/Validation/ProfferRulesTest.php b/tests/TestCase/Model/Validation/ProfferRulesTest.php index e345682..364c979 100644 --- a/tests/TestCase/Model/Validation/ProfferRulesTest.php +++ b/tests/TestCase/Model/Validation/ProfferRulesTest.php @@ -16,86 +16,6 @@ public function setUp() $this->Rules = new ProfferRules; } - public function providerFilesize() - { - return [ - [ - ['size' => 30000], - 35000, - true - ], - [ - ['size' => 35000], - 30000, - false - ], - [ - ['size' => 35000], - 35000, - true - ], - ]; - } - - /** - * @dataProvider providerFilesize - */ - public function testFilesize($value, $check, $expected) - { - $result = $this->Rules->filesize($value, $check); - $this->assertEquals($expected, $result); - } - - public function providerExtension() - { - return [ - [ - ['name' => 'image.jpg'], - ['jpg', 'jpeg', 'gif', 'png'], - true - ], - [ - ['name' => 'image.gif'], - ['jpg', 'png'], - false - ], - ]; - } - - /** - * @dataProvider providerExtension - */ - public function testExtension($value, $extensions, $expected) - { - $result = $this->Rules->extension($value, $extensions); - $this->assertEquals($expected, $result); - } - - public function providerMimeType() - { - return [ - [ - ['tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], - ['image/jpeg'], - true - ], - [ - ['tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], - ['image/gif'], - false - ], - ]; - } - - /** - * @dataProvider providerMimeType - */ - public function testMimeType($value, $types, $expected) - { - $result = $this->Rules->mimetype($value, $types); - $this->assertEquals($expected, $result); - } - public function providerDimensions() { return [ From 2e64fd8d429dfa622abe1909b619944b579adef4 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 6 Oct 2015 20:50:11 +0100 Subject: [PATCH 5/6] Don't need to store the phpstorm files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3504e5d..c959965 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ app/[Cc]onfig/database.php /vendor/ composer.lock +.idea/* From db071cd54e5f592a2e5cfcfe8dbaaf26e02b1fd0 Mon Sep 17 00:00:00 2001 From: David Yell Date: Tue, 6 Oct 2015 20:50:50 +0100 Subject: [PATCH 6/6] Updated the docs to reflect new changes, and help users upgrading --- README.md | 4 +--- docs/upgrading.md | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 docs/upgrading.md diff --git a/README.md b/README.md index 5c5ee95..126e1f5 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,7 @@ All the documentation can be found in the [docs](docs) folder. * [Shell tasks](docs/shell.md) * [Examples](docs/examples.md) * [FAQ](docs/faq.md) - -**NB** Please be aware of the change of configuration between versions `0.4.2` and `0.5.0`. -[See the FAQ](/docs/faq.md#bootstrap-file-is-missing). +* [Upgrading](docs/upgrading.md) ##Contribution Please open a pull request or submit an issue if there is anything you would like to contribute. Please write a test for diff --git a/docs/upgrading.md b/docs/upgrading.md new file mode 100644 index 0000000..5da8810 --- /dev/null +++ b/docs/upgrading.md @@ -0,0 +1,35 @@ +# Upgrading +If you are upgrading between versions this documentation page will give you some insights into the changes which you +will need to make and potential pitfalls. + +For more release information [please see the releases](https://github.com/davidyell/CakePHP3-Proffer/releases). + +## 0.6.0 +[Release 0.6.0](https://github.com/davidyell/CakePHP3-Proffer/releases/tag/0.6.0) + +When migrating to `0.6.0` you might encounter problems with validation, specifically the `filesize()` method. You will +need to change the param order to match, `fileSize($check, $operator = null, $size = null)`. This is documented in the +[api validation docs](http://api.cakephp.org/3.0/class-Cake.Validation.Validation.html#_fileSize). + +The `operator` can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, + equal to ==, not equal !=. + +## 0.5.0 +[Release 0.5.0](https://github.com/davidyell/CakePHP3-Proffer/tree/0.5.0) + +When upgrading to `0.5.0` you no longer need to bootstrap the plugin, as the data type class will be loaded +automatically. + +So the only change required is to change your `config/bootstrap.php` to be `Plugin::load('Proffer')`. + +## 0.4.0 +[Release 0.4.0](https://github.com/davidyell/CakePHP3-Proffer/releases/tag/v0.4.0) + +This version removes some of the events in the plugin, so any code which hooks the events will need to be updated. +Instead of hooking these events you can inject your own transform class in the plugin in which you can implement your +changes. + +## 0.3.0 +[Release 0.3.0](https://github.com/davidyell/CakePHP3-Proffer/releases/tag/v0.3.0) + +If you need to make the generation of thumbnails optional, this is now possible by updating the configuration. \ No newline at end of file