Small code fixes in API code

This commit is contained in:
2026-06-15 16:04:21 +02:00
parent 7429cd367d
commit 16be5448be
20 changed files with 64 additions and 75 deletions

View File

@@ -2,6 +2,9 @@
namespace api\classes;
use http\Exception\RuntimeException;
class imageProcessor
{
public $postedFile = null;
@@ -34,18 +37,18 @@ class imageProcessor
$mimeType = $matches[1];
$base64 = substr($base64, strpos($base64, ',') + 1);
} else {
throw new Exception('Invalid image data.');
throw new \RuntimeException('Invalid image data.');
}
$imageData = base64_decode($base64);
if ($imageData === false) {
throw new Exception('Invalid base64 image data.');
throw new \RuntimeException('Invalid base64 image data.');
}
# Create image directly from string (no file)
$srcImage = imagecreatefromstring($imageData);
if (!$srcImage) {
throw new Exception('Failed to create image from string.');
throw new \RuntimeException('Failed to create image from string.');
}
# Now you can get dimensions directly
@@ -53,7 +56,6 @@ class imageProcessor
$height = imagesy($srcImage);
# Store $srcImage in a class property, continue processing in-memory
$this->imageResource = $srcImage;
$this->imageInfo = [
'mime' => $mimeType,
'width' => $width,
@@ -74,11 +76,11 @@ class imageProcessor
$fileSizeKB = filesize($this->imageTmpPath) / 1024;
if (!in_array($mime, $this->imageRestrictions['allowed_types'])) {
throw new Exception("Invalid image type: $mime");
throw new \RuntimeException("Invalid image type: $mime");
}
if ($fileSizeKB > $this->imageRestrictions['max_size_kb']) {
throw new Exception("Image exceeds max file size.");
throw new \RuntimeException("Image exceeds max file size.");
}
# Resize to fit within min/max bounds
@@ -137,7 +139,7 @@ class imageProcessor
'image/jpeg' => imagecreatefromjpeg($this->imageTmpPath),
'image/png' => imagecreatefrompng($this->imageTmpPath),
'image/webp' => imagecreatefromwebp($this->imageTmpPath),
default => throw new Exception("Unsupported image type.")
default => throw new \RuntimeException("Unsupported image type.")
};
# Determine new size