Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #146 from davidyell/develop
Browse files Browse the repository at this point in the history
Merge to master for release
  • Loading branch information
davidyell committed Oct 6, 2015
2 parents e2c4a93 + db071cd commit 579b86f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 190 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ app/[Cc]onfig/database.php

/vendor/
composer.lock
.idea/*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +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)
* [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
Expand Down
13 changes: 11 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
24 changes: 3 additions & 21 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,9 @@ your customised thumbnails.
## Errors
If you are experiencing any of these errors, here are your solutions.

### Unknown type "proffer.file"
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.
Expand Down
35 changes: 35 additions & 0 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 0 additions & 16 deletions src/Error/DisabledExtension.php

This file was deleted.

22 changes: 14 additions & 8 deletions src/Lib/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,43 @@ 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);
}
}

/**
* 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);
}

/**
Expand Down
65 changes: 2 additions & 63 deletions src/Model/Validation/ProfferRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
80 changes: 0 additions & 80 deletions tests/TestCase/Model/Validation/ProfferRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit 579b86f

Please sign in to comment.