PHP Class nwtn\Respimg

This class extends Imagick () based on research into optimal image resizing techniques (). 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.
Author: David Newton ([email protected])
Inheritance: extends Imagick
Show file Open project: nwtn/php-respimg Class Usage Examples

Public Methods

Method Description
optimize ( string $path, integer $svgo, integer $image_optim, integer $picopt, integer $imageOptim ) Optimizes the image without reducing quality.
rasterize ( string $file, string $dest, integer $columns, integer $rows ) Rasterizes an SVG image to a PNG.
smartResize ( integer $columns, integer $rows, boolean $optim = false ) Resizes the image using smart defaults for high quality and low file size.
thumbnailImage ( integer $columns, integer $rows, boolean $bestfit = false, boolean $fill = false, integer $filter = Imagick::FILTER_TRIANGLE ) : boolean Changes the size of an image to the given dimensions and removes any associated profiles.

Method Details

optimize() public static method

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.
public static optimize ( string $path, integer $svgo, integer $image_optim, integer $picopt, integer $imageOptim )
$path string The path to the file or directory that should be optimized.
$svgo integer The number of times to optimize using SVGO.
$image_optim integer The number of times to optimize using image_optim.
$picopt integer The number of times to optimize using picopt.
$imageOptim integer The number of times to optimize using ImageOptim.

rasterize() public static method

Uses phantomjs to save the SVG as a PNG image at the specified size.
public static rasterize ( string $file, string $dest, integer $columns, integer $rows )
$file string The path to the file that should be rasterized.
$dest string The path to the directory where the output PNG should be saved.
$columns integer The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
$rows integer The number of rows in the output image. 0 = maintain aspect ratio based on $columns.

smartResize() public method

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
public smartResize ( integer $columns, integer $rows, boolean $optim = false )
$columns integer The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
$rows integer The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
$optim boolean Whether you intend to perform optimization on the resulting image. Note that setting this to `true` doesn’t actually perform any optimization.

thumbnailImage() public method

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: has been filed for this issue.
public thumbnailImage ( integer $columns, integer $rows, boolean $bestfit = false, boolean $fill = false, integer $filter = Imagick::FILTER_TRIANGLE ) : boolean
$columns integer The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
$rows integer The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
$bestfit boolean Treat $columns and $rows as a bounding box in which to fit the image.
$fill boolean Fill in the bounding box with the background colour.
$filter integer The resampling filter to use. Refer to the list of filter constants at .
return boolean Indicates whether the operation was performed successfully.