Created
April 15, 2019 11:44
-
-
Save finagin/cb7382e92ec8b290e33112163d1f63e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment