- Added basic validation, also upgraded resource to \GdImage since PHP > 8
This commit is contained in:
parent
a3e62c5720
commit
357bdacc08
|
@ -2,15 +2,17 @@
|
||||||
|
|
||||||
namespace Imagine;
|
namespace Imagine;
|
||||||
|
|
||||||
|
use resource;
|
||||||
|
|
||||||
class Canvas {
|
class Canvas {
|
||||||
|
|
||||||
public int $width;
|
public int $width;
|
||||||
|
|
||||||
public int $height;
|
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 = [];
|
public array $layers = [];
|
||||||
|
|
||||||
|
@ -225,6 +227,39 @@ class Canvas {
|
||||||
return $this;
|
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
|
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) ) {
|
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) ) {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace Imagine;
|
namespace Imagine;
|
||||||
|
|
||||||
class Image extends Canvas {
|
class Image extends Canvas {
|
||||||
const DEFAULT_QUALITY = 80;
|
const DEFAULT_QUALITY = 90;
|
||||||
|
|
||||||
protected $filename = "";
|
protected $filename = "";
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ class Image extends Canvas {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fromFile($filename) {
|
public function fromFile($filename) {
|
||||||
|
#dump($filename);
|
||||||
switch ( \exif_imagetype($filename) ) {
|
switch ( \exif_imagetype($filename) ) {
|
||||||
case 1 :
|
case 1 :
|
||||||
$this->forceType('image/gif')->image(\imagecreatefromgif($filename));
|
$this->forceType('image/gif')->image(\imagecreatefromgif($filename));
|
||||||
|
@ -46,8 +47,10 @@ class Image extends Canvas {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $this->valid() ) {
|
||||||
$this->width = \imagesx($this->image);
|
$this->width = \imagesx($this->image);
|
||||||
$this->height = \imagesy($this->image);
|
$this->height = \imagesy($this->image);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -71,8 +74,20 @@ class Image extends Canvas {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadJpeg(string $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);
|
$img = \imagecreatefromjpeg($filename);
|
||||||
|
}
|
||||||
|
catch(\Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$exif = @\exif_read_data($filename);
|
$exif = @\exif_read_data($filename);
|
||||||
|
|
||||||
if ( $img && !empty($exif['Orientation']) ) {
|
if ( $img && !empty($exif['Orientation']) ) {
|
||||||
|
|
Loading…
Reference in New Issue