ImageToPdfConversionService
in package
Converts uploaded image files to PDF format for consistent document storage.
Uses GD extension for image processing. Supports JPEG, PNG, GIF, BMP, WEBP formats. Maintains aspect ratio and fits images to standard page sizes (Letter, A4).
Tags
Table of Contents
Constants
- SUPPORTED_FORMATS = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'wbmp']
- Supported image formats
Methods
- convertImageToPdf() : ServiceResult
- Convert an image file to PDF format
- convertMixedToPdf() : ServiceResult
- Process mixed uploads (images and PDFs) into a single PDF document.
- convertMultipleImagesToPdf() : ServiceResult
- Convert multiple image files into a single multi-page PDF
- isPdf() : bool
- Check if a file is a PDF
- isSupportedImage() : bool
- Check if a file is a supported image format
- buildMultiPagePdfStructure() : string
- Build multi-page PDF structure
- buildPdfStructure() : string
- Build minimal PDF structure with embedded JPEG
- calculateFitDimensions() : array<string|int, mixed>
- Calculate dimensions to fit image on page while maintaining aspect ratio
- createPreviewFromJpegData() : string|null
- Persist first-page JPEG data into a temporary preview file.
- createSimplePdf() : ServiceResult
- Create a simple PDF with embedded JPEG
- determinePageOrientation() : string
- Get page dimensions based on size name
- generateThumbnailFromImage() : string|null
- Generate a thumbnail from an image file
- getPageDimensions() : array<string|int, mixed>
- Get page dimensions.
- isImageMimeType() : bool
- Check if a MIME type is a supported image type
- loadImage() : GdImage|false
- Load an image from file based on its type
- processImageForPdf() : array<string|int, mixed>
- Process image for PDF (resize and convert to grayscale)
- validateAndGetImageInfo() : array{success: bool, width?: int, height?: int, type?: int, error?: string}
- Validate and get information about an image file
Constants
SUPPORTED_FORMATS
Supported image formats
private
mixed
SUPPORTED_FORMATS
= ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'wbmp']
Note: WEBP support depends on GD library compilation options
Methods
convertImageToPdf()
Convert an image file to PDF format
public
convertImageToPdf(string $imagePath, string $outputPath[, string $pageSize = 'letter' ][, string|null &$previewPath = null ]) : ServiceResult
Parameters
- $imagePath : string
-
Full path to the source image file
- $outputPath : string
-
Full path where the PDF should be saved
- $pageSize : string = 'letter'
-
Page size: 'letter' or 'a4' (default: letter)
- $previewPath : string|null = null
-
Reference that will receive the path to a generated JPEG preview
Return values
ServiceResult —Success/failure with optional error message
convertMixedToPdf()
Process mixed uploads (images and PDFs) into a single PDF document.
public
convertMixedToPdf(array<string|int, mixed> $fileInfos, string $outputPath[, string $pageSize = 'letter' ][, string|null &$previewPath = null ]) : ServiceResult
Images are converted to PDF pages, then all PDFs are merged together. Files are processed in the order provided.
Parameters
- $fileInfos : array<string|int, mixed>
-
Array of file info arrays with keys: 'path', 'original_name', 'mime_type'
- $outputPath : string
-
Full path where the merged PDF should be saved
- $pageSize : string = 'letter'
-
Page size for image conversion: 'letter' or 'a4'
- $previewPath : string|null = null
-
Reference that will receive the path to a preview image
Return values
ServiceResult —Success with page count, or failure with error
convertMultipleImagesToPdf()
Convert multiple image files into a single multi-page PDF
public
convertMultipleImagesToPdf(array<string|int, mixed> $imagePaths, string $outputPath[, string $pageSize = 'letter' ][, string|null &$previewPath = null ]) : ServiceResult
Parameters
- $imagePaths : array<string|int, mixed>
-
Array of full paths to source image files
- $outputPath : string
-
Full path where the PDF should be saved
- $pageSize : string = 'letter'
-
Page size: 'letter' or 'a4' (default: letter)
- $previewPath : string|null = null
-
Reference that will receive the path to a generated JPEG preview for the first page
Return values
ServiceResult —Success/failure with optional error message
isPdf()
Check if a file is a PDF
public
isPdf(string $filePath) : bool
Parameters
- $filePath : string
-
Path to the file
Return values
bool —True if file is a PDF
isSupportedImage()
Check if a file is a supported image format
public
isSupportedImage(string $filePath) : bool
Parameters
- $filePath : string
-
Path to the file
Return values
bool —True if file is a supported image
buildMultiPagePdfStructure()
Build multi-page PDF structure
private
buildMultiPagePdfStructure(array<string|int, mixed> $jpegDataArray) : string
Parameters
- $jpegDataArray : array<string|int, mixed>
-
Array of page data (with per-page width/height)
Return values
string —PDF content
buildPdfStructure()
Build minimal PDF structure with embedded JPEG
private
buildPdfStructure(string $jpegData, int $jpegSize, int $jpegWidth, int $jpegHeight, int $displayWidth, int $displayHeight, int $pageWidth, int $pageHeight) : string
Parameters
- $jpegData : string
-
JPEG binary data
- $jpegSize : int
-
JPEG file size
- $jpegWidth : int
-
Actual JPEG pixel width (for XObject)
- $jpegHeight : int
-
Actual JPEG pixel height (for XObject)
- $displayWidth : int
-
Display width in points (for transformation matrix)
- $displayHeight : int
-
Display height in points (for transformation matrix)
- $pageWidth : int
-
Page width
- $pageHeight : int
-
Page height
Return values
string —PDF content
calculateFitDimensions()
Calculate dimensions to fit image on page while maintaining aspect ratio
private
calculateFitDimensions(int $imgWidth, int $imgHeight, int $pageWidth, int $pageHeight) : array<string|int, mixed>
Parameters
- $imgWidth : int
-
Image width
- $imgHeight : int
-
Image height
- $pageWidth : int
-
Page width
- $pageHeight : int
-
Page height
Return values
array<string|int, mixed> —[fitted width, fitted height]
createPreviewFromJpegData()
Persist first-page JPEG data into a temporary preview file.
private
createPreviewFromJpegData(string|null $jpegData) : string|null
Parameters
- $jpegData : string|null
-
Binary JPEG data or null when unavailable
Return values
string|null —Path to temporary preview file (caller responsible for cleanup)
createSimplePdf()
Create a simple PDF with embedded JPEG
private
createSimplePdf(GdImage $image, int $width, int $height, string $outputPath, int $pageWidth, int $pageHeight[, string|null &$previewPath = null ]) : ServiceResult
Creates a basic PDF structure with the image embedded as JPEG. This is a minimal implementation that works without external libraries.
Parameters
- $image : GdImage
-
GD image resource
- $width : int
-
Image width
- $height : int
-
Image height
- $outputPath : string
-
Output PDF path
- $pageWidth : int
-
Page width in points
- $pageHeight : int
-
Page height in points
- $previewPath : string|null = null
-
Output parameter that receives a temporary JPEG path
Return values
ServiceResultdeterminePageOrientation()
Get page dimensions based on size name
private
determinePageOrientation(int $width, int $height) : string
Parameters
- $width : int
- $height : int
Return values
string —"portrait" or "landscape" based on image aspect ratio
generateThumbnailFromImage()
Generate a thumbnail from an image file
private
generateThumbnailFromImage(string $imagePath[, int $maxWidth = 200 ][, int $maxHeight = 260 ]) : string|null
Parameters
- $imagePath : string
-
Path to the source image
- $maxWidth : int = 200
-
Maximum thumbnail width
- $maxHeight : int = 260
-
Maximum thumbnail height
Return values
string|null —Path to the generated thumbnail, or null on failure
getPageDimensions()
Get page dimensions.
private
getPageDimensions(string $pageSize[, string $orientation = 'portrait' ]) : array<string|int, mixed>
Parameters
- $pageSize : string
- $orientation : string = 'portrait'
Return values
array<string|int, mixed>isImageMimeType()
Check if a MIME type is a supported image type
private
isImageMimeType(string $mimeType) : bool
Parameters
- $mimeType : string
-
The MIME type to check
Return values
bool —True if MIME type is a supported image
loadImage()
Load an image from file based on its type
private
loadImage(string $path, int $type) : GdImage|false
Parameters
- $path : string
-
Path to image
- $type : int
-
Image type constant
Return values
GdImage|false —Image resource or false on failure
processImageForPdf()
Process image for PDF (resize and convert to grayscale)
private
processImageForPdf(GdImage $image, int $width, int $height, int $pageWidth, int $pageHeight) : array<string|int, mixed>
Parameters
- $image : GdImage
-
GD image resource
- $width : int
-
Original width
- $height : int
-
Original height
- $pageWidth : int
-
Page width in points
- $pageHeight : int
-
Page height in points
Return values
array<string|int, mixed> —Result with jpeg_data, jpeg_size, width, height
validateAndGetImageInfo()
Validate and get information about an image file
private
validateAndGetImageInfo(string $imagePath[, bool $throwException = false ]) : array{success: bool, width?: int, height?: int, type?: int, error?: string}
Performs comprehensive validation including:
- File existence check
- Image format detection
- Format support verification
- Debug file saving on failure
Parameters
- $imagePath : string
-
Path to image file
- $throwException : bool = false
-
If true, throws exception on failure; if false, returns error in array