annotate core/lib/Drupal/Core/Image/Image.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Image;
Chris@0 4
Chris@0 5 use Drupal\Core\ImageToolkit\ImageToolkitInterface;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Defines an image object to represent an image file.
Chris@0 9 *
Chris@0 10 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
Chris@0 11 * @see \Drupal\image\ImageEffectInterface
Chris@0 12 *
Chris@0 13 * @ingroup image
Chris@0 14 */
Chris@0 15 class Image implements ImageInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Path of the image file.
Chris@0 19 *
Chris@0 20 * @var string
Chris@0 21 */
Chris@0 22 protected $source = '';
Chris@0 23
Chris@0 24 /**
Chris@0 25 * An image toolkit object.
Chris@0 26 *
Chris@0 27 * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
Chris@0 28 */
Chris@0 29 protected $toolkit;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * File size in bytes.
Chris@0 33 *
Chris@0 34 * @var int
Chris@0 35 */
Chris@0 36 protected $fileSize;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Constructs a new Image object.
Chris@0 40 *
Chris@0 41 * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
Chris@0 42 * The image toolkit.
Chris@0 43 * @param string|null $source
Chris@0 44 * (optional) The path to an image file, or NULL to construct the object
Chris@0 45 * with no image source.
Chris@0 46 */
Chris@0 47 public function __construct(ImageToolkitInterface $toolkit, $source = NULL) {
Chris@0 48 $this->toolkit = $toolkit;
Chris@0 49 if ($source) {
Chris@0 50 $this->source = $source;
Chris@0 51 $this->getToolkit()->setSource($this->source);
Chris@0 52 // Defer image file validity check to the toolkit.
Chris@0 53 if ($this->getToolkit()->parseFile()) {
Chris@0 54 $this->fileSize = filesize($this->source);
Chris@0 55 }
Chris@0 56 }
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * {@inheritdoc}
Chris@0 61 */
Chris@0 62 public function isValid() {
Chris@0 63 return $this->getToolkit()->isValid();
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * {@inheritdoc}
Chris@0 68 */
Chris@0 69 public function getHeight() {
Chris@0 70 return $this->getToolkit()->getHeight();
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * {@inheritdoc}
Chris@0 75 */
Chris@0 76 public function getWidth() {
Chris@0 77 return $this->getToolkit()->getWidth();
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 public function getFileSize() {
Chris@0 84 return $this->fileSize;
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function getMimeType() {
Chris@0 91 return $this->getToolkit()->getMimeType();
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 public function getSource() {
Chris@0 98 return $this->source;
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * {@inheritdoc}
Chris@0 103 */
Chris@0 104 public function getToolkitId() {
Chris@0 105 return $this->getToolkit()->getPluginId();
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * {@inheritdoc}
Chris@0 110 */
Chris@0 111 public function getToolkit() {
Chris@0 112 return $this->toolkit;
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * {@inheritdoc}
Chris@0 117 */
Chris@0 118 public function save($destination = NULL) {
Chris@0 119 // Return immediately if the image is not valid.
Chris@0 120 if (!$this->isValid()) {
Chris@0 121 return FALSE;
Chris@0 122 }
Chris@0 123
Chris@0 124 $destination = $destination ?: $this->getSource();
Chris@0 125 if ($return = $this->getToolkit()->save($destination)) {
Chris@0 126 // Clear the cached file size and refresh the image information.
Chris@0 127 clearstatcache(TRUE, $destination);
Chris@0 128 $this->fileSize = filesize($destination);
Chris@0 129 $this->source = $destination;
Chris@0 130
Chris@18 131 if (\Drupal::service('file_system')->chmod($destination)) {
Chris@0 132 return $return;
Chris@0 133 }
Chris@0 134 }
Chris@0 135 return FALSE;
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * {@inheritdoc}
Chris@0 140 */
Chris@0 141 public function apply($operation, array $arguments = []) {
Chris@0 142 return $this->getToolkit()->apply($operation, $arguments);
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * {@inheritdoc}
Chris@0 147 */
Chris@0 148 public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
Chris@0 149 return $this->apply('create_new', ['width' => $width, 'height' => $height, 'extension' => $extension, 'transparent_color' => $transparent_color]);
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * {@inheritdoc}
Chris@0 154 */
Chris@0 155 public function convert($extension) {
Chris@0 156 return $this->apply('convert', ['extension' => $extension]);
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * {@inheritdoc}
Chris@0 161 */
Chris@0 162 public function crop($x, $y, $width, $height = NULL) {
Chris@0 163 return $this->apply('crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
Chris@0 164 }
Chris@0 165
Chris@0 166 /**
Chris@0 167 * {@inheritdoc}
Chris@0 168 */
Chris@0 169 public function desaturate() {
Chris@0 170 return $this->apply('desaturate', []);
Chris@0 171 }
Chris@0 172
Chris@0 173 /**
Chris@0 174 * {@inheritdoc}
Chris@0 175 */
Chris@0 176 public function resize($width, $height) {
Chris@0 177 return $this->apply('resize', ['width' => $width, 'height' => $height]);
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * {@inheritdoc}
Chris@0 182 */
Chris@0 183 public function rotate($degrees, $background = NULL) {
Chris@0 184 return $this->apply('rotate', ['degrees' => $degrees, 'background' => $background]);
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * {@inheritdoc}
Chris@0 189 */
Chris@0 190 public function scaleAndCrop($width, $height) {
Chris@0 191 return $this->apply('scale_and_crop', ['width' => $width, 'height' => $height]);
Chris@0 192 }
Chris@0 193
Chris@0 194 /**
Chris@0 195 * {@inheritdoc}
Chris@0 196 */
Chris@0 197 public function scale($width, $height = NULL, $upscale = FALSE) {
Chris@0 198 return $this->apply('scale', ['width' => $width, 'height' => $height, 'upscale' => $upscale]);
Chris@0 199 }
Chris@0 200
Chris@0 201 /**
Chris@0 202 * Provides a wrapper for drupal_chmod() to allow unit testing.
Chris@0 203 *
Chris@0 204 * @param string $uri
Chris@0 205 * A string containing a URI file, or directory path.
Chris@0 206 * @param int $mode
Chris@0 207 * Integer value for the permissions. Consult PHP chmod() documentation for
Chris@0 208 * more information.
Chris@0 209 *
Chris@0 210 * @return bool
Chris@0 211 * TRUE for success, FALSE in the event of an error.
Chris@18 212 *
Chris@18 213 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
Chris@18 214 * Use \Drupal\Core\File\FileSystem::chmod().
Chris@18 215 *
Chris@18 216 * @see \Drupal\Core\File\FileSystemInterface::chmod()
Chris@18 217 * @see https://www.drupal.org/node/2418133
Chris@0 218 */
Chris@0 219 protected function chmod($uri, $mode = NULL) {
Chris@18 220 @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);
Chris@18 221 return \Drupal::service('file_system')->chmod($uri, $mode);
Chris@0 222 }
Chris@0 223
Chris@0 224 }