Issue #2257587 by fietserwin: Remove parameter in calls between and within Image and toolkit.

8.0.x
Nathaniel Catchpole 2014-06-26 12:26:01 +01:00
parent d805f52278
commit dbb3bb005c
8 changed files with 163 additions and 143 deletions

View File

@ -58,6 +58,7 @@ class Image implements ImageInterface {
*/ */
public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
$this->toolkit = $toolkit; $this->toolkit = $toolkit;
$this->toolkit->setImage($this);
if ($source) { if ($source) {
$this->source = $source; $this->source = $source;
$this->parseFile(); $this->parseFile();
@ -75,14 +76,14 @@ class Image implements ImageInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getHeight() { public function getHeight() {
return $this->toolkit->getHeight($this); return $this->toolkit->getHeight();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getWidth() { public function getWidth() {
return $this->toolkit->getWidth($this); return $this->toolkit->getWidth();
} }
/** /**
@ -96,7 +97,7 @@ class Image implements ImageInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMimeType() { public function getMimeType() {
return $this->toolkit->getMimeType($this); return $this->toolkit->getMimeType();
} }
/** /**
@ -130,7 +131,7 @@ class Image implements ImageInterface {
} }
$destination = $destination ?: $this->getSource(); $destination = $destination ?: $this->getSource();
if ($return = $this->toolkit->save($this, $destination)) { if ($return = $this->toolkit->save($destination)) {
// Clear the cached file size and refresh the image information. // Clear the cached file size and refresh the image information.
clearstatcache(TRUE, $destination); clearstatcache(TRUE, $destination);
$this->fileSize = filesize($destination); $this->fileSize = filesize($destination);
@ -156,7 +157,7 @@ class Image implements ImageInterface {
* image information is populated. * image information is populated.
*/ */
protected function parseFile() { protected function parseFile() {
if ($this->valid = $this->toolkit->parseFile($this)) { if ($this->valid = $this->toolkit->parseFile()) {
$this->fileSize = filesize($this->source); $this->fileSize = filesize($this->source);
} }
return $this->valid; return $this->valid;
@ -189,13 +190,12 @@ class Image implements ImageInterface {
// removed through https://drupal.org/node/2073759, when // removed through https://drupal.org/node/2073759, when
// call_user_func_array() will be replaced by // call_user_func_array() will be replaced by
// $this->toolkit->apply($name, $this, $arguments). // $this->toolkit->apply($name, $this, $arguments).
if (in_array($method, array('setResource', 'getResource', 'hasResource', 'setWidth', 'setHeight', 'getType'))) { if (in_array($method, array('setResource', 'getResource', 'hasResource', 'setWidth', 'setHeight', 'getType', 'setImage'))) {
throw new \BadMethodCallException($method); throw new \BadMethodCallException($method);
} }
if (is_callable(array($this->toolkit, $method))) { if (is_callable(array($this->toolkit, $method))) {
// @todo In https://drupal.org/node/2073759, call_user_func_array() will // @todo In https://drupal.org/node/2073759, call_user_func_array() will
// be replaced by $this->toolkit->apply($name, $this, $arguments). // be replaced by $this->toolkit->apply($name, $arguments).
array_unshift($arguments, $this);
return call_user_func_array(array($this->toolkit, $method), $arguments); return call_user_func_array(array($this->toolkit, $method), $arguments);
} }
throw new \BadMethodCallException($method); throw new \BadMethodCallException($method);

View File

@ -7,10 +7,35 @@
namespace Drupal\Core\ImageToolkit; namespace Drupal\Core\ImageToolkit;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\PluginBase;
abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
/**
* Image object this toolkit instance is tied to.
*
* @var \Drupal\Core\Image\ImageInterface
*/
protected $image;
/**
* {@inheritdoc}
*/
public function setImage(ImageInterface $image) {
if ($this->image) {
throw new \BadMethodCallException(__METHOD__ . '() may only be called once.');
}
$this->image = $image;
}
/**
* {@inheritdoc}
*/
public function getImage() {
return $this->image;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -46,7 +46,7 @@ use Drupal\Core\Image\ImageInterface;
interface ImageToolkitInterface extends PluginInspectionInterface { interface ImageToolkitInterface extends PluginInspectionInterface {
/** /**
* Retrieves toolkit's settings form. * Retrieves the toolkit's settings form.
* *
* @see system_image_toolkit_settings() * @see system_image_toolkit_settings()
*/ */
@ -60,25 +60,40 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
public function settingsFormSubmit($form, &$form_state); public function settingsFormSubmit($form, &$form_state);
/** /**
* Scales an image to the specified size. * Sets the image object that this toolkit instance is tied to.
*
* @throws \BadMethodCallException
* When called twice.
* *
* @param \Drupal\Core\Image\ImageInterface $image * @param \Drupal\Core\Image\ImageInterface $image
* An image object. * The image that this toolkit instance will be tied to.
*/
public function setImage(ImageInterface $image);
/**
* Gets the image object that this toolkit instance is tied to.
*
* @return \Drupal\Core\Image\ImageInterface
* The image object that this toolkit instance is tied to.
*/
public function getImage();
/**
* Scales an image to the specified size.
*
* @param int $width * @param int $width
* The new width of the resized image, in pixels. * The new width of the resized image, in pixels.
* @param int $height * @param int $height
* The new height of the resized image, in pixels. * The new height of the resized image, in pixels.
* *
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE on success, FALSE on failure.
*/ */
public function resize(ImageInterface $image, $width, $height); public function resize($width, $height);
/** /**
* Rotates an image the given number of degrees. * Rotates an image the given number of degrees.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
* @param int $degrees * @param int $degrees
* The number of (clockwise) degrees to rotate the image. * The number of (clockwise) degrees to rotate the image.
* @param string $background * @param string $background
@ -89,15 +104,13 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
* be white. * be white.
* *
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE on success, FALSE on failure.
*/ */
public function rotate(ImageInterface $image, $degrees, $background = NULL); public function rotate($degrees, $background = NULL);
/** /**
* Crops an image. * Crops an image.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
* @param int $x * @param int $x
* The starting x offset at which to start the crop, in pixels. * The starting x offset at which to start the crop, in pixels.
* @param int $y * @param int $y
@ -108,46 +121,38 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
* The height of the cropped area, in pixels. * The height of the cropped area, in pixels.
* *
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE on success, FALSE on failure.
* *
* @see image_crop() * @see image_crop()
*/ */
public function crop(ImageInterface $image, $x, $y, $width, $height); public function crop($x, $y, $width, $height);
/** /**
* Converts an image resource to grayscale. * Converts an image resource to grayscale.
* *
* Note that transparent GIFs loose transparency when desaturated. * Note that transparent GIFs loose transparency when desaturated.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object. The $image->resource value will be modified by this
* call.
*
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE on success, FALSE on failure.
*/ */
public function desaturate(ImageInterface $image); public function desaturate();
/** /**
* Writes an image resource to a destination file. * Writes an image resource to a destination file.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
* @param string $destination * @param string $destination
* A string file URI or path where the image should be saved. * A string file URI or path where the image should be saved.
* *
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE on success, FALSE on failure.
*/ */
public function save(ImageInterface $image, $destination); public function save($destination);
/** /**
* Scales an image while maintaining aspect ratio. * Scales an image while maintaining aspect ratio.
* *
* The resulting image can be smaller for one or both target dimensions. * The resulting image can be smaller for one or both target dimensions.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
* @param int $width * @param int $width
* (optional) The target width, in pixels. This value is omitted then the * (optional) The target width, in pixels. This value is omitted then the
* scaling will based only on the height value. * scaling will based only on the height value.
@ -161,7 +166,7 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
* @return bool * @return bool
* TRUE on success, FALSE on failure. * TRUE on success, FALSE on failure.
*/ */
public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE); public function scale($width = NULL, $height = NULL, $upscale = FALSE);
/** /**
* Scales an image to the exact width and height given. * Scales an image to the exact width and height given.
@ -172,8 +177,6 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
* *
* The resulting image always has the exact target dimensions. * The resulting image always has the exact target dimensions.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
* @param int $width * @param int $width
* The target width, in pixels. * The target width, in pixels.
* @param int $height * @param int $height
@ -182,52 +185,40 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
* @return bool * @return bool
* TRUE on success, FALSE on failure. * TRUE on success, FALSE on failure.
*/ */
public function scaleAndCrop(ImageInterface $image, $width, $height); public function scaleAndCrop($width, $height);
/** /**
* Determines if a file contains a valid image. * Determines if a file contains a valid image.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
*
* @return bool * @return bool
* TRUE if the file could be found and is an image, FALSE otherwise. * TRUE if the file could be found and is an image, FALSE otherwise.
*/ */
public function parseFile(ImageInterface $image); public function parseFile();
/** /**
* Returns the height of the image. * Returns the height of the image.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
*
* @return int|null * @return int|null
* The height of the image, or NULL if the image is invalid. * The height of the image, or NULL if the image is invalid.
*/ */
public function getHeight(ImageInterface $image); public function getHeight();
/** /**
* Returns the width of the image. * Returns the width of the image.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
*
* @return int|null * @return int|null
* The width of the image, or NULL if the image is invalid. * The width of the image, or NULL if the image is invalid.
*/ */
public function getWidth(ImageInterface $image); public function getWidth();
/** /**
* Returns the MIME type of the image file. * Returns the MIME type of the image file.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
*
* @return string * @return string
* The MIME type of the image file, or an empty string if the image is * The MIME type of the image file, or an empty string if the image is
* invalid. * invalid.
*/ */
public function getMimeType(ImageInterface $image); public function getMimeType();
/** /**
* Gets toolkit requirements in a format suitable for hook_requirements(). * Gets toolkit requirements in a format suitable for hook_requirements().
@ -244,10 +235,10 @@ interface ImageToolkitInterface extends PluginInspectionInterface {
public function getRequirements(); public function getRequirements();
/** /**
* Verifies Image Toolkit is set up correctly. * Verifies that the Image Toolkit is set up correctly.
* *
* @return bool * @return bool
* True if the GD toolkit is available on this machine. * TRUE if the toolkit is available on this machine, FALSE otherwise.
*/ */
public static function isAvailable(); public static function isAvailable();

View File

@ -54,8 +54,8 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
$this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
} }
/** /**
@ -71,8 +71,8 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
$this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
} }
/** /**
@ -89,10 +89,10 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['crop'][0][1], 0, 'X was passed correctly'); $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly');
$this->assertEqual($calls['crop'][0][2], 1, 'Y was passed correctly'); $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly');
$this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
$this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
} }
/** /**
@ -107,8 +107,8 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); $this->assertEqual($calls['scaleAndCrop'][0][0], 5, 'Width was computed and passed correctly');
$this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); $this->assertEqual($calls['scaleAndCrop'][0][1], 10, 'Height was computed and passed correctly');
} }
/** /**
@ -120,7 +120,7 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
} }
/** /**
@ -136,8 +136,8 @@ class ImageEffectsTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly'); $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
$this->assertEqual($calls['rotate'][0][2], 0xffffff, 'Background color was passed correctly'); $this->assertEqual($calls['rotate'][0][1], 0xffffff, 'Background color was passed correctly');
} }
/** /**

View File

@ -8,7 +8,6 @@
namespace Drupal\system\Plugin\ImageToolkit; namespace Drupal\system\Plugin\ImageToolkit;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\ImageToolkit\ImageToolkitBase; use Drupal\Core\ImageToolkit\ImageToolkitBase;
use Drupal\Component\Utility\Image as ImageUtility; use Drupal\Component\Utility\Image as ImageUtility;
@ -87,7 +86,7 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function resize(ImageInterface $image, $width, $height) { public function resize($width, $height) {
// @todo Dimensions computation will be moved into a dedicated functionality // @todo Dimensions computation will be moved into a dedicated functionality
// in https://drupal.org/node/2108307. // in https://drupal.org/node/2108307.
$width = (int) round($width); $width = (int) round($width);
@ -99,7 +98,7 @@ class GDToolkit extends ImageToolkitBase {
$res = $this->createTmp($this->getType(), $width, $height); $res = $this->createTmp($this->getType(), $width, $height);
if (!imagecopyresampled($res, $this->getResource(), 0, 0, 0, 0, $width, $height, $this->getWidth($image), $this->getHeight($image))) { if (!imagecopyresampled($res, $this->getResource(), 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight())) {
return FALSE; return FALSE;
} }
@ -112,10 +111,10 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rotate(ImageInterface $image, $degrees, $background = NULL) { public function rotate($degrees, $background = NULL) {
// PHP installations using non-bundled GD do not have imagerotate. // PHP installations using non-bundled GD do not have imagerotate.
if (!function_exists('imagerotate')) { if (!function_exists('imagerotate')) {
watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->getSource())); watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getImage()->getSource()));
return FALSE; return FALSE;
} }
@ -161,10 +160,10 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function crop(ImageInterface $image, $x, $y, $width, $height) { public function crop($x, $y, $width, $height) {
// @todo Dimensions computation will be moved into a dedicated functionality // @todo Dimensions computation will be moved into a dedicated functionality
// in https://drupal.org/node/2108307. // in https://drupal.org/node/2108307.
$aspect = $this->getHeight($image) / $this->getWidth($image); $aspect = $this->getHeight() / $this->getWidth();
$height = empty($height) ? $width * $aspect : $height; $height = empty($height) ? $width * $aspect : $height;
$width = empty($width) ? $height / $aspect : $width; $width = empty($width) ? $height / $aspect : $width;
$width = (int) round($width); $width = (int) round($width);
@ -189,10 +188,10 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function desaturate(ImageInterface $image) { public function desaturate() {
// PHP installations using non-bundled GD do not have imagefilter. // PHP installations using non-bundled GD do not have imagefilter.
if (!function_exists('imagefilter')) { if (!function_exists('imagefilter')) {
watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->getSource())); watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $this->getImage()->getSource()));
return FALSE; return FALSE;
} }
@ -202,12 +201,12 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { public function scale($width = NULL, $height = NULL, $upscale = FALSE) {
// @todo Dimensions computation will be moved into a dedicated functionality // @todo Dimensions computation will be moved into a dedicated functionality
// in https://drupal.org/node/2108307. // in https://drupal.org/node/2108307.
$dimensions = array( $dimensions = array(
'width' => $this->getWidth($image), 'width' => $this->getWidth(),
'height' => $this->getHeight($image), 'height' => $this->getHeight(),
); );
// Scale the dimensions - if they don't change then just return success. // Scale the dimensions - if they don't change then just return success.
@ -215,21 +214,21 @@ class GDToolkit extends ImageToolkitBase {
return TRUE; return TRUE;
} }
return $this->resize($image, $dimensions['width'], $dimensions['height']); return $this->resize($dimensions['width'], $dimensions['height']);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function scaleAndCrop(ImageInterface $image, $width, $height) { public function scaleAndCrop($width, $height) {
// @todo Dimensions computation will be moved into a dedicated functionality // @todo Dimensions computation will be moved into a dedicated functionality
// in https://drupal.org/node/2108307. // in https://drupal.org/node/2108307.
$scale = max($width / $this->getWidth($image), $height / $this->getHeight($image)); $scale = max($width / $this->getWidth(), $height / $this->getHeight());
$x = ($this->getWidth($image) * $scale - $width) / 2; $x = ($this->getWidth() * $scale - $width) / 2;
$y = ($this->getHeight($image) * $scale - $height) / 2; $y = ($this->getHeight() * $scale - $height) / 2;
if ($this->resize($image, $this->getWidth($image) * $scale, $this->getHeight($image) * $scale)) { if ($this->resize($this->getWidth() * $scale, $this->getHeight() * $scale)) {
return $this->crop($image, $x, $y, $width, $height); return $this->crop($x, $y, $width, $height);
} }
return FALSE; return FALSE;
@ -238,15 +237,12 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* Loads a GD resource from a file. * Loads a GD resource from a file.
* *
* @param \Drupal\Core\Image\ImageInterface $image
* An image object.
*
* @return bool * @return bool
* TRUE or FALSE, based on success. * TRUE or FALSE, based on success.
*/ */
protected function load($image) { protected function load() {
$function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
if (function_exists($function) && $resource = $function($image->getSource())) { if (function_exists($function) && $resource = $function($this->getImage()->getSource())) {
$this->setResource($resource); $this->setResource($resource);
if (!imageistruecolor($resource)) { if (!imageistruecolor($resource)) {
// Convert indexed images to true color, so that filters work // Convert indexed images to true color, so that filters work
@ -264,7 +260,7 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function save(ImageInterface $image, $destination) { public function save($destination) {
$scheme = file_uri_scheme($destination); $scheme = file_uri_scheme($destination);
// Work around lack of stream wrapper support in imagejpeg() and imagepng(). // Work around lack of stream wrapper support in imagejpeg() and imagepng().
if ($scheme && file_stream_wrapper_valid_scheme($scheme)) { if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
@ -303,11 +299,11 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function parseFile(ImageInterface $image) { public function parseFile() {
$data = @getimagesize($image->getSource()); $data = @getimagesize($this->getImage()->getSource());
if ($data && in_array($data[2], static::supportedTypes())) { if ($data && in_array($data[2], static::supportedTypes())) {
$this->setType($data[2]); $this->setType($data[2]);
$this->load($image); $this->load();
return (bool) $this->getResource(); return (bool) $this->getResource();
} }
return FALSE; return FALSE;
@ -370,14 +366,14 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getWidth(ImageInterface $image) { public function getWidth() {
return $this->getResource() ? imagesx($this->getResource()) : NULL; return $this->getResource() ? imagesx($this->getResource()) : NULL;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getHeight(ImageInterface $image) { public function getHeight() {
return $this->getResource() ? imagesy($this->getResource()) : NULL; return $this->getResource() ? imagesy($this->getResource()) : NULL;
} }
@ -411,7 +407,7 @@ class GDToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMimeType(ImageInterface $image) { public function getMimeType() {
return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
} }

View File

@ -58,8 +58,8 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
$this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
} }
/** /**
@ -72,8 +72,8 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
$this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
} }
/** /**
@ -86,8 +86,8 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); $this->assertEqual($calls['scaleAndCrop'][0][0], 5, 'Width was computed and passed correctly');
$this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); $this->assertEqual($calls['scaleAndCrop'][0][1], 10, 'Height was computed and passed correctly');
} }
/** /**
@ -99,8 +99,8 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly'); $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
$this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly'); $this->assertEqual($calls['rotate'][0][1], 1, 'Background color was passed correctly');
} }
/** /**
@ -112,10 +112,10 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly'); $this->assertEqual($calls['crop'][0][0], 1, 'X was passed correctly');
$this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly'); $this->assertEqual($calls['crop'][0][1], 2, 'Y was passed correctly');
$this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
$this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
} }
/** /**
@ -127,6 +127,6 @@ class ToolkitTest extends ToolkitTestBase {
// Check the parameters. // Check the parameters.
$calls = $this->imageTestGetAllCalls(); $calls = $this->imageTestGetAllCalls();
$this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
} }
} }

View File

@ -8,7 +8,6 @@
namespace Drupal\image_test\Plugin\ImageToolkit; namespace Drupal\image_test\Plugin\ImageToolkit;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\ImageToolkit\ImageToolkitBase; use Drupal\Core\ImageToolkit\ImageToolkitBase;
/** /**
@ -70,9 +69,9 @@ class TestToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function parseFile(ImageInterface $image) { public function parseFile() {
$this->logCall('parseFile', array($image)); $this->logCall('parseFile', array());
$data = @getimagesize($image->getSource()); $data = @getimagesize($this->getImage()->getSource());
if ($data && in_array($data[2], static::supportedTypes())) { if ($data && in_array($data[2], static::supportedTypes())) {
$this->setType($data[2]); $this->setType($data[2]);
$this->width = $data[0]; $this->width = $data[0];
@ -85,8 +84,8 @@ class TestToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function save(ImageInterface $image, $destination) { public function save($destination) {
$this->logCall('save', array($image, $destination)); $this->logCall('save', array($destination));
// Return false so that image_save() doesn't try to chmod the destination // Return false so that image_save() doesn't try to chmod the destination
// file that we didn't bother to create. // file that we didn't bother to create.
return FALSE; return FALSE;
@ -95,48 +94,48 @@ class TestToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function crop(ImageInterface $image, $x, $y, $width, $height) { public function crop($x, $y, $width, $height) {
$this->logCall('crop', array($image, $x, $y, $width, $height)); $this->logCall('crop', array($x, $y, $width, $height));
return TRUE; return TRUE;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function resize(ImageInterface $image, $width, $height) { public function resize($width, $height) {
$this->logCall('resize', array($image, $width, $height)); $this->logCall('resize', array($width, $height));
return TRUE; return TRUE;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rotate(ImageInterface $image, $degrees, $background = NULL) { public function rotate($degrees, $background = NULL) {
$this->logCall('rotate', array($image, $degrees, $background)); $this->logCall('rotate', array($degrees, $background));
return TRUE; return TRUE;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function desaturate(ImageInterface $image) { public function desaturate() {
$this->logCall('desaturate', array($image)); $this->logCall('desaturate', array());
return TRUE; return TRUE;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { public function scale($width = NULL, $height = NULL, $upscale = FALSE) {
$this->logCall('scale', array($image, $width, $height, $upscale)); $this->logCall('scale', array($width, $height, $upscale));
return TRUE; return TRUE;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function scaleAndCrop(ImageInterface $image, $width, $height) { public function scaleAndCrop($width, $height) {
$this->logCall('scaleAndCrop', array($image, $width, $height)); $this->logCall('scaleAndCrop', array($width, $height));
return TRUE; return TRUE;
} }
@ -161,14 +160,14 @@ class TestToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getWidth(ImageInterface $image) { public function getWidth() {
return $this->width; return $this->width;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getHeight(ImageInterface $image) { public function getHeight() {
return $this->height; return $this->height;
} }
@ -202,7 +201,7 @@ class TestToolkit extends ImageToolkitBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMimeType(ImageInterface $image) { public function getMimeType() {
return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
} }

View File

@ -15,6 +15,13 @@ use Drupal\Tests\UnitTestCase;
*/ */
class ImageTest extends UnitTestCase { class ImageTest extends UnitTestCase {
/**
* Path to the image file.
*
* @var string
*/
protected $source;
/** /**
* Image object. * Image object.
* *
@ -121,11 +128,12 @@ class ImageTest extends UnitTestCase {
*/ */
public function testSave() { public function testSave() {
// This will fail if save() method isn't called on the toolkit. // This will fail if save() method isn't called on the toolkit.
$this->toolkit->expects($this->once()) $toolkit = $this->getToolkitMock();
$toolkit->expects($this->once())
->method('save') ->method('save')
->will($this->returnValue(TRUE)); ->will($this->returnValue(TRUE));
$image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource()));
$image->expects($this->any()) $image->expects($this->any())
->method('chmod') ->method('chmod')
->will($this->returnValue(TRUE)); ->will($this->returnValue(TRUE));
@ -150,11 +158,12 @@ class ImageTest extends UnitTestCase {
*/ */
public function testChmodFails() { public function testChmodFails() {
// This will fail if save() method isn't called on the toolkit. // This will fail if save() method isn't called on the toolkit.
$this->toolkit->expects($this->once()) $toolkit = $this->getToolkitMock();
$toolkit->expects($this->once())
->method('save') ->method('save')
->will($this->returnValue(TRUE)); ->will($this->returnValue(TRUE));
$image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource()));
$image->expects($this->any()) $image->expects($this->any())
->method('chmod') ->method('chmod')
->will($this->returnValue(FALSE)); ->will($this->returnValue(FALSE));
@ -182,7 +191,7 @@ class ImageTest extends UnitTestCase {
$toolkit->expects($this->any()) $toolkit->expects($this->any())
->method('resize') ->method('resize')
->will($this->returnArgument(2)); ->will($this->returnArgument(1));
$height = $image->scale(44); $height = $image->scale(44);
$this->assertEquals($height, 50); $this->assertEquals($height, 50);
} }
@ -196,7 +205,7 @@ class ImageTest extends UnitTestCase {
$toolkit->expects($this->any()) $toolkit->expects($this->any())
->method('resize') ->method('resize')
->will($this->returnArgument(1)); ->will($this->returnArgument(0));
$width = $image->scale(NULL, 50); $width = $image->scale(NULL, 50);
$this->assertEquals($width, 44); $this->assertEquals($width, 44);
} }
@ -211,7 +220,7 @@ class ImageTest extends UnitTestCase {
// Dimensions are the same, resize should not be called. // Dimensions are the same, resize should not be called.
$toolkit->expects($this->never()) $toolkit->expects($this->never())
->method('resize') ->method('resize')
->will($this->returnArgument(1)); ->will($this->returnArgument(0));
$width = $image->scale(88, 100); $width = $image->scale(88, 100);
$this->assertEquals($width, 88); $this->assertEquals($width, 88);
@ -230,7 +239,7 @@ class ImageTest extends UnitTestCase {
$toolkit->expects($this->once()) $toolkit->expects($this->once())
->method('crop') ->method('crop')
->will($this->returnArgument(1)); ->will($this->returnArgument(0));
$x = $image->scaleAndCrop(34, 50); $x = $image->scaleAndCrop(34, 50);
$this->assertEquals($x, 5); $this->assertEquals($x, 5);
@ -249,7 +258,7 @@ class ImageTest extends UnitTestCase {
$toolkit->expects($this->once()) $toolkit->expects($this->once())
->method('crop') ->method('crop')
->will($this->returnArgument(2)); ->will($this->returnArgument(1));
$y = $image->scaleAndCrop(44, 40); $y = $image->scaleAndCrop(44, 40);
$this->assertEquals($y, 5); $this->assertEquals($y, 5);
@ -300,7 +309,7 @@ class ImageTest extends UnitTestCase {
$toolkit->expects($this->once()) $toolkit->expects($this->once())
->method('crop') ->method('crop')
->will($this->returnArgument(3)); ->will($this->returnArgument(2));
$width = $image->crop(0, 0, 44, 50); $width = $image->crop(0, 0, 44, 50);
$this->assertEquals($width, 44); $this->assertEquals($width, 44);
} }