ImageService
in package
Image service for handling photo uploads, resizing, and thumbnail generation.
Uses GD library for image processing. Stores files in organized directory structure under public/uploads/.
Tags
Table of Contents
Constants
- ALLOWED_TYPES = ['image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp']
- MAX_DIMENSION = 1200
- MAX_FILE_SIZE = 5 * 1024 * 1024
- THUMB_HEIGHT = 300
- THUMB_WIDTH = 300
Methods
- createThumbnail() : void
- Create a center-cropped thumbnail.
- deleteFiles() : void
- Delete a photo and its thumbnail from disk.
- resize() : array<string|int, mixed>
- Resize an image to fit within max dimensions while maintaining aspect ratio.
- upload() : array<string|int, mixed>
- Upload, validate, resize, and create thumbnail for a photo.
- validate() : void
- Validate an uploaded file.
- createImageFromFile() : GdImage
- Create a GD image resource from a file path.
- generateFileName() : string
- Generate a unique file name.
- saveImage() : void
- Save a GD image resource to a file.
Constants
ALLOWED_TYPES
private
array<string|int, mixed>
ALLOWED_TYPES
= ['image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp']
Allowed MIME types
MAX_DIMENSION
private
int
MAX_DIMENSION
= 1200
Maximum image dimension for full-size images
MAX_FILE_SIZE
private
int
MAX_FILE_SIZE
= 5 * 1024 * 1024
Maximum upload file size in bytes (5MB)
THUMB_HEIGHT
private
int
THUMB_HEIGHT
= 300
Thumbnail height
THUMB_WIDTH
private
int
THUMB_WIDTH
= 300
Thumbnail width
Methods
createThumbnail()
Create a center-cropped thumbnail.
public
createThumbnail(string $sourcePath, string $thumbPath) : void
Parameters
- $sourcePath : string
-
Source image path
- $thumbPath : string
-
Destination thumbnail path
deleteFiles()
Delete a photo and its thumbnail from disk.
public
deleteFiles(string $filePath[, string|null $thumbnailPath = null ]) : void
Parameters
- $filePath : string
-
Relative path to full image
- $thumbnailPath : string|null = null
-
Relative path to thumbnail
resize()
Resize an image to fit within max dimensions while maintaining aspect ratio.
public
resize(string $filePath, int $maxDimension) : array<string|int, mixed>
Parameters
- $filePath : string
-
Path to the image file
- $maxDimension : int
-
Maximum width or height
Return values
array<string|int, mixed> —Width and height after resize
upload()
Upload, validate, resize, and create thumbnail for a photo.
public
upload(array<string|int, mixed> $file, int $businessId[, string $photoType = 'menu_item' ]) : array<string|int, mixed>
Parameters
- $file : array<string|int, mixed>
-
The $_FILES entry for the upload
- $businessId : int
-
Business ID for directory organization
- $photoType : string = 'menu_item'
-
Type of photo (menu_item, gallery, etc.)
Tags
Return values
array<string|int, mixed> —Associative array with file metadata
validate()
Validate an uploaded file.
public
validate(array<string|int, mixed> $file) : void
Parameters
- $file : array<string|int, mixed>
-
The $_FILES entry
Tags
createImageFromFile()
Create a GD image resource from a file path.
private
createImageFromFile(string $filePath, string $mime) : GdImage
Parameters
- $filePath : string
-
File path
- $mime : string
-
MIME type
Tags
Return values
GdImagegenerateFileName()
Generate a unique file name.
private
generateFileName(string $extension) : string
Parameters
- $extension : string
-
File extension
Return values
stringsaveImage()
Save a GD image resource to a file.
private
saveImage(GdImage $image, string $filePath, string $mime) : void
Parameters
- $image : GdImage
-
GD image resource
- $filePath : string
-
Destination file path
- $mime : string
-
MIME type