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
- optimize() : mixed
- Optimizes the image without reducing quality.
- resize() : mixed
- smartResize() : mixed
- Resizes the image using smart defaults for high quality and low file size.
- thumbnailImage() : bool
- Changes the size of an image to the given dimensions and removes any associated profiles.
Methods
optimize()
Optimizes the image without reducing quality.
public
static optimize(string $path, int $svgo, int $image_optim, int $picopt, int $imageOptim) : mixed
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.
- $image_optim : 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.
Tags
Return values
mixed —resize()
public
resize( $file, $width, $height, $output) : mixed
Parameters
Tags
Return values
mixed —smartResize()
Resizes the image using smart defaults for high quality and low file size.
public
smartResize(int $columns, int $rows[, bool $optim = false ][, string $filter = Imagick::FILTER_TRIANGLE ][, bool $bestfit = false ][, bool $crop = false ]) : mixed
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 : string = 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
Tags
Return values
mixed —thumbnailImage()
Changes the size of an image to the given dimensions and removes any associated profiles.
public
thumbnailImage(int $columns, int $rows[, bool $bestfit = false ][, bool $fill = false ][, int $filter = Imagick::FILTER_TRIANGLE ]) : bool
thumbnailImage 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 thumbnailImage 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.
Tags
Return values
bool —Indicates whether the operation was performed successfully.