annotate core/lib/Drupal/Core/Image/Image.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children af1871eacc83
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@0 131 // @todo Use File utility when https://www.drupal.org/node/2050759 is in.
Chris@0 132 if ($this->chmod($destination)) {
Chris@0 133 return $return;
Chris@0 134 }
Chris@0 135 }
Chris@0 136 return FALSE;
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * {@inheritdoc}
Chris@0 141 */
Chris@0 142 public function apply($operation, array $arguments = []) {
Chris@0 143 return $this->getToolkit()->apply($operation, $arguments);
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * {@inheritdoc}
Chris@0 148 */
Chris@0 149 public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff') {
Chris@0 150 return $this->apply('create_new', ['width' => $width, 'height' => $height, 'extension' => $extension, 'transparent_color' => $transparent_color]);
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@0 154 * {@inheritdoc}
Chris@0 155 */
Chris@0 156 public function convert($extension) {
Chris@0 157 return $this->apply('convert', ['extension' => $extension]);
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function crop($x, $y, $width, $height = NULL) {
Chris@0 164 return $this->apply('crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
Chris@0 165 }
Chris@0 166
Chris@0 167 /**
Chris@0 168 * {@inheritdoc}
Chris@0 169 */
Chris@0 170 public function desaturate() {
Chris@0 171 return $this->apply('desaturate', []);
Chris@0 172 }
Chris@0 173
Chris@0 174 /**
Chris@0 175 * {@inheritdoc}
Chris@0 176 */
Chris@0 177 public function resize($width, $height) {
Chris@0 178 return $this->apply('resize', ['width' => $width, 'height' => $height]);
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * {@inheritdoc}
Chris@0 183 */
Chris@0 184 public function rotate($degrees, $background = NULL) {
Chris@0 185 return $this->apply('rotate', ['degrees' => $degrees, 'background' => $background]);
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * {@inheritdoc}
Chris@0 190 */
Chris@0 191 public function scaleAndCrop($width, $height) {
Chris@0 192 return $this->apply('scale_and_crop', ['width' => $width, 'height' => $height]);
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * {@inheritdoc}
Chris@0 197 */
Chris@0 198 public function scale($width, $height = NULL, $upscale = FALSE) {
Chris@0 199 return $this->apply('scale', ['width' => $width, 'height' => $height, 'upscale' => $upscale]);
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Provides a wrapper for drupal_chmod() to allow unit testing.
Chris@0 204 *
Chris@0 205 * @param string $uri
Chris@0 206 * A string containing a URI file, or directory path.
Chris@0 207 * @param int $mode
Chris@0 208 * Integer value for the permissions. Consult PHP chmod() documentation for
Chris@0 209 * more information.
Chris@0 210 *
Chris@0 211 * @see drupal_chmod()
Chris@0 212 *
Chris@0 213 * @todo Remove when https://www.drupal.org/node/2050759 is in.
Chris@0 214 *
Chris@0 215 * @return bool
Chris@0 216 * TRUE for success, FALSE in the event of an error.
Chris@0 217 */
Chris@0 218 protected function chmod($uri, $mode = NULL) {
Chris@0 219 return drupal_chmod($uri, $mode);
Chris@0 220 }
Chris@0 221
Chris@0 222 }