PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/Image/Image.php

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