Skip to content

Commit

Permalink
fixes broken verifyFormatCompatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
vendeeglobe committed Jun 7, 2022
1 parent 751a0b2 commit 6bda8cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions src/lib/phpthumb/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public function __construct($fileName, array $options = [], array $plugins = [])
case 'GIF':
$this->oldImage = imagecreatefromgif($this->fileName);
break;
case 'JFIF':
case 'JPG':
case 'JPEG':
$this->oldImage = imagecreatefromjpeg($this->fileName);
break;
case 'PNG':
Expand Down Expand Up @@ -551,7 +550,8 @@ public function adaptiveResizeQuadrant(int $width, int $height, string $quadrant
'R' => intval(($this->currentDimensions['width'] - $this->maxWidth)),
default => intval(($this->currentDimensions['width'] - $this->maxWidth) / 2),
};
} else if ($this->currentDimensions['height'] > $this->maxHeight)
}
else if ($this->currentDimensions['height'] > $this->maxHeight)
{
// Image is portrait
$cropY = match ($quadrant) {
Expand Down Expand Up @@ -836,7 +836,7 @@ public function show(bool $rawData = false): GD
}
imagegif($this->oldImage);
break;
case 'JPG':
case 'JPEG':
if ($rawData === false)
{
header('Content-type: image/jpeg');
Expand Down Expand Up @@ -891,13 +891,13 @@ public function getImageAsString(): string
* \RuntimeException is thrown.
*
* @param string $fileName The full path and filename of the image to save
* @param string|null $format The format to save the image in (optional, must be one of [GIF,JPG,PNG]
* @param string|null $format The format to save the image in (optional, must be one of [AVIF, GIF, JPEG, JPG, PNG, WEBP]
* @return GD
*/
public function save(string $fileName, string $format = null): GD
{
$validFormats = ['AVIF', 'GIF', 'JPG', 'PNG', 'WEBP'];
$format = ($format !== null) ? strtoupper($format) : $this->format;
$validFormats = ['AVIF', 'GIF', 'JPEG', 'JPG', 'PNG', 'WEBP'];
$format = ($format !== null) ? strtoupper($format) : $this->format;

if (!in_array($format, $validFormats))
{
Expand Down Expand Up @@ -941,6 +941,7 @@ public function save(string $fileName, string $format = null): GD
case 'GIF':
imagegif($this->oldImage, $fileName);
break;
case 'JPEG':
case 'JPG':
imagejpeg($this->oldImage, $fileName, $this->options['jpegQuality']);
break;
Expand Down Expand Up @@ -1354,7 +1355,7 @@ protected function determineFormat()
$this->format = match ($mimeType) {
'image/avif' => 'AVIF',
'image/gif' => 'GIF',
'image/jpeg' => 'JPG',
'image/jpeg' => 'JPEG',
'image/png' => 'PNG',
'image/webp' => 'WEBP',
default => throw new \Exception('Image format not supported: ' . $mimeType),
Expand All @@ -1373,16 +1374,18 @@ protected function verifyFormatCompatiblity()
$isCompatible = match ($this->format) {
'AVIF' => $gdInfo[$this->format . ' Support'],
'GIF' => $gdInfo['GIF Create Support'],
'JPG' => isset($gdInfo['JPG Support']) || isset($gdInfo['JPEG Support']),
'JPEG' => isset($gdInfo['JPG Support']) || isset($gdInfo['JPEG Support']),
'PNG' => $gdInfo[$this->format . ' Support'],
'WEBP' => $gdInfo['WebP Support'],
default => false,
};

$suffix = strtolower($this->format);
$compiled = function_exists('image' . $suffix) && function_exists('imagecreatefrom' . $suffix);
$suffix = strtolower($this->format);

$isCompatible = $isCompatible & $compiled;
$isCompatible =
function_exists('image' . $suffix)
&& function_exists('imagecreatefrom' . $suffix)
&& $isCompatible;

if (!$isCompatible)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/phpthumb/Plugins/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ protected function hex2rgb ($hex, $asString = false)
{
$hex = substr($hex, 1);
}
elseif (str_starts_with($hex, '&H'))
else if (str_starts_with($hex, '&H'))
{
$hex = substr($hex, 2);
}
Expand Down

0 comments on commit 6bda8cf

Please sign in to comment.