Image.php 5.2 KB

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