View source
<?php
namespace Drupal\Core\Image;
use Drupal\Core\ImageToolkit\ImageToolkitInterface;
class Image implements ImageInterface {
protected $source = '';
protected $toolkit;
protected $fileSize;
public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
$this->toolkit = $toolkit;
if ($source) {
$this->source = $source;
$this
->getToolkit()
->setSource($this->source);
if ($this
->getToolkit()
->parseFile()) {
$this->fileSize = filesize($this->source);
}
}
}
public function isValid() {
return $this
->getToolkit()
->isValid();
}
public function getHeight() {
return $this
->getToolkit()
->getHeight();
}
public function getWidth() {
return $this
->getToolkit()
->getWidth();
}
public function getFileSize() {
return $this->fileSize;
}
public function getMimeType() {
return $this
->getToolkit()
->getMimeType();
}
public function getSource() {
return $this->source;
}
public function getToolkitId() {
return $this
->getToolkit()
->getPluginId();
}
public function getToolkit() {
return $this->toolkit;
}
public function save($destination = NULL) {
if (!$this
->isValid()) {
return FALSE;
}
$destination = $destination ?: $this
->getSource();
if ($return = $this
->getToolkit()
->save($destination)) {
clearstatcache(TRUE, $destination);
$this->fileSize = filesize($destination);
$this->source = $destination;
if (\Drupal::service('file_system')
->chmod($destination)) {
return $return;
}
}
return FALSE;
}
public function apply($operation, array $arguments = []) {
return $this
->getToolkit()
->apply($operation, $arguments);
}
public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
return $this
->apply('create_new', [
'width' => $width,
'height' => $height,
'extension' => $extension,
'transparent_color' => $transparent_color,
]);
}
public function convert($extension) {
return $this
->apply('convert', [
'extension' => $extension,
]);
}
public function crop($x, $y, $width, $height = NULL) {
return $this
->apply('crop', [
'x' => $x,
'y' => $y,
'width' => $width,
'height' => $height,
]);
}
public function desaturate() {
return $this
->apply('desaturate', []);
}
public function resize($width, $height) {
return $this
->apply('resize', [
'width' => $width,
'height' => $height,
]);
}
public function rotate($degrees, $background = NULL) {
return $this
->apply('rotate', [
'degrees' => $degrees,
'background' => $background,
]);
}
public function scaleAndCrop($width, $height) {
return $this
->apply('scale_and_crop', [
'width' => $width,
'height' => $height,
]);
}
public function scale($width, $height = NULL, $upscale = FALSE) {
return $this
->apply('scale', [
'width' => $width,
'height' => $height,
'upscale' => $upscale,
]);
}
protected function chmod($uri, $mode = NULL) {
@trigger_error('chmod() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \\Drupal\\Core\\File\\FileSystemInterface::chmod(). See https://www.drupal.org/node/2418133.', E_USER_DEPRECATED);
return \Drupal::service('file_system')
->chmod($uri, $mode);
}
}