\nwtnRespimg

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.

Summary

Methods
Properties
Constants
optimize()
rasterize()
smartResize()
thumbnailImage()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

optimize()

optimize(string  $path, integer  $svgo, integer  $image_optim, integer  $picopt, integer  $imageOptim) 

Optimizes the image without reducing quality.

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

string $path

The path to the file or directory that should be optimized.

integer $svgo

The number of times to optimize using SVGO.

integer $image_optim

The number of times to optimize using image_optim.

integer $picopt

The number of times to optimize using picopt.

integer $imageOptim

The number of times to optimize using ImageOptim.

rasterize()

rasterize(string  $file, string  $dest, integer  $columns, integer  $rows) 

Rasterizes an SVG image to a PNG.

Uses phantomjs to save the SVG as a PNG image at the specified size.

Parameters

string $file

The path to the file that should be rasterized.

string $dest

The path to the directory where the output PNG should be saved.

integer $columns

The number of columns in the output image. 0 = maintain aspect ratio based on $rows.

integer $rows

The number of rows in the output image. 0 = maintain aspect ratio based on $columns.

smartResize()

smartResize(integer  $columns, integer  $rows, boolean  $optim = false) 

Resizes the image using smart defaults for high quality and low file size.

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

integer $columns

The number of columns in the output image. 0 = maintain aspect ratio based on $rows.

integer $rows

The number of rows in the output image. 0 = maintain aspect ratio based on $columns.

boolean $optim

Whether you intend to perform optimization on the resulting image. Note that setting this to true doesn’t actually perform any optimization.

thumbnailImage()

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.

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

integer $columns

The number of columns in the output image. 0 = maintain aspect ratio based on $rows.

integer $rows

The number of rows in the output image. 0 = maintain aspect ratio based on $columns.

boolean $bestfit

Treat $columns and $rows as a bounding box in which to fit the image.

boolean $fill

Fill in the bounding box with the background colour.

integer $filter

The resampling filter to use. Refer to the list of filter constants at http://php.net/manual/en/imagick.constants.php.

Returns

boolean —

Indicates whether the operation was performed successfully.