diff --git a/core/includes/image.inc b/core/includes/image.inc index ead62b9..d8d1a34 100644 --- a/core/includes/image.inc +++ b/core/includes/image.inc @@ -6,8 +6,6 @@ */ use Drupal\Core\Image\ImageFile; -use Drupal\system\Plugin\ImageToolkitInterface; -use Drupal\Component\Image\Image; /** * @defgroup image Image toolkits @@ -45,8 +43,6 @@ * * @param string $filepath * String specifying the path of the image file. - * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit - * (optional) An image toolkit object to override the default. * * @return array * FALSE, if the file could not be found or is not an image. Otherwise, a @@ -57,183 +53,14 @@ * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png'). * - "file_size": File size in bytes. */ -function image_get_info($filepath, ImageToolkitInterface $toolkit = NULL) { - $details = FALSE; - if (!is_file($filepath) && !is_uploaded_file($filepath)) { - return $details; - } - - if ($toolkit === NULL) { - $toolkit = Drupal::service('image.toolkit'); - } - if ($toolkit) { - $image = new ImageFile($filepath, $toolkit); - $details = $toolkit->getInfo($image); - if (isset($details) && is_array($details)) { - $details['file_size'] = filesize($filepath); - } - } - - return $details; -} - -/** - * Scales an image to the exact width and height given. - * - * This function achieves the target aspect ratio by cropping the original image - * equally on both sides, or equally on the top and bottom. This function is - * useful to create uniform sized avatars from larger images. - * - * The resulting image always has the exact target dimensions. - * - * @param \Drupal\Core\Image\ImageFile $image - * An image object returned by image_load(). - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see image_resize() - * @see image_crop() - */ -function image_scale_and_crop(ImageFile $image, $width, $height) { - $scale = max($width / $image->get('width'), $height / $image->get('height')); - $x = ($image->get('width') * $scale - $width) / 2; - $y = ($image->get('height') * $scale - $height) / 2; - - if (image_resize($image, $image->get('width') * $scale, $image->get('height') * $scale)) { - return image_crop($image, $x, $y, $width, $height); +function image_get_info($filepath) { + $image = new ImageFile($filepath); + if ($image->processInfo()) { + return $image->getInfo(); } return FALSE; } /** - * Scales an image while maintaining aspect ratio. - * - * The resulting image can be smaller for one or both target dimensions. - * - * @param \Drupal\Core\Image\ImageFile $image - * An image object returned by image_load(). - * @param int $width - * (optional) The target width, in pixels. This value is omitted then the - * scaling will based only on the height value. - * @param int $height - * (optional) The target height, in pixels. This value is omitted then the - * scaling will based only on the width value. - * @param bool $upscale - * (optional) Boolean indicating that files smaller than the dimensions will - * be scaled up. This generally results in a low quality image. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see image_scale_and_crop() - */ -function image_scale(ImageFile $image, $width = NULL, $height = NULL, $upscale = FALSE) { - $dimensions = $image->getInfo(); - - // Scale the dimensions - if they don't change then just return success. - if (!Image::scaleDimensions($dimensions, $width, $height, $upscale)) { - return TRUE; - } - - return image_resize($image, $dimensions['width'], $dimensions['height']); -} - -/** - * Resizes an image to the given dimensions (ignoring aspect ratio). - * - * @param \Drupal\Core\Image\ImageFile $image - * An image object returned by image_load(). - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::resize() - */ -function image_resize(ImageFile $image, $width, $height) { - $width = (int) round($width); - $height = (int) round($height); - - return $image->getToolkit()->resize($image, $width, $height); -} - -/** - * Rotates an image by the given number of degrees. - * - * @param $image - * An image object returned by image_load(). - * @param int $degrees - * The number of (clockwise) degrees to rotate the image. - * @param string $background - * (optional) An hexadecimal integer specifying the background color to use - * for the uncovered area of the image after the rotation. E.g. 0x000000 for - * black, 0xff00ff for magenta, and 0xffffff for white. For images that - * support transparency, this will default to transparent. Otherwise it will - * be white. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::rotate() - */ -function image_rotate(ImageFile $image, $degrees, $background = NULL) { - return $image->getToolkit()->rotate($image, $degrees, $background); -} - -/** - * Crops an image to a rectangle specified by the given dimensions. - * - * @param $image - * An image object returned by image_load(). - * @param int $x - * The top left coordinate, in pixels, of the crop area (x axis value). - * @param int $y - * The top left coordinate, in pixels, of the crop area (y axis value). - * @param int $width - * The target width, in pixels. - * @param int $height - * The target height, in pixels. - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see image_scale_and_crop() - * @see \Drupal\system\Plugin\ImageToolkitInterface::crop() - */ -function image_crop(ImageFile $image, $x, $y, $width, $height) { - $aspect = $image->get('height') / $image->get('width'); - if (empty($height)) $height = $width / $aspect; - if (empty($width)) $width = $height * $aspect; - - $width = (int) round($width); - $height = (int) round($height); - - return $image->getToolkit()->crop($image, $x, $y, $width, $height); -} - -/** - * Converts an image to grayscale. - * - * @param $image - * An image object returned by image_load(). - * - * @return bool - * TRUE on success, FALSE on failure. - * - * @see \Drupal\system\Plugin\ImageToolkitInterface::desaturate() - */ -function image_desaturate(ImageFile $image) { - return $image->getToolkit()->desaturate($image); -} - -/** * @} End of "defgroup image". */ diff --git a/core/lib/Drupal/Core/Image/ImageFile.php b/core/lib/Drupal/Core/Image/ImageFile.php index 97389fd..03d7f28 100644 --- a/core/lib/Drupal/Core/Image/ImageFile.php +++ b/core/lib/Drupal/Core/Image/ImageFile.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Image; +use Drupal\Component\Image\Image; use Drupal\system\Plugin\ImageToolkitInterface; /** @@ -36,11 +37,9 @@ class ImageFile { /** * @param $source - * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit */ - public function __construct($source, ImageToolkitInterface $toolkit) { + public function __construct($source) { $this->source = $source; - $this->toolkit = $toolkit; } /** @@ -59,9 +58,9 @@ public function setInfo(array $info) { } /** - * @param string|null $key + * @param string $key * - * @return mixed|array + * @return mixed|null */ public function get($key) { if (isset($this->info[$key])) { @@ -115,14 +114,24 @@ public function setToolkit(ImageToolkitInterface $toolkit) { /** * @return \Drupal\system\Plugin\ImageToolkitInterface */ - public function getToolkit() { + protected function getToolkit() { + if (!$this->toolkit) { + $this->toolkit = \Drupal::service('image.toolkit'); + } return $this->toolkit; } /** + * @return string + */ + public function getToolkitId() { + return $this->getToolkit()->getPluginId(); + } + + /** * Loads an image file and returns an image object. * - * Any changes to the file are not saved until image_save() is called. + * Any changes to the file are not saved until self::save() is called. * * @param string $source * Path to an image file. @@ -133,12 +142,12 @@ public function getToolkit() { * An image object or FALSE if there was a problem loading the file. */ public static function load($source, ImageToolkitInterface $toolkit = NULL) { - if ($toolkit === NULL) { - $toolkit = \Drupal::service('image.toolkit'); - } - $image = new static($source, $toolkit); + $image = new static($source); - $image->setInfo(image_get_info($source, $toolkit)); + if ($toolkit) { + $image->setToolkit($toolkit); + } + $image->processInfo($source); if ($toolkit->load($image)) { return $image; } @@ -161,10 +170,10 @@ public function save($destination = NULL) { if (empty($destination)) { $destination = $this->source; } - if ($return = $this->toolkit->save($this, $destination)) { + if ($return = $this->getToolkit()->save($this, $destination)) { // Clear the cached file size and refresh the image information. clearstatcache(TRUE, $destination); - $this->info = image_get_info($destination, $this->toolkit); + $this->processInfo($destination); if (drupal_chmod($destination)) { return $return; @@ -173,4 +182,160 @@ public function save($destination = NULL) { return FALSE; } + public function processInfo($destination = NULL) { + if (empty($destination)) { + $destination = $this->getSource(); + } + + if (!is_file($destination) && !is_uploaded_file($destination)) { + return FALSE; + } + + $details = $this->getToolkit()->getInfo($this); + if (isset($details) && is_array($details)) { + $details['file_size'] = filesize($destination); + } + $this->setInfo($details); + return TRUE; + } + + /** + * Scales an image while maintaining aspect ratio. + * + * The resulting image can be smaller for one or both target dimensions. + * + * @param int $width + * (optional) The target width, in pixels. This value is omitted then the + * scaling will based only on the height value. + * @param int $height + * (optional) The target height, in pixels. This value is omitted then the + * scaling will based only on the width value. + * @param bool $upscale + * (optional) Boolean indicating that files smaller than the dimensions will + * be scaled up. This generally results in a low quality image. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function scale($width = NULL, $height = NULL, $upscale = FALSE) { + $dimensions = $this->getInfo(); + + // Scale the dimensions - if they don't change then just return success. + if (!Image::scaleDimensions($dimensions, $width, $height, $upscale)) { + return TRUE; + } + + return $this->resize($dimensions['width'], $dimensions['height']); + + } + + /** + * Scales an image to the exact width and height given. + * + * This function achieves the target aspect ratio by cropping the original image + * equally on both sides, or equally on the top and bottom. This function is + * useful to create uniform sized avatars from larger images. + * + * The resulting image always has the exact target dimensions. + * + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function scaleAndCrop($width, $height) { + $scale = max($width / $this->get('width'), $height / $this->get('height')); + $x = ($this->get('width') * $scale - $width) / 2; + $y = ($this->get('height') * $scale - $height) / 2; + + if ($this->resize($this->get('width') * $scale, $this->get('height') * $scale)) { + return $this->crop($x, $y, $width, $height); + } + return FALSE; + } + + /** + * Crops an image to a rectangle specified by the given dimensions. + * + * @param int $x + * The top left coordinate, in pixels, of the crop area (x axis value). + * @param int $y + * The top left coordinate, in pixels, of the crop area (y axis value). + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @see \Drupal\system\Plugin\ImageToolkitInterface::crop() + */ + function crop($x, $y, $width, $height) { + $aspect = $this->get('height') / $this->get('width'); + if (empty($height)) $height = $width / $aspect; + if (empty($width)) $width = $height * $aspect; + + $width = (int) round($width); + $height = (int) round($height); + + return $this->getToolkit()->crop($this, $x, $y, $width, $height); + } + + /** + * Resizes an image to the given dimensions (ignoring aspect ratio). + * + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @see \Drupal\system\Plugin\ImageToolkitInterface::resize() + */ + public function resize($width, $height) { + $width = (int) round($width); + $height = (int) round($height); + + return $this->getToolkit()->resize($this, $width, $height); + } + + /** + * Converts an image to grayscale. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @see \Drupal\system\Plugin\ImageToolkitInterface::desaturate() + */ + public function desaturate() { + return $this->getToolkit()->desaturate($this); + } + + /** + * Rotates an image by the given number of degrees. + * + * @param int $degrees + * The number of (clockwise) degrees to rotate the image. + * @param string $background + * (optional) An hexadecimal integer specifying the background color to use + * for the uncovered area of the image after the rotation. E.g. 0x000000 for + * black, 0xff00ff for magenta, and 0xffffff for white. For images that + * support transparency, this will default to transparent. Otherwise it will + * be white. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @see \Drupal\system\Plugin\ImageToolkitInterface::rotate() + */ + public function rotate($degrees, $background = NULL) { + return $this->getToolkit()->rotate($this, $degrees, $background); + } + } diff --git a/core/modules/file/file.module b/core/modules/file/file.module index ad2d597..f437cae 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -464,9 +464,9 @@ function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $mi if ($info['width'] > $width || $info['height'] > $height) { // Try to resize the image to fit the dimensions. if ($image = ImageFile::load($file->getFileUri())) { - image_scale($image, $width, $height); + $image->scale($width, $height); $image->save(); - $file->filesize = $image->info['file_size']; + $file->filesize = $image->get('file_size'); drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); } else { diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php index 6d56e5a..776099f 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php @@ -34,8 +34,8 @@ public function processEffect(ImageFile $image) { list($x, $y) = explode('-', $this->configuration['anchor']); $x = image_filter_keyword($x, $image->get('width'), $this->configuration['width']); $y = image_filter_keyword($y, $image->get('height'), $this->configuration['height']); - if (!image_crop($image, $x, $y, $this->configuration['width'], $this->configuration['height'])) { - watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php index 60ce476..f2577c5 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php @@ -34,8 +34,8 @@ public function processDimensions(array &$dimensions) { * {@inheritdoc} */ public function processEffect(ImageFile $image) { - if (!image_desaturate($image)) { - watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->desaturate()) { + watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php index af46327..71ee0e4 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php @@ -27,8 +27,8 @@ class ResizeImageEffect extends ImageEffectBase { * {@inheritdoc} */ public function processEffect(ImageFile $image) { - if (!image_resize($image, $this->configuration['width'], $this->configuration['height'])) { - watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->resize($this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php index 0c32542..053eac7 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php @@ -53,8 +53,8 @@ public function processEffect(ImageFile $image) { $this->configuration['degrees'] = rand(-1 * $degrees, $degrees); } - if (!image_rotate($image, $this->configuration['degrees'], $this->configuration['bgcolor'])) { - watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) { + watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php index c619efb..66fddf7 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -26,8 +26,8 @@ class ScaleAndCropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function processEffect(ImageFile $image) { - if (!image_scale_and_crop($image, $this->configuration['width'], $this->configuration['height'])) { - watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->scaleAndCrop($this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php index 55d9c23..577e4b7 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php @@ -34,8 +34,8 @@ public function processEffect(ImageFile $image) { 'upscale' => FALSE, ); - if (!image_scale($image, $this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) { - watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkit()->getPluginId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); + if (!$image->scale($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) { + watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->get('mime_type'), '%dimensions' => $image->get('width') . 'x' . $image->get('height')), WATCHDOG_ERROR); return FALSE; } return TRUE; diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index 26ec4eb..90bd15f 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -45,8 +45,6 @@ function settingsFormSubmit($form, &$form_state); * * @return bool * TRUE or FALSE, based on success. - * - * @see image_resize() */ function resize(ImageFile $image, $width, $height); @@ -67,8 +65,6 @@ function resize(ImageFile $image, $width, $height); * * @return bool * TRUE or FALSE, based on success. - * - * @see image_rotate() */ function rotate(ImageFile $image, $degrees, $background = NULL); @@ -105,8 +101,6 @@ function crop(ImageFile $image, $x, $y, $width, $height); * * @return bool * TRUE or FALSE, based on success. - * - * @see image_desaturate() */ function desaturate(ImageFile $image); diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php index 0cbbc6d6..e02a6e1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php @@ -111,49 +111,49 @@ function testManipulations() { // Setup a list of tests to perform on each type. $operations = array( 'resize' => array( - 'function' => 'resize', + 'method' => 'resize', 'arguments' => array(20, 10), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'scale_x' => array( - 'function' => 'scale', + 'method' => 'scale', 'arguments' => array(20, NULL), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'scale_y' => array( - 'function' => 'scale', + 'method' => 'scale', 'arguments' => array(NULL, 10), 'width' => 20, 'height' => 10, 'corners' => $default_corners, ), 'upscale_x' => array( - 'function' => 'scale', + 'method' => 'scale', 'arguments' => array(80, NULL, TRUE), 'width' => 80, 'height' => 40, 'corners' => $default_corners, ), 'upscale_y' => array( - 'function' => 'scale', + 'method' => 'scale', 'arguments' => array(NULL, 40, TRUE), 'width' => 80, 'height' => 40, 'corners' => $default_corners, ), 'crop' => array( - 'function' => 'crop', + 'method' => 'crop', 'arguments' => array(12, 4, 16, 12), 'width' => 16, 'height' => 12, 'corners' => array_fill(0, 4, $this->white), ), 'scale_and_crop' => array( - 'function' => 'scale_and_crop', + 'method' => 'scaleAndCrop', 'arguments' => array(10, 8), 'width' => 10, 'height' => 8, @@ -165,28 +165,28 @@ function testManipulations() { if (function_exists('imagerotate')) { $operations += array( 'rotate_5' => array( - 'function' => 'rotate', + 'method' => 'rotate', 'arguments' => array(5, 0xFF00FF), // Fuchsia background. 'width' => 42, 'height' => 24, 'corners' => array_fill(0, 4, $this->fuchsia), ), 'rotate_90' => array( - 'function' => 'rotate', + 'method' => 'rotate', 'arguments' => array(90, 0xFF00FF), // Fuchsia background. 'width' => 20, 'height' => 40, 'corners' => array($this->transparent, $this->red, $this->green, $this->blue), ), 'rotate_transparent_5' => array( - 'function' => 'rotate', + 'method' => 'rotate', 'arguments' => array(5), 'width' => 42, 'height' => 24, 'corners' => array_fill(0, 4, $this->transparent), ), 'rotate_transparent_90' => array( - 'function' => 'rotate', + 'method' => 'rotate', 'arguments' => array(90), 'width' => 20, 'height' => 40, @@ -199,7 +199,7 @@ function testManipulations() { if (function_exists('imagefilter')) { $operations += array( 'desaturate' => array( - 'function' => 'desaturate', + 'method' => 'desaturate', 'arguments' => array(), 'height' => 20, 'width' => 40, @@ -238,11 +238,7 @@ function testManipulations() { } // Perform our operation. - $function = 'image_' . $values['function']; - $arguments = array(); - $arguments[] = &$image; - $arguments = array_merge($arguments, $values['arguments']); - call_user_func_array($function, $arguments); + call_user_func_array(array($image, $values['method']), $values['arguments']); // To keep from flooding the test with assert values, make a general // value for whether each group of values fail. diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php index 76f2ad1..1021372 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php @@ -40,7 +40,7 @@ function testGetAvailableToolkits() { function testLoad() { $image = ImageFile::load($this->file, $this->toolkit); $this->assertTrue(is_object($image), 'Returned an object.'); - $this->assertEqual($this->toolkit, $image->getToolkit(), t('Image had toolkit set.')); + $this->assertEqual($this->toolkit->getPluginId(), $image->getToolkitId(), t('Image had toolkit set.')); $this->assertToolkitOperationsCalled(array('load', 'get_info')); } @@ -56,7 +56,7 @@ function testSave() { * Test the image_resize() function. */ function testResize() { - $this->assertTrue(image_resize($this->image, 1, 2), 'Function returned the expected value.'); + $this->assertTrue($this->image->resize(1, 2), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('resize')); // Check the parameters. @@ -70,7 +70,7 @@ function testResize() { */ function testScale() { // TODO: need to test upscaling - $this->assertTrue(image_scale($this->image, 10, 10), 'Function returned the expected value.'); + $this->assertTrue($this->image->scale(10, 10), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('resize')); // Check the parameters. @@ -83,7 +83,7 @@ function testScale() { * Test the image_scale_and_crop() function. */ function testScaleAndCrop() { - $this->assertTrue(image_scale_and_crop($this->image, 5, 10), 'Function returned the expected value.'); + $this->assertTrue($this->image->scaleAndCrop(5, 10), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('resize', 'crop')); // Check the parameters. @@ -99,7 +99,7 @@ function testScaleAndCrop() { * Test the image_rotate() function. */ function testRotate() { - $this->assertTrue(image_rotate($this->image, 90, 1), 'Function returned the expected value.'); + $this->assertTrue($this->image->rotate(90, 1), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('rotate')); // Check the parameters. @@ -112,7 +112,7 @@ function testRotate() { * Test the image_crop() function. */ function testCrop() { - $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), 'Function returned the expected value.'); + $this->assertTrue($this->image->crop(1, 2, 3, 4), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('crop')); // Check the parameters. @@ -127,7 +127,7 @@ function testCrop() { * Test the image_desaturate() function. */ function testDesaturate() { - $this->assertTrue(image_desaturate($this->image), 'Function returned the expected value.'); + $this->assertTrue($this->image->desaturate(), 'Function returned the expected value.'); $this->assertToolkitOperationsCalled(array('desaturate')); // Check the parameters. diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php index 910294c..f2f8c39 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php @@ -28,7 +28,15 @@ * @var \Drupal\system\Plugin\ImageToolkitInterface */ protected $toolkit; + + /** + * @var string + */ protected $file; + + /** + * @var \Drupal\Core\Image\ImageFile + */ protected $image; function setUp() { @@ -44,7 +52,8 @@ function setUp() { // Setup a dummy image to work with, this replicate image_load() so we // can avoid calling it. - $this->image = new ImageFile($this->file, $this->toolkit); + $this->image = new ImageFile($this->file); + $this->image->setToolkit($this->toolkit); $this->image->setInfo(image_get_info($this->file)); // Clear out any hook calls.