ImageHelper
in package
Image utility.
Table of Contents
Methods
- copy() : string
- Copy image.
- dataURL2Blob() : string
- Convert Data URL to blob.
- extractFirstFrameOfGif() : void
- Extract and save the first frame of the animated GIF.
- getNumberOfGifFrames() : int
- Get the number of GIF frames.
- isDataURL() : bool
- Check if it is a Data URL.
- pdf2Image() : void
- Convert PDF to image.
- readAsBlob() : string
- Get the contents of the media file as a blob string.
- readAsDataURL() : string
- Get the contents of the media file as a data URL string.
- resize() : void
- Resize the image.
- writeBlobToFile() : void
- Write blob to a file.
- writeDataURLToFile() : string
- Write data URL to a file.
Methods
copy()
Copy image.
public
static copy(string $src, string $dir[, string|null $filename = null ]) : string
use \X\Util\ImageHelper;
ImageHelper::copy('/tmp/example.png', '/home');// => /tmp/example.png -> /home/example.png
ImageHelper::copy('/tmp/old.png', '/home', 'new');// => /tmp/old.png -> /home/new.png
Parameters
- $src : string
-
Source file path.
- $dir : string
-
Destination directory path.
- $filename : string|null = null
-
(optional) Destination file name. If omitted, the source file name in $src becomes the destination file name.
Return values
string —Output file name.
dataURL2Blob()
Convert Data URL to blob.
public
static dataURL2Blob(string $dataURL[, string &$mime = null ]) : string
Parameters
- $dataURL : string
-
String.
- $mime : string = null
-
(optional) If specified, the MIME type detected from the Data URL is set.
Return values
string —Blob.
extractFirstFrameOfGif()
Extract and save the first frame of the animated GIF.
public
static extractFirstFrameOfGif(string $src[, string|null $dest = null ]) : void
use \X\Util\ImageHelper;
// Write the first frame of sample.gif to sample_0.gif.
ImageHelper::extractFirstFrameOfGif('sample.gif', 'sample_0.gif');
// Overwrite sample.gif with the first frame.
ImageHelper::extractFirstFrameOfGif('sample.gif');
Parameters
- $src : string
-
Input file path.
- $dest : string|null = null
-
(optional) Output file path. If omitted, the input file is overwritten.
getNumberOfGifFrames()
Get the number of GIF frames.
public
static getNumberOfGifFrames(string $filePath) : int
Parameters
- $filePath : string
-
Input file path.
Return values
int —Number of GIF frames.
isDataURL()
Check if it is a Data URL.
public
static isDataURL(string $dataURL[, string &$mime = null ]) : bool
Parameters
- $dataURL : string
-
String.
- $mime : string = null
-
(optional) If specified, the MIME type detected from the Data URL is set.
Return values
bool —Data URL or not.
pdf2Image()
Convert PDF to image.
public
static pdf2Image(string $src, string $dest[, array<string|int, mixed> $options = [] ]) : void
Parameters
- $src : string
-
Input file path.
- $dest : string
-
Output file path.
- $options : array<string|int, mixed> = []
readAsBlob()
Get the contents of the media file as a blob string.
public
static readAsBlob(string $filePath) : string
Parameters
- $filePath : string
-
File path.
Return values
string —Blob.
readAsDataURL()
Get the contents of the media file as a data URL string.
public
static readAsDataURL(string $filePath) : string
Parameters
- $filePath : string
-
File path.
Return values
string —Data URL.
resize()
Resize the image.
public
static resize(string $src, string $dest[, int|null $width = null ][, int|null $height = null ][, bool $keepAspectRatio = true ]) : void
Parameters
- $src : string
-
Input file path.
- $dest : string
-
Output file path.
- $width : int|null = null
-
(optional) Width (pixels) after resizing. If omitted, adjust to height.
- $height : int|null = null
-
(optional) Height after resizing (pixels). If omitted, matches the width.
- $keepAspectRatio : bool = true
-
(optional) Whether the aspect ratio of the original image is maintained after resizing. Default is true.
writeBlobToFile()
Write blob to a file.
public
static writeBlobToFile(string $blob, string $filePath) : void
Parameters
- $blob : string
-
Image Blob.
- $filePath : string
-
Output file path.
writeDataURLToFile()
Write data URL to a file.
public
static writeDataURLToFile(string $dataURL, string $dir[, string|null $filename = null ]) : string
use \X\Util\ImageHelper;
ImageHelper::writeDataURLToFile('data:image/png;base64,iVBOR...', '/tmp', 'sample');
ImageHelper::writeDataURLToFile('data:image/png;base64,iVBOR...', '/tmp/sample.png');
Parameters
- $dataURL : string
-
Image Data URL.
- $dir : string
-
Destination directory or file path. For file paths, the $filename parameter is not required.
- $filename : string|null = null
-
(optional) File Name. If the file name does not have an extension, the guessed extension is automatically assigned.
Return values
string —Name of the output file.