From d2f7c79cf6deed8da403ffe96a1319f6f2b9152c Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Sat, 5 Jun 2021 21:07:31 +0200 Subject: [PATCH] Update Simple Image Class to 3.6.3 --- bl-kernel/helpers/simple-image.class.php | 43 +++++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/bl-kernel/helpers/simple-image.class.php b/bl-kernel/helpers/simple-image.class.php index 347c5e51..bfc89455 100644 --- a/bl-kernel/helpers/simple-image.class.php +++ b/bl-kernel/helpers/simple-image.class.php @@ -1,6 +1,6 @@ image !== null && is_resource($this->image) && get_resource_type($this->image) === 'gd') { + //Check for a valid GDimage instance + $type_check = (gettype($this->image) == "object" && get_class($this->image) == "GdImage"); + + if($this->image !== null && is_resource($this->image) && $type_check) { imagedestroy($this->image); } } @@ -128,7 +131,7 @@ class SimpleImage { fclose($handle); // Get image info - $info = getimagesize($file); + $info = @getimagesize($file); if($info === false) { throw new \Exception("Invalid image file: $file", self::ERR_INVALID_IMAGE); } @@ -587,13 +590,27 @@ class SimpleImage { $y1 = self::keepWithin($y1, 0, $this->getHeight()); $y2 = self::keepWithin($y2, 0, $this->getHeight()); + // Avoid using native imagecrop() because of a bug with PNG transparency + $dstW = abs($x2 - $x1); + $dstH = abs($y2 - $y1); + $newImage = imagecreatetruecolor($dstW, $dstH); + $transparentColor = imagecolorallocatealpha($newImage, 0, 0, 0, 127); + imagecolortransparent($newImage, $transparentColor); + imagefill($newImage, 0, 0, $transparentColor); + // Crop it - $this->image = imagecrop($this->image, [ - 'x' => min($x1, $x2), - 'y' => min($y1, $y2), - 'width' => abs($x2 - $x1), - 'height' => abs($y2 - $y1) - ]); + imagecopyresampled( + $newImage, + $this->image, + 0, 0, min($x1, $x2), min($y1, $y2), + $dstW, + $dstH, + $dstW, + $dstH + ); + + // Swap out the new image + $this->image = $newImage; return $this; } @@ -1361,7 +1378,7 @@ class SimpleImage { imagefilledarc($tempImage->image, $x, $y, $width+$thickness, $height+$thickness, $start, $end, $tempColor, IMG_ARC_PIE); // Draw a smaller ellipse filled with red|blue (-$thickness pixels) - $tempColor = ($color == 'red') ? 'blue' : 'red'; + $tempColor = (self::normalizeColor($color)['red'] == 255) ? 'blue' : 'red'; $tempColor = $tempImage->allocateColor($tempColor); imagefilledarc($tempImage->image, $x, $y, $width-$thickness, $height-$thickness, $start, $end, $tempColor, IMG_ARC_PIE); @@ -1443,7 +1460,7 @@ class SimpleImage { imagefilledellipse($tempImage->image, $x, $y, $width+$thickness, $height+$thickness, $tempColor); // Draw a smaller ellipse filled with red|blue (-$thickness pixels) - $tempColor = ($color == 'red') ? 'blue' : 'red'; + $tempColor = (self::normalizeColor($color)['red'] == 255) ? 'blue' : 'red'; $tempColor = $tempImage->allocateColor($tempColor); imagefilledellipse($tempImage->image, $x, $y, $width-$thickness, $height-$thickness, $tempColor); @@ -1603,7 +1620,7 @@ class SimpleImage { $tempImage->roundedRectangle($x1, $y1, $x2, $y2, $radius, $color,'filled'); // Draw a smaller rectangle filled with red|blue (-$thickness pixels on each side) - $tempColor = ($color == 'red') ? 'blue' : 'red'; + $tempColor = (self::normalizeColor($color)['red'] == 255) ? 'blue' : 'red'; $radius = $radius - $thickness; $radius = self::keepWithin($radius, 0, $radius); $tempImage->roundedRectangle( @@ -2131,4 +2148,4 @@ class SimpleImage { throw new \Exception("Invalid color value: $color", self::ERR_INVALID_COLOR); } -} \ No newline at end of file +}