Skip to content

Commit

Permalink
Merge pull request #39 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Apr 26, 2018
2 parents c1516a8 + 9e46082 commit 9ad513f
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 167 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 1.x branch
## 1.5 branch
# 1.5.1
* added `ThumbCreator::getUrl()` method;
* added `getThumbCreatorInstance()` and `getThumbCreatorInstanceWithSave()`
methods to the `Thumber\TestSuite\TestCase` class. This simplifies tests.

# 1.5.0
* the plugin has been migrated to CakePHP 3.6;
* added `TestCase::skipIfDriverIs()` method;
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ init:

install:
- cd c:\
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-5.6.35-nts-Win32-VC11-x86.zip
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-5.6.36-nts-Win32-VC11-x86.zip
- 7z x php.zip -oc:\php > nul
- cd c:\php
- copy php.ini-production php.ini
Expand Down
3 changes: 1 addition & 2 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
$target = Configure::read(THUMBER . '.target');

if (!file_exists($target)) {
//@codingStandardsIgnoreLine
@mkdir($target, 0777, true);
safe_mkdir($target, 0777, true);
}

if (!is_writeable($target)) {
Expand Down
3 changes: 1 addition & 2 deletions src/TestSuite/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function tearDown()
parent::tearDown();

foreach (glob($this->getPath() . DS . '*') as $file) {
//@codingStandardsIgnoreLine
@unlink($file);
safe_unlink($file);
}
}

Expand Down
53 changes: 45 additions & 8 deletions src/TestSuite/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\Filesystem\Folder;
use Cake\TestSuite\TestCase as CakeTestCase;
use Thumber\ThumbsPathTrait;
use Thumber\Utility\ThumbCreator;
use Tools\ReflectionTrait;
use Tools\TestSuite\TestCaseTrait;

Expand All @@ -37,8 +38,7 @@ public function tearDown()
parent::tearDown();

foreach (glob($this->getPath() . DS . '*') as $file) {
//@codingStandardsIgnoreLine
@unlink($file);
safe_unlink($file);
}
}

Expand All @@ -51,7 +51,7 @@ protected static function createCopy($path)
{
$result = tempnam(sys_get_temp_dir(), $path);

copy($path, $result);
safe_copy($path, $result);

return $result;
}
Expand Down Expand Up @@ -80,10 +80,8 @@ public static function assertImageFileEquals($expected, $actual, $message = '')

self::assertFileEquals($expectedCopy, $actualCopy, $message);

//@codingStandardsIgnoreStart
@unlink($expectedCopy);
@unlink($actualCopy);
//@codingStandardsIgnoreEnd
safe_unlink($expectedCopy);
safe_unlink($actualCopy);
}

/**
Expand All @@ -96,7 +94,7 @@ public static function assertImageFileEquals($expected, $actual, $message = '')
*/
public function assertThumbPath($path, $message = '')
{
$regex = sprintf('/^%s[\w\d]{32}_[\w\d]{32}\.\w{3,4}/', preg_quote($this->getPath() . DS, '/'));
$regex = sprintf('/^%s[\w\d_]+\.\w{3,4}/', preg_quote($this->getPath() . DS, '/'));
self::assertRegExp($regex, $path, $message);
}

Expand All @@ -113,6 +111,45 @@ public function assertThumbUrl($url, $message = '')
self::assertRegExp('/^(http:\/\/localhost)?\/thumb\/[\w\d]+/', $url, $message);
}

/**
* Returns an instance of `ThumbCreator`
* @param string $path Path of the image from which to create the
* thumbnail. It can be a relative path (to APP/webroot/img), a full path
* or a remote url
* @return ThumbCreator
* @since 1.5.1
*/
protected function getThumbCreatorInstance($path = null)
{
return new ThumbCreator($path ?: '400x400.jpg');
}

/**
* Returns an instance of `ThumbCreator`, after calling `resize()` and
* `save()` methods.
*
* It can be called passing only the array of options as first argument.
* @param string $path Path of the image from which to create the
* thumbnail. It can be a relative path (to APP/webroot/img), a full path
* or a remote url
* @param array $options Options for saving
* @return ThumbCreator
* @since 1.5.1
* @uses getThumbCreatorInstance()
*/
protected function getThumbCreatorInstanceWithSave($path = null, array $options = [])
{
if (is_array($path) && func_num_args() < 2) {
$options = $path;
$path = null;
}

$thumbCreator = $this->getThumbCreatorInstance($path);
$thumbCreator->resize(200)->save($options);

return $thumbCreator;
}

/**
* Skips the test if you running the designated driver
* @param string $driver Driver name
Expand Down
35 changes: 33 additions & 2 deletions src/Utility/ThumbCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

use Cake\Core\Configure;
use Cake\Filesystem\Folder;
use Cake\Routing\Router;
use Intervention\Image\Constraint;
use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use InvalidArgumentException;
use RuntimeException;
use Thumber\ThumbsPathTrait;

Expand Down Expand Up @@ -61,11 +63,17 @@ class ThumbCreator
protected $driver;

/**
* File path
* Path of the file from which the thumbnail will be generated
* @var string
*/
protected $path;

/**
* Path of the generated thumbnail
* @var string
*/
protected $target;

/**
* Construct.
* It sets the file path and extension.
Expand Down Expand Up @@ -136,6 +144,28 @@ protected function getImageInstance()
return $imageInstance;
}

/**
* Builds and returns the url for the generated thumbnail
* @param bool $fullBase If `true`, the full base URL will be prepended to
* the result
* @return string
* @since 1.5.1
* @throws InvalidArgumentException
* @uses $target
*/
public function getUrl($fullBase = true)
{
if (empty($this->target)) {
throw new InvalidArgumentException(__d(
'thumber',
'Missing path of the generated thumbnail. Probably the `{0}` method has not been invoked',
'save()'
));
}

return Router::url(['_name' => 'thumb', base64_encode(basename($this->target))], $fullBase);
}

/**
* Crops the image, cutting out a rectangular part of the image.
*
Expand Down Expand Up @@ -280,6 +310,7 @@ public function resizeCanvas($width, $heigth = null, array $options = [])
* @uses $callbacks
* @uses $driver
* @uses $path
* @uses $target
*/
public function save(array $options = [])
{
Expand Down Expand Up @@ -324,6 +355,6 @@ public function save(array $options = [])
//Resets arguments and callbacks
$this->arguments = $this->callbacks = [];

return $target;
return $this->target = $target;
}
}
11 changes: 5 additions & 6 deletions src/View/Helper/ThumbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ThumbHelper extends Helper
* Helpers
* @var array
*/
public $helpers = ['Html', 'Url'];
public $helpers = ['Html'];

/**
* Magic method.
Expand Down Expand Up @@ -109,15 +109,14 @@ protected function runUrlMethod($name, $path, array $params = [], array $options
$params += ['format' => 'jpg', 'height' => null, 'width' => null];
$options += ['fullBase' => true];

//Creates the thumbnail
$thumb = new ThumbCreator($path);
$thumber = new ThumbCreator($path);

if (!method_exists($thumb, $name)) {
if (!method_exists($thumber, $name)) {
throw new RuntimeException(__d('thumber', 'Method {0}::{1} does not exist', get_class($this), $name));
}

$thumb = $thumb->$name($params['width'], $params['height'])->save($params);
$thumber->$name($params['width'], $params['height'])->save($params);

return $this->Url->build(['_name' => 'thumb', base64_encode(basename($thumb))], $options['fullBase']);
return $thumber->getUrl($options['fullBase']);
}
}
3 changes: 1 addition & 2 deletions tests/TestCase/Controller/ThumbsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public function testThumb()
$this->assertResponseCode(304);

//Deletes the last thumbnail file. Now the `Last-Modified` header is different
//@codingStandardsIgnoreLine
@unlink($thumb);
safe_unlink($thumb);
sleep(1);
$thumb = (new ThumbCreator($file))->resize(200)->save();
$this->get($url);
Expand Down
Loading

0 comments on commit 9ad513f

Please sign in to comment.