ImageToPdf
in package
Converts images into a PDF with a page for each, every page exactly the size of its image.
One pixel becomes one point, as in the ReportLab script this was ported from. Any format GD can read works (PNG, JPEG, GIF, WebP, BMP and AVIF). Transparency is kept as a soft mask, so transparent areas show the white page; GD stores alpha in 7 bits, so it is kept to within one step of 255.
Images are decoded one at a time, and only their compressed pixels are kept, so many large images can go into one PDF.
Needs only the gd and zlib extensions, which PHP ships with.
Table of Contents
Methods
- convert() : string
- Converts an image file and writes a one-page PDF.
- convertAll() : string
- Converts image files and writes a PDF with a page for each, in the order given.
- fromFile() : string
- Converts an image file into the bytes of a one-page PDF.
- fromFiles() : string
- Converts image files into the bytes of a PDF with a page for each, in the order given.
- fromString() : string
- Converts image bytes into the bytes of a one-page PDF.
- fromStrings() : string
- Converts images' bytes into the bytes of a PDF with a page for each, in the order given.
- document() : string
- Builds a PDF with a page for each image, each image drawn across its whole page.
- fromImages() : string
- Decodes and compresses each image in turn, then builds the PDF.
- image() : string
- An image XObject holding 8-bit samples.
- samples() : array{0: string, 1: ?string}
- Reads an image's pixels, top row first, as 8-bit RGB samples and 8-bit alpha samples, compressed with zlib.
- stream() : string
- A stream object: its dictionary, which this closes after adding the length, and its data.
Methods
convert()
Converts an image file and writes a one-page PDF.
public
static convert(string $imagePath, string $pdfPath) : string
Parameters
- $imagePath : string
-
The image to convert.
- $pdfPath : string
-
Where to write the PDF; an existing file is replaced.
Tags
Return values
string —The PDF's path.
convertAll()
Converts image files and writes a PDF with a page for each, in the order given.
public
static convertAll(array<int, string> $imagePaths, string $pdfPath) : string
Parameters
- $imagePaths : array<int, string>
-
The images to convert, one per page.
- $pdfPath : string
-
Where to write the PDF; an existing file is replaced.
Tags
Return values
string —The PDF's path.
fromFile()
Converts an image file into the bytes of a one-page PDF.
public
static fromFile(string $imagePath) : string
Parameters
- $imagePath : string
-
The image to convert.
Tags
Return values
stringfromFiles()
Converts image files into the bytes of a PDF with a page for each, in the order given.
public
static fromFiles(array<int, string> $imagePaths) : string
Each file is read only when its page is made.
Parameters
- $imagePaths : array<int, string>
-
The images to convert, one per page.
Tags
Return values
stringfromString()
Converts image bytes into the bytes of a one-page PDF.
public
static fromString(string $bytes[, string $name = 'The image' ]) : string
Parameters
- $bytes : string
-
The image file's contents.
- $name : string = 'The image'
-
What to call the image in an error message.
Tags
Return values
stringfromStrings()
Converts images' bytes into the bytes of a PDF with a page for each, in the order given.
public
static fromStrings(array<int, string> $images[, array<int, string> $names = [] ]) : string
Parameters
- $images : array<int, string>
-
Each image file's contents, one per page.
- $names : array<int, string> = []
-
What to call each image in an error message; by default "Image 1", "Image 2" and so on.
Tags
Return values
stringdocument()
Builds a PDF with a page for each image, each image drawn across its whole page.
private
static document(array<int, array{0: int, 1: int, 2: string, 3: ?string}> $pages) : string
Each page takes the next object numbers in turn: the page, its image, its content and, when the image has transparency, its mask. One image therefore gives objects 3 to 5 or 6, as it always has.
Parameters
- $pages : array<int, array{0: int, 1: int, 2: string, 3: ?string}>
-
Each image's width and height in pixels, which are its page's in points, and its RGB and alpha samples compressed with zlib, top row first; alpha is null for an opaque image.
Return values
stringfromImages()
Decodes and compresses each image in turn, then builds the PDF.
private
static fromImages(iterable<string|int, array{0: string, 1: string}> $images) : string
Parameters
- $images : iterable<string|int, array{0: string, 1: string}>
-
Each image's bytes and what to call it in an error message.
Tags
Return values
stringimage()
An image XObject holding 8-bit samples.
private
static image(int $width, int $height, string $colorSpace, string $samples, string $extra) : string
Parameters
- $width : int
- $height : int
- $colorSpace : string
-
DeviceRGBorDeviceGray. - $samples : string
-
The samples, compressed with zlib, which is what
/FlateDecodereads. - $extra : string
-
More dictionary entries, each with a leading space.
Return values
stringsamples()
Reads an image's pixels, top row first, as 8-bit RGB samples and 8-bit alpha samples, compressed with zlib.
private
static samples(GdImage $image) : array{0: string, 1: ?string}
Each row is compressed as it is read, so the uncompressed pixels are never held in memory all at once.
Parameters
- $image : GdImage
Return values
array{0: string, 1: ?string} —The compressed RGB samples, and the compressed alpha samples, or null when every pixel is opaque.
stream()
A stream object: its dictionary, which this closes after adding the length, and its data.
private
static stream(string $dictionary, string $data) : string
Parameters
- $dictionary : string
-
The dictionary, opened but not closed.
- $data : string