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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Jan 28, 2016
2 parents 109a77b + 87e4e84 commit c88eaa4
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 188 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"cakephp/cakephp": "3.*",
"imagine/imagine": "*",
"imagine/imagine": "0.6.*",
"cakephp/plugin-installer": "*"
},
"require-dev": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 61 additions & 61 deletions src/Controller/Component/ImagineComponent.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Copyright 2011-2015, Florian Krämer
* Copyright 2011-2016, Florian Krämer
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* Copyright 2011-2015, Florian Krämer
* Copyright 2011-2016, Florian Krämer
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Burzum\Imagine\Controller\Component;
Expand All @@ -25,54 +25,54 @@
*/
class ImagineComponent extends Component {

/**
* Default config
*
* These are merged with user-provided config when the component is used.
*
* @var array
*/
/**
* Default config
*
* These are merged with user-provided config when the component is used.
*
* @var array
*/
protected $_defaultConfig = [
'hashField' => 'hash',
'checkHash' => true,
'actions' => [],
];

/**
* Controller instance
*
* @var object
*/
/**
* Controller instance
*
* @var object
*/
public $Controller;

/**
* Image processing operations taken by ImagineBehavior::processImage()
*
* This property is auto populated by ImagineComponent::unpackParams()
*
* @var array
*/
/**
* Image processing operations taken by ImagineBehavior::processImage()
*
* This property is auto populated by ImagineComponent::unpackParams()
*
* @var array
*/
public $operations = [];

/**
* Constructor
*
* @param \Cake\Controller\ComponentRegistry $collection
* @param array $config Config options array
*/
/**
* Constructor
*
* @param \Cake\Controller\ComponentRegistry $collection
* @param array $config Config options array
*/
public function __construct(ComponentRegistry $collection, $config = []) {
parent::__construct($collection, $config);
$Controller = $collection->getController();
$this->request = $Controller->request;
$this->response = $Controller->response;
}

/**
* Start Up
*
* @param Event $Event
* @return void
*/
/**
* Start Up
*
* @param Event $Event
* @return void
*/
public function startup(Event $Event) {
$Controller = $Event->subject();
$this->Controller = $Controller;
Expand All @@ -86,16 +86,16 @@ public function startup(Event $Event) {
}
}

/**
* Creates a hash based on the named params but ignores the hash field
*
* The hash can also be used to determine if there is already a cached version
* of the requested image that was processed with these params. How you do that
* is up to you.
*
* @throws InvalidArgumentException
* @return mixed String if a hash could be retrieved, false if not
*/
/**
* Creates a hash based on the named params but ignores the hash field
*
* The hash can also be used to determine if there is already a cached version
* of the requested image that was processed with these params. How you do that
* is up to you.
*
* @throws InvalidArgumentException
* @return mixed String if a hash could be retrieved, false if not
*/
public function getHash() {
$mediaSalt = Configure::read('Imagine.salt');
if (empty($mediaSalt)) {
Expand All @@ -111,17 +111,17 @@ public function getHash() {
return false;
}

/**
* Compares the hash passed within the named args with the hash calculated based
* on the other named args and the imagine salt
*
* This is done to avoid that people can randomly generate tons of images by
* just incrementing the width and height for example in the url.
*
* @param bool $error If set to false no 404 page will be rendered if the hash is wrong
* @throws NotFoundException if the hash was not present
* @return bool True if the hashes match
*/
/**
* Compares the hash passed within the named args with the hash calculated based
* on the other named args and the imagine salt
*
* This is done to avoid that people can randomly generate tons of images by
* just incrementing the width and height for example in the url.
*
* @param bool $error If set to false no 404 page will be rendered if the hash is wrong
* @throws NotFoundException if the hash was not present
* @return bool True if the hashes match
*/
public function checkHash($error = true) {
if (!isset($this->request->query[$this->_config['hashField']]) && $error) {
throw new NotFoundException();
Expand All @@ -136,13 +136,13 @@ public function checkHash($error = true) {
return $result;
}

/**
* Unpacks the strings into arrays that were packed with ImagineHelper::pack()
*
* @param array $namedParams
* @internal param array $params If empty the method tries to get them from Controller->request['named']
* @return array Array with operation options for imagine, if none found an empty array
*/
/**
* Unpacks the strings into arrays that were packed with ImagineHelper::pack()
*
* @param array $namedParams
* @internal param array $params If empty the method tries to get them from Controller->request['named']
* @return array Array with operation options for imagine, if none found an empty array
*/
public function unpackParams($namedParams = []) {
if (empty($namedParams)) {
$namedParams = $this->request->query;
Expand Down
77 changes: 56 additions & 21 deletions src/Lib/ImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,39 +369,74 @@ public function rotate(array $options = []) {
/**
* Wrapper for Imagines thumbnail.
*
* This method had a bunch of issues and the code inside the method is a
* workaround! Please see:
*
* @link https://github.com/burzum/cakephp-imagine-plugin/issues/42
* @link https://github.com/avalanche123/Imagine/issues/478
*
* @throws \InvalidArgumentException
* @param array Array of options for processing the image.
* @throws InvalidArgumentException if no height or width was passed
* @return $this
*/
public function thumbnail(array $options = []) {
$this->_thumbnail($this->_image, $options);
return $this;
}

/**
* Workaround method...
*
* Seriously, I have NO IDEA why this only works when passed through a
* method as reference... If you use the very same code in the thumbnail()
* method directly it doesn't resize. I think there is weird bug with the
* references or something in Imagine itself going on. Their own example is
* not working properly. See https://github.com/avalanche123/Imagine/issues/478
*
* @link https://github.com/avalanche123/Imagine/issues/478
* @param \Imagine\Image\AbstractImage
* @throws InvalidArgumentException if no height or width was passed
* @return void
*/
protected function _thumbnail(&$Image, $options) {
if (empty($options['height']) || empty($options['width'])) {
throw new \InvalidArgumentException(__d('imagine', 'You have to pass height and width in the options!'));
}

$mode = ImageInterface::THUMBNAIL_INSET;
if (isset($options['mode']) && $options['mode'] === 'outbound') {
$mode = ImageInterface::THUMBNAIL_OUTBOUND;
}
$Image = $Image->thumbnail(new Box($options['width'], $options['height']), $mode);

$filter = ImageInterface::FILTER_UNDEFINED;
if (isset($options['filter'])) {
$filter = $options['filter'];
}

$size = new Box($options['width'], $options['height']);
$imageSize = $this->_image->getSize();
$ratios = array(
$size->getWidth() / $imageSize->getWidth(),
$size->getHeight() / $imageSize->getHeight()
);

// if target width is larger than image width
// AND target height is longer than image height
if ($size->contains($imageSize)) {
return $this->_image;
}
if ($mode === ImageInterface::THUMBNAIL_INSET) {
$ratio = min($ratios);
} else {
$ratio = max($ratios);
}
if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) {
if (!$imageSize->contains($size)) {
$size = new Box(
min($imageSize->getWidth(), $size->getWidth()),
min($imageSize->getHeight(), $size->getHeight())
);
} else {
$imageSize = $this->_image->getSize()->scale($ratio);
$this->_image->resize($imageSize, $filter);
}
$this->_image->crop(new Point(
max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)),
max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))
), $size);
} else {
if (!$imageSize->contains($size)) {
$imageSize = $imageSize->scale($ratio);
$this->_image->resize($imageSize, $filter);
} else {
$imageSize = $this->_image->getSize()->scale($ratio);
$this->_image->resize($imageSize, $filter);
}
}

return $this;
}

/**
Expand All @@ -416,7 +451,7 @@ public function resize(array $options = []) {
throw new \InvalidArgumentException(__d('imagine', 'You have to pass height and width in the options!'));
}

$this->_image = $this->_image->resize(new Box($options['width'], $options['height']));
$this->_image->resize(new Box($options['width'], $options['height']));
return $this;
}

Expand Down
Loading

0 comments on commit c88eaa4

Please sign in to comment.