Newer
Older
<?php
/**
* @file
* Contains \Drupal\Core\Image\Image.
*/
namespace Drupal\Core\Image;
Angie Byron
committed
use Drupal\Core\ImageToolkit\ImageToolkitInterface;
/**
* Defines an image object to represent an image file.
*
Angie Byron
committed
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
* @see \Drupal\image\ImageEffectInterface
*
* @ingroup image
*/
class Image implements ImageInterface {
/**
Alex Pott
committed
* Path of the image file.
*
* @var string
*/
Alex Pott
committed
protected $source = '';
/**
* An image toolkit object.
*
Angie Byron
committed
* @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
*/
protected $toolkit;
/**
* File size in bytes.
*
* @var int
*/
Alex Pott
committed
protected $fileSize;
/**
* Constructs a new Image object.
*
Angie Byron
committed
* @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
* The image toolkit.
catch
committed
* @param string|null $source
* (optional) The path to an image file, or NULL to construct the object
* with no image source.
*/
catch
committed
public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
$this->toolkit = $toolkit;
Alex Pott
committed
$this->getToolkit()->setImage($this);
catch
committed
if ($source) {
$this->source = $source;
// Defer image file validity check to the toolkit.
if ($this->getToolkit()->parseFile()) {
$this->fileSize = filesize($this->source);
}
catch
committed
}
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function isValid() {
return $this->getToolkit()->isValid();
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function getHeight() {
Alex Pott
committed
return $this->getToolkit()->getHeight();
}
/**
* {@inheritdoc}
*/
public function getWidth() {
Alex Pott
committed
return $this->getToolkit()->getWidth();
}
/**
* {@inheritdoc}
*/
public function getFileSize() {
return $this->fileSize;
}
/**
* {@inheritdoc}
*/
public function getMimeType() {
Alex Pott
committed
return $this->getToolkit()->getMimeType();
}
/**
* {@inheritdoc}
*/
public function getSource() {
return $this->source;
}
/**
* {@inheritdoc}
*/
public function getToolkitId() {
Alex Pott
committed
return $this->getToolkit()->getPluginId();
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function getToolkit() {
return $this->toolkit;
}
/**
* {@inheritdoc}
*/
public function save($destination = NULL) {
Alex Pott
committed
// Return immediately if the image is not valid.
if (!$this->isValid()) {
return FALSE;
}
Alex Pott
committed
$destination = $destination ?: $this->getSource();
Alex Pott
committed
if ($return = $this->getToolkit()->save($destination)) {
// Clear the cached file size and refresh the image information.
clearstatcache(TRUE, $destination);
Alex Pott
committed
$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;
}
/**
Alex Pott
committed
* {@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));
}
Angie Byron
committed
/**
* {@inheritdoc}
*/
public function convert($extension) {
return $this->apply('convert', array('extension' => $extension));
}
Alex Pott
committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* {@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.