Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,227 @@
<?php
/**
* @file
* Contains \Drupal\Core\Image\Image.
*/
namespace Drupal\Core\Image;
use Drupal\Core\ImageToolkit\ImageToolkitInterface;
/**
* Defines an image object to represent an image file.
*
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
* @see \Drupal\image\ImageEffectInterface
*
* @ingroup image
*/
class Image implements ImageInterface {
/**
* Path of the image file.
*
* @var string
*/
protected $source = '';
/**
* An image toolkit object.
*
* @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
*/
protected $toolkit;
/**
* File size in bytes.
*
* @var int
*/
protected $fileSize;
/**
* Constructs a new Image object.
*
* @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
* The image toolkit.
* @param string|null $source
* (optional) The path to an image file, or NULL to construct the object
* with no image source.
*/
public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
$this->toolkit = $toolkit;
$this->getToolkit()->setImage($this);
if ($source) {
$this->source = $source;
// Defer image file validity check to the toolkit.
if ($this->getToolkit()->parseFile()) {
$this->fileSize = filesize($this->source);
}
}
}
/**
* {@inheritdoc}
*/
public function isValid() {
return $this->getToolkit()->isValid();
}
/**
* {@inheritdoc}
*/
public function getHeight() {
return $this->getToolkit()->getHeight();
}
/**
* {@inheritdoc}
*/
public function getWidth() {
return $this->getToolkit()->getWidth();
}
/**
* {@inheritdoc}
*/
public function getFileSize() {
return $this->fileSize;
}
/**
* {@inheritdoc}
*/
public function getMimeType() {
return $this->getToolkit()->getMimeType();
}
/**
* {@inheritdoc}
*/
public function getSource() {
return $this->source;
}
/**
* {@inheritdoc}
*/
public function getToolkitId() {
return $this->getToolkit()->getPluginId();
}
/**
* {@inheritdoc}
*/
public function getToolkit() {
return $this->toolkit;
}
/**
* {@inheritdoc}
*/
public function save($destination = NULL) {
// Return immediately if the image is not valid.
if (!$this->isValid()) {
return FALSE;
}
$destination = $destination ?: $this->getSource();
if ($return = $this->getToolkit()->save($destination)) {
// Clear the cached file size and refresh the image information.
clearstatcache(TRUE, $destination);
$this->fileSize = filesize($destination);
$this->source = $destination;
// @todo Use File utility when https://www.drupal.org/node/2050759 is in.
if ($this->chmod($destination)) {
return $return;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function apply($operation, array $arguments = array()) {
return $this->getToolkit()->apply($operation, $arguments);
}
/**
* {@inheritdoc}
*/
public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
return $this->apply('create_new', array('width' => $width, 'height' => $height, 'extension' => $extension, 'transparent_color' => $transparent_color));
}
/**
* {@inheritdoc}
*/
public function convert($extension) {
return $this->apply('convert', array('extension' => $extension));
}
/**
* {@inheritdoc}
*/
public function crop($x, $y, $width, $height = NULL) {
return $this->apply('crop', array('x' => $x, 'y' => $y, 'width' => $width, 'height' => $height));
}
/**
* {@inheritdoc}
*/
public function desaturate() {
return $this->apply('desaturate', array());
}
/**
* {@inheritdoc}
*/
public function resize($width, $height) {
return $this->apply('resize', array('width' => $width, 'height' => $height));
}
/**
* {@inheritdoc}
*/
public function rotate($degrees, $background = NULL) {
return $this->apply('rotate', array('degrees' => $degrees, 'background' => $background));
}
/**
* {@inheritdoc}
*/
public function scaleAndCrop($width, $height) {
return $this->apply('scale_and_crop', array('width' => $width, 'height' => $height));
}
/**
* {@inheritdoc}
*/
public function scale($width, $height = NULL, $upscale = FALSE) {
return $this->apply('scale', array('width' => $width, 'height' => $height, 'upscale' => $upscale));
}
/**
* Provides a wrapper for drupal_chmod() to allow unit testing.
*
* @param string $uri
* A string containing a URI file, or directory path.
* @param int $mode
* Integer value for the permissions. Consult PHP chmod() documentation for
* more information.
*
* @see drupal_chmod()
*
* @todo Remove when https://www.drupal.org/node/2050759 is in.
*
* @return bool
* TRUE for success, FALSE in the event of an error.
*/
protected function chmod($uri, $mode = NULL) {
return drupal_chmod($uri, $mode);
}
}

View file

@ -0,0 +1,111 @@
<?php
/**
* @file
* Contains \Drupal\Core\Image\ImageFactory.
*/
namespace Drupal\Core\Image;
use Drupal\Core\ImageToolkit\ImageToolkitManager;
/**
* Provides a factory for image objects.
*/
class ImageFactory {
/**
* The image toolkit plugin manager.
*
* @var \Drupal\Core\ImageToolkit\ImageToolkitManager
*/
protected $toolkitManager;
/**
* The image toolkit ID to use for this factory.
*
* @var string
*/
protected $toolkitId;
/**
* Constructs a new ImageFactory object.
*
* @param \Drupal\Core\ImageToolkit\ImageToolkitManager $toolkit_manager
* The image toolkit plugin manager.
*/
public function __construct(ImageToolkitManager $toolkit_manager) {
$this->toolkitManager = $toolkit_manager;
$this->toolkitId = $this->toolkitManager->getDefaultToolkitId();
}
/**
* Sets the ID of the image toolkit.
*
* @param string $toolkit_id
* The ID of the image toolkit to use for this image factory.
*
* @return $this
*/
public function setToolkitId($toolkit_id) {
$this->toolkitId = $toolkit_id;
return $this;
}
/**
* Gets the ID of the image toolkit currently in use.
*
* @return string
* The ID of the image toolkit in use by the image factory.
*/
public function getToolkitId() {
return $this->toolkitId;
}
/**
* Constructs a new Image object.
*
* Normally, the toolkit set as default in the admin UI is used by the
* factory to create new Image objects. This can be overridden through
* \Drupal\Core\Image\ImageInterface::setToolkitId() so that any new Image
* object created will use the new toolkit specified. Finally, a single
* Image object can be created using a specific toolkit, regardless of the
* current factory settings, by passing its plugin ID in the $toolkit_id
* argument.
*
* @param string|null $source
* (optional) The path to an image file, or NULL to construct the object
* with no image source.
* @param string|null $toolkit_id
* (optional) The ID of the image toolkit to use for this image, or NULL
* to use the current toolkit.
*
* @return \Drupal\Core\Image\ImageInterface
* An Image object.
*
* @see ImageFactory::setToolkitId()
*/
public function get($source = NULL, $toolkit_id = NULL) {
$toolkit_id = $toolkit_id ?: $this->toolkitId;
return new Image($this->toolkitManager->createInstance($toolkit_id), $source);
}
/**
* Returns the image file extensions supported by the toolkit.
*
* @param string|null $toolkit_id
* (optional) The ID of the image toolkit to use for checking, or NULL
* to use the current toolkit.
*
* @return array
* An array of supported image file extensions (e.g. png/jpeg/gif).
*
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions()
*/
public function getSupportedExtensions($toolkit_id = NULL) {
$toolkit_id = $toolkit_id ?: $this->toolkitId;
$definition = $this->toolkitManager->getDefinition($toolkit_id);
return call_user_func($definition['class'] . '::getSupportedExtensions');
}
}

View file

@ -0,0 +1,243 @@
<?php
/**
* @file
* Contains \Drupal\Core\Image\ImageInterface.
*/
namespace Drupal\Core\Image;
/**
* Provides an interface for image objects.
*/
interface ImageInterface {
/**
* Checks if the image is valid.
*
* @return bool
* TRUE if the image object contains a valid image, FALSE otherwise.
*/
public function isValid();
/**
* Returns the height of the image.
*
* @return int|null
* The height of the image, or NULL if the image is invalid.
*/
public function getHeight();
/**
* Returns the width of the image.
*
* @return int|null
* The width of the image, or NULL if the image is invalid.
*/
public function getWidth();
/**
* Returns the size of the image file.
*
* @return int|null
* The size of the file in bytes, or NULL if the image is invalid.
*/
public function getFileSize();
/**
* Returns the MIME type of the image file.
*
* @return string
* The MIME type of the image file, or an empty string if the image is
* invalid.
*/
public function getMimeType();
/**
* Retrieves the source path of the image file.
*
* @return string
* The source path of the image file. An empty string if the source is
* not set.
*/
public function getSource();
/**
* Returns the image toolkit used for this image file.
*
* @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
* The image toolkit.
*/
public function getToolkit();
/**
* Returns the ID of the image toolkit used for this image file.
*
* @return string
* The ID of the image toolkit.
*/
public function getToolkitId();
/**
* Applies a toolkit operation to the image.
*
* The operation is deferred to the active toolkit.
*
* @param string $operation
* The operation to be performed against the image.
* @param array $arguments
* An associative array of arguments to be passed to the toolkit
* operation, e.g. array('width' => 50, 'height' => 100,
* 'upscale' => TRUE).
*
* @return bool
* TRUE on success, FALSE on failure.
*/
public function apply($operation, array $arguments = array());
/**
* Closes the image and saves the changes to a file.
*
* @param string|null $destination
* (optional) Destination path where the image should be saved. If it is empty
* the original image file will be overwritten.
*
* @return bool
* TRUE on success, FALSE on failure.
*
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::save()
*/
public function save($destination = NULL);
/**
* Prepares a new image, without loading it from a file.
*
* For a working example, see
* \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew.
*
* @param int $width
* The width of the new image, in pixels.
* @param int $height
* The height of the new image, in pixels.
* @param string $extension
* (Optional) The extension of the image file (e.g. 'png', 'gif', etc.).
* Allowed values depend on the implementation of the image toolkit.
* Defaults to 'png'.
* @param string $transparent_color
* (Optional) The hexadecimal string representing the color to be used
* for transparency, needed for GIF images. Defaults to '#ffffff' (white).
*
* @return bool
* TRUE on success, FALSE on failure.
*/
public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff');
/**
* Scales an image while maintaining aspect ratio.
*
* The resulting image can be smaller for one or both target dimensions.
*
* @param int|null $width
* The target width, in pixels. If this value is null then the scaling will
* be based only on the height value.
* @param int|null $height
* (optional) The target height, in pixels. If this value is null then the
* scaling will be 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, $height = NULL, $upscale = FALSE);
/**
* 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);
/**
* Instructs the toolkit to save the image in the format specified by the
* extension.
*
* @param string $extension
* The extension to convert to (e.g. 'jpeg' or 'png'). Allowed values depend
* on the current image toolkit.
*
* @return bool
* TRUE on success, FALSE on failure.
*
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions()
*/
public function convert($extension);
/**
* 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.
*/
public function crop($x, $y, $width, $height = NULL);
/**
* 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.
*/
public function resize($width, $height);
/**
* Converts an image to grayscale.
*
* @return bool
* TRUE on success, FALSE on failure.
*/
public function desaturate();
/**
* Rotates an image by the given number of degrees.
*
* @param float $degrees
* The number of (clockwise) degrees to rotate the image.
* @param string|null $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.
*/
public function rotate($degrees, $background = NULL);
}