ImageOptimizer
extends Imagick
in package
ImageOptimizer - An Imagick extension to provide better (higher quality, lower file size) image resizes.
This class extends Imagick (http://php.net/manual/en/book.imagick.php) based on research into optimal image resizing techniques (https://github.com/nwtn/image-resize-tests).
Using these methods with their default settings should provide image resizing that is visually indistinguishable from Photoshop’s “Save for Web…”, but at lower file sizes.
Table of Contents
- alternateResize() : mixed
- AlternateResize method.
- generateWebpFile() : false|string
- Generate Webp image.
- isImageOptimEnabled() : bool
- Checks if the imageoptim program is available on the $PATH.
- isImageUnderscoreOptimEnabled() : bool
- Checks if the image_optim program is available on the $PATH.
- isPicOptEnabled() : bool
- Checks if the picopt program is available on the $PATH.
- isSvgoEnabled() : bool
- Checks if the svgo program is available on the $PATH.
- optimalImage() : bool
- Changes the size of an image to the given dimensions and removes any associated profiles.
- optimize() : string
- Optimizes the image without reducing quality.
- smartResize() : void
- Resizes the image using smart defaults for high quality and low file size.
- smartResizeFile() : bool|resource
- Easy image resize function.
- smartResizeImage() : bool|resource
- Easy image resize function.
- calculateDestinationDimensions() : array<string|int, mixed>
- Calculates the destination dimensions.
- isExternalProgramEnabled() : bool
- Checks if the external program is available on the $PATH.
- replaceExtension() : string
- Replace file extension.
- svgoOptimize() : bool
- Optimize an image using the svgo program.
Methods
alternateResize()
AlternateResize method.
public
alternateResize(string $file, int $width, int $height, string $destination) : mixed
Variant method from forked repository avonis/respimg. Untested! Unverified! Requires Gumlet\ImageResize.
Parameters
- $file : string
-
The file to process.
- $width : int
-
The width to resize to.
- $height : int
-
The height to resize to.
- $destination : string
-
The file path to save to.
Tags
Return values
mixed —generateWebpFile()
Generate Webp image.
public
static generateWebpFile(string $file[, int $compressionQuality = 80 ]) : false|string
Uses either Imagick or GD imagewebp to generate webp image. Originally copied from https://www.jclabs.co.uk /generate-webp-images-in-php-using-and-gd-or-imagick/.
Parameters
- $file : string
-
Path to image being converted.
- $compressionQuality : int = 80
-
Quality ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file).
Return values
false|string —Returns path to generated webp image, otherwise returns false.
isImageOptimEnabled()
Checks if the imageoptim program is available on the $PATH.
public
static isImageOptimEnabled() : bool
Return values
bool —Indicates whether the imageoptim program is available on the $PATH.
isImageUnderscoreOptimEnabled()
Checks if the image_optim program is available on the $PATH.
public
static isImageUnderscoreOptimEnabled() : bool
Return values
bool —Indicates whether the image_optim program is available on the $PATH.
isPicOptEnabled()
Checks if the picopt program is available on the $PATH.
public
static isPicOptEnabled() : bool
Return values
bool —Indicates whether the picopt program is available on the $PATH.
isSvgoEnabled()
Checks if the svgo program is available on the $PATH.
public
static isSvgoEnabled() : bool
Return values
bool —Indicates whether the svgo program is available on the $PATH.
optimalImage()
Changes the size of an image to the given dimensions and removes any associated profiles.
public
optimalImage(int $columns, int $rows[, bool $bestfit = false ][, bool $fill = false ][, int $filter = Imagick::FILTER_TRIANGLE ]) : bool
optimalImage was originally named thumbnailImage. But due to PHP's
stricter checking, along the modified default values would result in the
PHP warning of 'Declaration of should be compatible with
Imagick::thumbnailimage.
optimalImage changes the size of an image to the given dimensions and
removes any associated profiles. The goal is to produce small low cost
thumbnail images suited for display on the Web.
With the original Imagick optimalImage implementation, there is no way to choose a resampling filter. This class recreates Imagick’s C implementation and adds this additional feature.
Note: https://github.com/mkoppanen/imagick/issues/90 has been filed for this issue.
Parameters
- $columns : int
-
The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
- $rows : int
-
The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
- $bestfit : bool = false
-
Treat $columns and $rows as a bounding box in which to fit the image.
- $fill : bool = false
-
Fill in the bounding box with the background colour.
- $filter : int = Imagick::FILTER_TRIANGLE
-
The resampling filter to use. Refer to the list of filter constants at http://php.net/manual/en/imagick.constants.php.
Return values
bool —Indicates whether the operation was performed successfully.
optimize()
Optimizes the image without reducing quality.
public
static optimize(string $path, int $svgo, int $imageOptimizer1, int $picopt, int $imageOptim) : string
This function calls up to four external programs, which must be installed and available in the $PATH:
- SVGO
- image_optim
- picopt
- ImageOptim
Note that these are executed using PHP’s exec command, so there may be
security implications.
Parameters
- $path : string
-
The path to the file or directory that should be optimized.
- $svgo : int
-
The number of times to optimize using SVGO.
- $imageOptimizer1 : int
-
The number of times to optimize using image_optim.
- $picopt : int
-
The number of times to optimize using picopt.
- $imageOptim : int
-
The number of times to optimize using ImageOptim.
Return values
string —$output
smartResize()
Resizes the image using smart defaults for high quality and low file size.
public
smartResize(int $columns, int $rows[, bool $optim = false ][, int $filter = Imagick::FILTER_TRIANGLE ][, bool $bestfit = false ][, bool $crop = false ]) : void
This function is basically equivalent to:
$optim == true: mogrify -path OUTPUT_PATH -filter Triangle \ -define filter:support=2.0 -thumbnail OUTPUT_WIDTH \ -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 \ -define jpeg:fancy-upsampling=off -define png:compression-filter=5 \ -define png:compression-level=9 -define png:compression-strategy=1 \ -define png:exclude-chunk=all -interlace none \ -colorspace sRGB INPUT_PATH
$optim == false: mogrify -path OUTPUT_PATH -filter Triangle \ -define filter:support=2.0 -thumbnail OUTPUT_WIDTH \ -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 \ -define jpeg:fancy-upsampling=off -define png:compression-filter=5 \ -define png:compression-level=9 -define png:compression-strategy=1 \ -define png:exclude-chunk=all -interlace none -colorspace sRGB \ -strip INPUT_PATH
Parameters
- $columns : int
-
The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
- $rows : int
-
The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
- $optim : bool = false
-
Whether you intend to perform optimization on the resulting image. Note that setting this to
truedoesn’t actually perform any optimization. - $filter : int = Imagick::FILTER_TRIANGLE
-
The filter to use when generating thumbnail image.
- $bestfit : bool = false
-
Treat $columns and $rows as a bounding box in which to fit the image.
- $crop : bool = false
-
Whether you want to crop the image.
Return values
void —smartResizeFile()
Easy image resize function.
public
static smartResizeFile(string $file[, string $output = 'file' ], int $width, int $height[, bool $proportional = false ][, int $quality = 100 ][, bool $grayscale = false ][, bool $deleteOriginal = true ]) : bool|resource
Uses PHP GD library functions.
Parameters
- $file : string
-
File name to resize.
- $output : string = 'file'
-
Name of the new file. Iinclude path if needed.
- $width : int
-
New image width.
- $height : int
-
New image height.
- $proportional : bool = false
-
Keep image proportional, default is false.
- $quality : int = 100
-
Enter 1-100. 100 is best quality. Default is 100.
- $grayscale : bool = false
-
If true, image will be grayscale. Default is false.
- $deleteOriginal : bool = true
-
If true the original file will be deleted.
Return values
bool|resource —smartResizeImage()
Easy image resize function.
public
static smartResizeImage([string $string = null ][, string $output = 'file' ], int $width, int $height[, bool $proportional = false ][, int $quality = 100 ][, bool $grayscale = false ][, bool $deleteOriginal = true ]) : bool|resource
Uses PHP GD library functions
Parameters
- $string : string = null
-
The image data, as a string.
- $output : string = 'file'
-
Name of the new file. Iinclude path if needed.
- $width : int
-
New image width.
- $height : int
-
New image height.
- $proportional : bool = false
-
Keep image proportional, default is false.
- $quality : int = 100
-
Enter 1-100. 100 is best quality. Default is 100.
- $grayscale : bool = false
-
If true, image will be grayscale. Default is false.
- $deleteOriginal : bool = true
-
If true the original file will be deleted.
Return values
bool|resource —calculateDestinationDimensions()
Calculates the destination dimensions.
private
static calculateDestinationDimensions(int $width, int $height, int $sourceWidth, int $sourceHeight) : array<string|int, mixed>
Parameters
- $width : int
-
The width to calcuate to.
- $height : int
-
The height to calcuate to.
- $sourceWidth : int
-
The width of the source image.
- $sourceHeight : int
-
The height of the source image.
Return values
array<string|int, mixed> —An array of destination width and height.
isExternalProgramEnabled()
Checks if the external program is available on the $PATH.
private
static isExternalProgramEnabled(string $program) : bool
Parameters
- $program : string
-
The name of the program to check for.
Return values
bool —Indicates whether the external program is available on the $PATH.
replaceExtension()
Replace file extension.
private
static replaceExtension(string $fileName, string $newExtension) : string
Parameters
- $fileName : string
-
Path to file in question.
- $newExtension : string
-
The new extension.
Return values
string —Returns path to file with new extension.
svgoOptimize()
Optimize an image using the svgo program.
private
static svgoOptimize(string $path, int $svgo, bool $isDir) : bool
Parameters
- $path : string
-
The path to the file or directory that should be optimized.
- $svgo : int
-
The number of times to optimize the image.
- $isDir : bool
-
Indicates that the given path is a directory or a file.
Return values
bool —Indicates whether the operation was performed successfully.