From 357bdacc08fa8231e7371b561e681b2e55c2382d Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 29 Nov 2022 19:49:58 +0000 Subject: [PATCH] - Added basic validation, also upgraded resource to \GdImage since PHP > 8 --- src/Canvas.php | 39 +++++++++++++++++++++++++++++++++++++-- src/Image.php | 25 ++++++++++++++++++++----- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Canvas.php b/src/Canvas.php index 28284c8..f72b30e 100644 --- a/src/Canvas.php +++ b/src/Canvas.php @@ -2,15 +2,17 @@ namespace Imagine; +use resource; + class Canvas { public int $width; public int $height; - public /*\resource*/ $image; /* actual img resource */ + public /*\resource|GdImage*/ $image; /* actual img resource */ - public /*\resource*/ $parent; + public Canvas $parent; public array $layers = []; @@ -225,6 +227,39 @@ class Canvas { return $this; } + public function crop(int $width, int $height, bool $centered = false, bool $immutable = true) : self + { + $x = $y = 0; + + if ($centered) { + if ($this->width >= $this->height) { + $r = $this->height / $height; + $x = ($this->width / 2) - ($width * $r / 2); + } + else { + $r = $this->width / $width; + $y = ($this->height / 2) - ($height * $r / 2); + } + } + + $new = ( new static() )->create($width, $height); + + $this->copyTo($new, 0, 0, $x, $y, 0, 0, $this->width - ($x * 2), $this->height - ($y * 2)); + + if ( ! $immutable ) { + $this->grabImageResource($new); + + $this->width = $width; + $this->height = $height; + } + else { + return $new; + } + + + return $this; + } + public function copyTo(Canvas $dest, int $dst_x = 0, int $dst_y = 0, int $src_x = 0, int $src_y = 0, int $dst_w = 0, int $dst_h = 0, int $src_w = 0, int $src_h = 0) : self { if ( ! \imagecopyresampled($dest->image(), $this->image(), $dst_x, $dst_y, $src_x, $src_y, $dst_w ?: $dest->width, $dst_h ?: $dest->height, $src_w ?: $this->width, $src_h ?: $this->height) ) { diff --git a/src/Image.php b/src/Image.php index a9ec0b3..3fe4e8e 100644 --- a/src/Image.php +++ b/src/Image.php @@ -3,7 +3,7 @@ namespace Imagine; class Image extends Canvas { - const DEFAULT_QUALITY = 80; + const DEFAULT_QUALITY = 90; protected $filename = ""; @@ -28,6 +28,7 @@ class Image extends Canvas { } public function fromFile($filename) { + #dump($filename); switch ( \exif_imagetype($filename) ) { case 1 : $this->forceType('image/gif')->image(\imagecreatefromgif($filename)); @@ -46,8 +47,10 @@ class Image extends Canvas { break; } - $this->width = \imagesx($this->image); - $this->height = \imagesy($this->image); + if ( $this->valid() ) { + $this->width = \imagesx($this->image); + $this->height = \imagesy($this->image); + } return $this; } @@ -71,8 +74,20 @@ class Image extends Canvas { return $this; } - public function loadJpeg(string $filename) { - $img = \imagecreatefromjpeg($filename); + public function valid() : bool + { + return is_resource($this->image ?? null) || $this->image instanceof \GdImage; + } + + public function loadJpeg(string $filename) /* : false|GdImage */ + { + try { + $img = \imagecreatefromjpeg($filename); + } + catch(\Exception $e) { + return false; + } + $exif = @\exif_read_data($filename); if ( $img && !empty($exif['Orientation']) ) {