3 namespace Drupal\Core\Image;
5 use Drupal\Core\ImageToolkit\ImageToolkitInterface;
8 * Defines an image object to represent an image file.
10 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
11 * @see \Drupal\image\ImageEffectInterface
15 class Image implements ImageInterface {
18 * Path of the image file.
22 protected $source = '';
25 * An image toolkit object.
27 * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
39 * Constructs a new Image object.
41 * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
43 * @param string|null $source
44 * (optional) The path to an image file, or NULL to construct the object
45 * with no image source.
47 public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
48 $this->toolkit = $toolkit;
50 $this->source = $source;
51 $this->getToolkit()->setSource($this->source);
52 // Defer image file validity check to the toolkit.
53 if ($this->getToolkit()->parseFile()) {
54 $this->fileSize = filesize($this->source);
62 public function isValid() {
63 return $this->getToolkit()->isValid();
69 public function getHeight() {
70 return $this->getToolkit()->getHeight();
76 public function getWidth() {
77 return $this->getToolkit()->getWidth();
83 public function getFileSize() {
84 return $this->fileSize;
90 public function getMimeType() {
91 return $this->getToolkit()->getMimeType();
97 public function getSource() {
104 public function getToolkitId() {
105 return $this->getToolkit()->getPluginId();
111 public function getToolkit() {
112 return $this->toolkit;
118 public function save($destination = NULL) {
119 // Return immediately if the image is not valid.
120 if (!$this->isValid()) {
124 $destination = $destination ?: $this->getSource();
125 if ($return = $this->getToolkit()->save($destination)) {
126 // Clear the cached file size and refresh the image information.
127 clearstatcache(TRUE, $destination);
128 $this->fileSize = filesize($destination);
129 $this->source = $destination;
131 // @todo Use File utility when https://www.drupal.org/node/2050759 is in.
132 if ($this->chmod($destination)) {
142 public function apply($operation, array $arguments = []) {
143 return $this->getToolkit()->apply($operation, $arguments);
149 public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
150 return $this->apply('create_new', ['width' => $width, 'height' => $height, 'extension' => $extension, 'transparent_color' => $transparent_color]);
156 public function convert($extension) {
157 return $this->apply('convert', ['extension' => $extension]);
163 public function crop($x, $y, $width, $height = NULL) {
164 return $this->apply('crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
170 public function desaturate() {
171 return $this->apply('desaturate', []);
177 public function resize($width, $height) {
178 return $this->apply('resize', ['width' => $width, 'height' => $height]);
184 public function rotate($degrees, $background = NULL) {
185 return $this->apply('rotate', ['degrees' => $degrees, 'background' => $background]);
191 public function scaleAndCrop($width, $height) {
192 return $this->apply('scale_and_crop', ['width' => $width, 'height' => $height]);
198 public function scale($width, $height = NULL, $upscale = FALSE) {
199 return $this->apply('scale', ['width' => $width, 'height' => $height, 'upscale' => $upscale]);
203 * Provides a wrapper for drupal_chmod() to allow unit testing.
206 * A string containing a URI file, or directory path.
208 * Integer value for the permissions. Consult PHP chmod() documentation for
211 * @see drupal_chmod()
213 * @todo Remove when https://www.drupal.org/node/2050759 is in.
216 * TRUE for success, FALSE in the event of an error.
218 protected function chmod($uri, $mode = NULL) {
219 return drupal_chmod($uri, $mode);