Просмотр файла vendor/intervention/image/src/Intervention/Image/Image.php

Размер файла: 19.87Kb
  1. <?php
  2.  
  3. namespace Intervention\Image;
  4.  
  5. use Intervention\Image\Exception\NotWritableException;
  6. use Intervention\Image\Exception\RuntimeException;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\StreamInterface;
  9.  
  10. /**
  11. * @method \Intervention\Image\Image backup(string $name = 'default') Backups current image state as fallback for reset method under an optional name. Overwrites older state on every call, unless a different name is passed.
  12. * @method \Intervention\Image\Image blur(int $amount = 1) Apply a gaussian blur filter with a optional amount on the current image. Use values between 0 and 100.
  13. * @method \Intervention\Image\Image brightness(int $level) Changes the brightness of the current image by the given level. Use values between -100 for min. brightness. 0 for no change and +100 for max. brightness.
  14. * @method \Intervention\Image\Image cache(\Closure $callback, int $lifetime = null, boolean $returnObj = false) Method to create a new cached image instance from a Closure callback. Pass a lifetime in minutes for the callback and decide whether you want to get an Intervention Image instance as return value or just receive the image stream.
  15. * @method \Intervention\Image\Image canvas(int $width, int $height, mixed $bgcolor = null) Factory method to create a new empty image instance with given width and height. You can define a background-color optionally. By default the canvas background is transparent.
  16. * @method \Intervention\Image\Image circle(int $radius, int $x, int $y, \Closure $callback = null) Draw a circle at given x, y, coordinates with given radius. You can define the appearance of the circle by an optional closure callback.
  17. * @method \Intervention\Image\Image colorize(int $red, int $green, int $blue) Change the RGB color values of the current image on the given channels red, green and blue. The input values are normalized so you have to include parameters from 100 for maximum color value. 0 for no change and -100 to take out all the certain color on the image.
  18. * @method \Intervention\Image\Image contrast(int $level) Changes the contrast of the current image by the given level. Use values between -100 for min. contrast 0 for no change and +100 for max. contrast.
  19. * @method \Intervention\Image\Image crop(int $width, int $height, int $x = null, int $y = null) Cut out a rectangular part of the current image with given width and height. Define optional x,y coordinates to move the top-left corner of the cutout to a certain position.
  20. * @method void destroy() Frees memory associated with the current image instance before the PHP script ends. Normally resources are destroyed automatically after the script is finished.
  21. * @method \Intervention\Image\Image ellipse(int $width, int $height, int $x, int $y, \Closure $callback = null) Draw a colored ellipse at given x, y, coordinates. You can define width and height and set the appearance of the circle by an optional closure callback.
  22. * @method mixed exif(string $key = null) Read Exif meta data from current image.
  23. * @method mixed iptc(string $key = null) Read Iptc meta data from current image.
  24. * @method \Intervention\Image\Image fill(mixed $filling, int $x = null, int $y = null) Fill current image with given color or another image used as tile for filling. Pass optional x, y coordinates to start at a certain point.
  25. * @method \Intervention\Image\Image flip(string $mode = 'h') Mirror the current image horizontally or vertically by specifying the mode.
  26. * @method \Intervention\Image\Image fit(int $width, int $height = null, \Closure $callback = null, string $position = 'center') Combine cropping and resizing to format image in a smart way. The method will find the best fitting aspect ratio of your given width and height on the current image automatically, cut it out and resize it to the given dimension. You may pass an optional Closure callback as third parameter, to prevent possible upsizing and a custom position of the cutout as fourth parameter.
  27. * @method \Intervention\Image\Image gamma(float $correction) Performs a gamma correction operation on the current image.
  28. * @method \Intervention\Image\Image greyscale() Turns image into a greyscale version.
  29. * @method \Intervention\Image\Image heighten(int $height, \Closure $callback = null) Resizes the current image to new height, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.
  30. * @method \Intervention\Image\Image insert(mixed $source, string $position = 'top-left', int $x = 0, int $y = 0) Paste a given image source over the current image with an optional position and a offset coordinate. This method can be used to apply another image as watermark because the transparency values are maintained.
  31. * @method \Intervention\Image\Image interlace(boolean $interlace = true) Determine whether an image should be encoded in interlaced or standard mode by toggling interlace mode with a boolean parameter. If an JPEG image is set interlaced the image will be processed as a progressive JPEG.
  32. * @method \Intervention\Image\Image invert() Reverses all colors of the current image.
  33. * @method \Intervention\Image\Image limitColors(int $count, mixed $matte = null) Method converts the existing colors of the current image into a color table with a given maximum count of colors. The function preserves as much alpha channel information as possible and blends transarent pixels against a optional matte color.
  34. * @method \Intervention\Image\Image line(int $x1, int $y1, int $x2, int $y2, \Closure $callback = null) Draw a line from x,y point 1 to x,y point 2 on current image. Define color and/or width of line in an optional Closure callback.
  35. * @method \Intervention\Image\Image make(mixed $source) Universal factory method to create a new image instance from source, which can be a filepath, a GD image resource, an Imagick object or a binary image data.
  36. * @method \Intervention\Image\Image mask(mixed $source, boolean $mask_with_alpha) Apply a given image source as alpha mask to the current image to change current opacity. Mask will be resized to the current image size. By default a greyscale version of the mask is converted to alpha values, but you can set mask_with_alpha to apply the actual alpha channel. Any transparency values of the current image will be maintained.
  37. * @method \Intervention\Image\Image opacity(int $transparency) Set the opacity in percent of the current image ranging from 100% for opaque and 0% for full transparency.
  38. * @method \Intervention\Image\Image orientate() This method reads the EXIF image profile setting 'Orientation' and performs a rotation on the image to display the image correctly.
  39. * @method mixed pickColor(int $x, int $y, string $format = 'array') Pick a color at point x, y out of current image and return in optional given format.
  40. * @method \Intervention\Image\Image pixel(mixed $color, int $x, int $y) Draw a single pixel in given color on x, y position.
  41. * @method \Intervention\Image\Image pixelate(int $size) Applies a pixelation effect to the current image with a given size of pixels.
  42. * @method \Intervention\Image\Image polygon(array $points, \Closure $callback = null) Draw a colored polygon with given points. You can define the appearance of the polygon by an optional closure callback.
  43. * @method \Intervention\Image\Image rectangle(int $x1, int $y1, int $x2, int $y2, \Closure $callback = null) Draw a colored rectangle on current image with top-left corner on x,y point 1 and bottom-right corner at x,y point 2. Define the overall appearance of the shape by passing a Closure callback as an optional parameter.
  44. * @method \Intervention\Image\Image reset(string $name = 'default') Resets all of the modifications to a state saved previously by backup under an optional name.
  45. * @method \Intervention\Image\Image resize(int $width = null, int $height = null, \Closure $callback = null) Resizes current image based on given width and/or height. To contraint the resize command, pass an optional Closure callback as third parameter.
  46. * @method \Intervention\Image\Image resizeCanvas(int $width, int $height, string $anchor = 'center', boolean $relative = false, mixed $bgcolor = null) Resize the boundaries of the current image to given width and height. An anchor can be defined to determine from what point of the image the resizing is going to happen. Set the mode to relative to add or subtract the given width or height to the actual image dimensions. You can also pass a background color for the emerging area of the image.
  47. * @method mixed response(string $format = null, int $quality = 90) Sends HTTP response with current image in given format and quality.
  48. * @method \Intervention\Image\Image rotate(float $angle, mixed $bgcolor = null) Rotate the current image counter-clockwise by a given angle. Optionally define a background color for the uncovered zone after the rotation.
  49. * @method \Intervention\Image\Image sharpen(int $amount = 10) Sharpen current image with an optional amount. Use values between 0 and 100.
  50. * @method \Intervention\Image\Image text(string $text, int $x = 0, int $y = 0, \Closure $callback = null) Write a text string to the current image at an optional x,y basepoint position. You can define more details like font-size, font-file and alignment via a callback as the fourth parameter.
  51. * @method \Intervention\Image\Image trim(string $base = 'top-left', array $away = array('top', 'bottom', 'left', 'right'), int $tolerance = 0, int $feather = 0) Trim away image space in given color. Define an optional base to pick a color at a certain position and borders that should be trimmed away. You can also set an optional tolerance level, to trim similar colors and add a feathering border around the trimed image.
  52. * @method \Intervention\Image\Image widen(int $width, \Closure $callback = null) Resizes the current image to new width, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.
  53. * @method StreamInterface stream(string $format = null, int $quality = 90) Build PSR-7 compatible StreamInterface with current image in given format and quality.
  54. * @method ResponseInterface psrResponse(string $format = null, int $quality = 90) Build PSR-7 compatible ResponseInterface with current image in given format and quality.
  55. */
  56. class Image extends File
  57. {
  58. /**
  59. * Instance of current image driver
  60. *
  61. * @var AbstractDriver
  62. */
  63. protected $driver;
  64.  
  65. /**
  66. * Image resource/object of current image processor
  67. *
  68. * @var mixed
  69. */
  70. protected $core;
  71.  
  72. /**
  73. * Array of Image resource backups of current image processor
  74. *
  75. * @var array
  76. */
  77. protected $backups = [];
  78.  
  79. /**
  80. * Last image encoding result
  81. *
  82. * @var string
  83. */
  84. public $encoded = '';
  85.  
  86. /**
  87. * Creates a new Image instance
  88. *
  89. * @param AbstractDriver $driver
  90. * @param mixed $core
  91. */
  92. public function __construct(AbstractDriver $driver = null, $core = null)
  93. {
  94. $this->driver = $driver;
  95. $this->core = $core;
  96. }
  97.  
  98. /**
  99. * Magic method to catch all image calls
  100. * usually any AbstractCommand
  101. *
  102. * @param string $name
  103. * @param Array $arguments
  104. * @return mixed
  105. */
  106. public function __call($name, $arguments)
  107. {
  108. $command = $this->driver->executeCommand($this, $name, $arguments);
  109. return $command->hasOutput() ? $command->getOutput() : $this;
  110. }
  111.  
  112. /**
  113. * Starts encoding of current image
  114. *
  115. * @param string $format
  116. * @param int $quality
  117. * @return \Intervention\Image\Image
  118. */
  119. public function encode($format = null, $quality = 90)
  120. {
  121. return $this->driver->encode($this, $format, $quality);
  122. }
  123.  
  124. /**
  125. * Saves encoded image in filesystem
  126. *
  127. * @param string $path
  128. * @param int $quality
  129. * @param string $format
  130. * @return \Intervention\Image\Image
  131. */
  132. public function save($path = null, $quality = null, $format = null)
  133. {
  134. $path = is_null($path) ? $this->basePath() : $path;
  135.  
  136. if (is_null($path)) {
  137. throw new NotWritableException(
  138. "Can't write to undefined path."
  139. );
  140. }
  141.  
  142. if ($format === null) {
  143. $format = pathinfo($path, PATHINFO_EXTENSION);
  144. }
  145.  
  146. $data = $this->encode($format, $quality);
  147. $saved = @file_put_contents($path, $data);
  148.  
  149. if ($saved === false) {
  150. throw new NotWritableException(
  151. "Can't write image data to path ({$path})"
  152. );
  153. }
  154.  
  155. // set new file info
  156. $this->setFileInfoFromPath($path);
  157.  
  158. return $this;
  159. }
  160.  
  161. /**
  162. * Runs a given filter on current image
  163. *
  164. * @param FiltersFilterInterface $filter
  165. * @return \Intervention\Image\Image
  166. */
  167. public function filter(Filters\FilterInterface $filter)
  168. {
  169. return $filter->applyFilter($this);
  170. }
  171.  
  172. /**
  173. * Returns current image driver
  174. *
  175. * @return \Intervention\Image\AbstractDriver
  176. */
  177. public function getDriver()
  178. {
  179. return $this->driver;
  180. }
  181.  
  182. /**
  183. * Sets current image driver
  184. * @param AbstractDriver $driver
  185. */
  186. public function setDriver(AbstractDriver $driver)
  187. {
  188. $this->driver = $driver;
  189.  
  190. return $this;
  191. }
  192.  
  193. /**
  194. * Returns current image resource/obj
  195. *
  196. * @return mixed
  197. */
  198. public function getCore()
  199. {
  200. return $this->core;
  201. }
  202.  
  203. /**
  204. * Sets current image resource
  205. *
  206. * @param mixed $core
  207. */
  208. public function setCore($core)
  209. {
  210. $this->core = $core;
  211.  
  212. return $this;
  213. }
  214.  
  215. /**
  216. * Returns current image backup
  217. *
  218. * @param string $name
  219. * @return mixed
  220. */
  221. public function getBackup($name = null)
  222. {
  223. $name = is_null($name) ? 'default' : $name;
  224.  
  225. if ( ! $this->backupExists($name)) {
  226. throw new RuntimeException(
  227. "Backup with name ({$name}) not available. Call backup() before reset()."
  228. );
  229. }
  230.  
  231. return $this->backups[$name];
  232. }
  233.  
  234. /**
  235. * Returns all backups attached to image
  236. *
  237. * @return array
  238. */
  239. public function getBackups()
  240. {
  241. return $this->backups;
  242. }
  243.  
  244. /**
  245. * Sets current image backup
  246. *
  247. * @param mixed $resource
  248. * @param string $name
  249. * @return self
  250. */
  251. public function setBackup($resource, $name = null)
  252. {
  253. $name = is_null($name) ? 'default' : $name;
  254.  
  255. $this->backups[$name] = $resource;
  256.  
  257. return $this;
  258. }
  259.  
  260. /**
  261. * Checks if named backup exists
  262. *
  263. * @param string $name
  264. * @return bool
  265. */
  266. private function backupExists($name)
  267. {
  268. return array_key_exists($name, $this->backups);
  269. }
  270.  
  271. /**
  272. * Checks if current image is already encoded
  273. *
  274. * @return boolean
  275. */
  276. public function isEncoded()
  277. {
  278. return ! empty($this->encoded);
  279. }
  280.  
  281. /**
  282. * Returns encoded image data of current image
  283. *
  284. * @return string
  285. */
  286. public function getEncoded()
  287. {
  288. return $this->encoded;
  289. }
  290.  
  291. /**
  292. * Sets encoded image buffer
  293. *
  294. * @param string $value
  295. */
  296. public function setEncoded($value)
  297. {
  298. $this->encoded = $value;
  299.  
  300. return $this;
  301. }
  302.  
  303. /**
  304. * Calculates current image width
  305. *
  306. * @return int
  307. */
  308. public function getWidth()
  309. {
  310. return $this->getSize()->width;
  311. }
  312.  
  313. /**
  314. * Alias of getWidth()
  315. *
  316. * @return int
  317. */
  318. public function width()
  319. {
  320. return $this->getWidth();
  321. }
  322.  
  323. /**
  324. * Calculates current image height
  325. *
  326. * @return int
  327. */
  328. public function getHeight()
  329. {
  330. return $this->getSize()->height;
  331. }
  332.  
  333. /**
  334. * Alias of getHeight
  335. *
  336. * @return int
  337. */
  338. public function height()
  339. {
  340. return $this->getHeight();
  341. }
  342.  
  343. /**
  344. * Reads mime type
  345. *
  346. * @return string
  347. */
  348. public function mime()
  349. {
  350. return $this->mime;
  351. }
  352.  
  353. /**
  354. * Returns encoded image data in string conversion
  355. *
  356. * @return string
  357. */
  358. public function __toString()
  359. {
  360. return $this->encoded;
  361. }
  362.  
  363. /**
  364. * Cloning an image
  365. */
  366. public function __clone()
  367. {
  368. $this->core = $this->driver->cloneCore($this->core);
  369. }
  370. }