Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace {
- use Illuminate\Support\HigherOrderTapProxy;
- if (!function_exists('tap')) {
- /**
- * Call the given Closure with the given value then return the value.
- *
- * @param mixed $value
- * @param callable|null $callback
- *
- * @return mixed
- */
- function tap($value, $callback = null)
- {
- if ($callback === null) {
- return new HigherOrderTapProxy($value);
- }
- $callback($value);
- return $value;
- }
- }
- }
- namespace Illuminate\Support {
- class HigherOrderTapProxy
- {
- /**
- * The target being tapped.
- *
- * @var mixed
- */
- public $target;
- /**
- * Create a new tap proxy instance.
- *
- * @param mixed $target
- *
- * @return void
- */
- public function __construct($target)
- {
- $this->target = $target;
- }
- /**
- * Dynamically pass method calls to the target.
- *
- * @param string $method
- * @param array $parameters
- *
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- $this->target->{$method}(...$parameters);
- return $this->target;
- }
- }
- }
- namespace Finagin {
- use RuntimeException;
- class Image
- {
- protected const READERS = [
- 'imagecreatefrombmp',
- 'imagecreatefromgd2',
- 'imagecreatefromgd2part',
- //'imagecreatefromgd',
- 'imagecreatefromgif',
- 'imagecreatefromjpeg',
- 'imagecreatefrompng',
- 'imagecreatefromstring',
- 'imagecreatefromwbmp',
- 'imagecreatefromwebp',
- 'imagecreatefromxbm',
- 'imagecreatefromxpm',
- ];
- /**
- * @var resource|null
- */
- protected $source;
- public function __construct($filename)
- {
- $this->source = static::read($filename);
- }
- protected static function read(string $filename)
- {
- $image = null;
- set_error_handler(function () {
- throw new RuntimeException;
- }, E_ALL);
- foreach (static::READERS as $reader) {
- try {
- $image = $reader($filename);
- break;
- } catch (\Throwable $t) {
- }
- }
- restore_error_handler();
- return $image;
- }
- public static function tap($value, $callback)
- {
- $callback($value);
- return $value;
- }
- public function rotate(float $degrees)
- {
- $width = imagesx($this->source);
- $height = imagesy($this->source);
- $max = max($width, $height);
- $top = ($max === $width) ? ($max - $height) / 2 : 0;
- $left = ($max === $width) ? 0 : ($max - $width) / 2;
- $image = imagecreatetruecolor($max, $max);
- $border_color = imagecolorallocate($image, 255, 255, 255);
- imagefilledrectangle($image, 0, 0, $max, $max, $border_color);
- imagecopyresized($image, $this->source, $left, $top, 0, 0, $width, $height, $width, $height);
- $transColor = imagecolorallocatealpha($image, 255, 255, 255, 127);
- $this->source = imagerotate($image, $degrees, $transColor);
- }
- public function save($filename)
- {
- imagepng($this->source, $filename);
- }
- }
- }
- namespace {
- $input = __DIR__.'/test.png';
- $output = $input;
- tap(new \Finagin\Image($input))
- ->rotate(90)
- ->save($output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement