Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. <?php
  2.  
  3. namespace {
  4.  
  5. use Illuminate\Support\HigherOrderTapProxy;
  6.  
  7. if (!function_exists('tap')) {
  8. /**
  9. * Call the given Closure with the given value then return the value.
  10. *
  11. * @param mixed $value
  12. * @param callable|null $callback
  13. *
  14. * @return mixed
  15. */
  16. function tap($value, $callback = null)
  17. {
  18. if ($callback === null) {
  19. return new HigherOrderTapProxy($value);
  20. }
  21.  
  22. $callback($value);
  23.  
  24. return $value;
  25. }
  26. }
  27. }
  28.  
  29. namespace Illuminate\Support {
  30.  
  31. class HigherOrderTapProxy
  32. {
  33. /**
  34. * The target being tapped.
  35. *
  36. * @var mixed
  37. */
  38. public $target;
  39.  
  40. /**
  41. * Create a new tap proxy instance.
  42. *
  43. * @param mixed $target
  44. *
  45. * @return void
  46. */
  47. public function __construct($target)
  48. {
  49. $this->target = $target;
  50. }
  51.  
  52. /**
  53. * Dynamically pass method calls to the target.
  54. *
  55. * @param string $method
  56. * @param array $parameters
  57. *
  58. * @return mixed
  59. */
  60. public function __call($method, $parameters)
  61. {
  62. $this->target->{$method}(...$parameters);
  63.  
  64. return $this->target;
  65. }
  66. }
  67. }
  68.  
  69. namespace Finagin {
  70.  
  71. use RuntimeException;
  72.  
  73. class Image
  74. {
  75. protected const READERS = [
  76. 'imagecreatefrombmp',
  77. 'imagecreatefromgd2',
  78. 'imagecreatefromgd2part',
  79. //'imagecreatefromgd',
  80. 'imagecreatefromgif',
  81. 'imagecreatefromjpeg',
  82. 'imagecreatefrompng',
  83. 'imagecreatefromstring',
  84. 'imagecreatefromwbmp',
  85. 'imagecreatefromwebp',
  86. 'imagecreatefromxbm',
  87. 'imagecreatefromxpm',
  88. ];
  89.  
  90. /**
  91. * @var resource|null
  92. */
  93. protected $source;
  94.  
  95. public function __construct($filename)
  96. {
  97. $this->source = static::read($filename);
  98. }
  99.  
  100. protected static function read(string $filename)
  101. {
  102. $image = null;
  103.  
  104. set_error_handler(function () {
  105. throw new RuntimeException;
  106. }, E_ALL);
  107.  
  108. foreach (static::READERS as $reader) {
  109. try {
  110. $image = $reader($filename);
  111. break;
  112. } catch (\Throwable $t) {
  113. }
  114. }
  115.  
  116. restore_error_handler();
  117.  
  118. return $image;
  119. }
  120.  
  121. public static function tap($value, $callback)
  122. {
  123. $callback($value);
  124.  
  125. return $value;
  126. }
  127.  
  128. public function rotate(float $degrees)
  129. {
  130. $width = imagesx($this->source);
  131. $height = imagesy($this->source);
  132.  
  133. $max = max($width, $height);
  134.  
  135. $top = ($max === $width) ? ($max - $height) / 2 : 0;
  136. $left = ($max === $width) ? 0 : ($max - $width) / 2;
  137.  
  138. $image = imagecreatetruecolor($max, $max);
  139. $border_color = imagecolorallocate($image, 255, 255, 255);
  140. imagefilledrectangle($image, 0, 0, $max, $max, $border_color);
  141.  
  142. imagecopyresized($image, $this->source, $left, $top, 0, 0, $width, $height, $width, $height);
  143.  
  144. $transColor = imagecolorallocatealpha($image, 255, 255, 255, 127);
  145. $this->source = imagerotate($image, $degrees, $transColor);
  146. }
  147.  
  148. public function save($filename)
  149. {
  150. imagepng($this->source, $filename);
  151. }
  152. }
  153. }
  154.  
  155. namespace {
  156. $input = __DIR__.'/test.png';
  157. $output = $input;
  158.  
  159. tap(new \Finagin\Image($input))
  160. ->rotate(90)
  161. ->save($output);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement