public function thumbnailImage($columns, $rows, $bestfit = false, $fill = false, $filter = \Imagick::FILTER_TRIANGLE)
{
// sample factor; defined in original ImageMagick thumbnailImage function
// the scale to which the image should be resized using the `sample` function
$SampleFactor = 5;
// filter whitelist
$filters = array(\Imagick::FILTER_POINT, \Imagick::FILTER_BOX, \Imagick::FILTER_TRIANGLE, \Imagick::FILTER_HERMITE, \Imagick::FILTER_HANNING, \Imagick::FILTER_HAMMING, \Imagick::FILTER_BLACKMAN, \Imagick::FILTER_GAUSSIAN, \Imagick::FILTER_QUADRATIC, \Imagick::FILTER_CUBIC, \Imagick::FILTER_CATROM, \Imagick::FILTER_MITCHELL, \Imagick::FILTER_LANCZOS, \Imagick::FILTER_BESSEL, \Imagick::FILTER_SINC);
// Parse parameters given to function
$columns = (double) $columns;
$rows = (double) $rows;
$bestfit = (bool) $bestfit;
$fill = (bool) $fill;
// We can’t resize to (0,0)
if ($rows < 1 && $columns < 1) {
return false;
}
// Set a default filter if an acceptable one wasn’t passed
if (!in_array($filter, $filters)) {
$filter = \Imagick::FILTER_TRIANGLE;
}
// figure out the output width and height
$width = (double) $this->getImageWidth();
$height = (double) $this->getImageHeight();
$new_width = $columns;
$new_height = $rows;
$x_factor = $columns / $width;
$y_factor = $rows / $height;
if ($rows < 1) {
$new_height = round($x_factor * $height);
} elseif ($columns < 1) {
$new_width = round($y_factor * $width);
}
// if bestfit is true, the new_width/new_height of the image will be different than
// the columns/rows parameters; those will define a bounding box in which the image will be fit
if ($bestfit && $x_factor > $y_factor) {
$x_factor = $y_factor;
$new_width = round($y_factor * $width);
} elseif ($bestfit && $y_factor > $x_factor) {
$y_factor = $x_factor;
$new_height = round($x_factor * $height);
}
if ($new_width < 1) {
$new_width = 1;
}
if ($new_height < 1) {
$new_height = 1;
}
// if we’re resizing the image to more than about 1/3 it’s original size
// then just use the resize function
if ($x_factor * $y_factor > 0.1) {
$this->resizeImage($new_width, $new_height, $filter, 1);
// if we’d be using sample to scale to smaller than 128x128, just use resize
} elseif ($SampleFactor * $new_width < 128 || $SampleFactor * $new_height < 128) {
$this->resizeImage($new_width, $new_height, $filter, 1);
// otherwise, use sample first, then resize
} else {
$this->sampleImage($SampleFactor * $new_width, $SampleFactor * $new_height);
$this->resizeImage($new_width, $new_height, $filter, 1);
}
// if the alpha channel is not defined, make it opaque
if ($this->getImageAlphaChannel() == \Imagick::ALPHACHANNEL_UNDEFINED) {
$this->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE);
}
// set the image’s bit depth to 8 bits
$this->setImageDepth(8);
// turn off interlacing
$this->setInterlaceScheme(\Imagick::INTERLACE_NO);
// Strip all profiles except color profiles.
foreach ($this->getImageProfiles('*', true) as $key => $value) {
if ($key != 'icc' && $key != 'icm') {
$this->removeImageProfile($key);
}
}
if (method_exists($this, 'deleteImageProperty')) {
$this->deleteImageProperty('comment');
$this->deleteImageProperty('Thumb::URI');
$this->deleteImageProperty('Thumb::MTime');
$this->deleteImageProperty('Thumb::Size');
$this->deleteImageProperty('Thumb::Mimetype');
$this->deleteImageProperty('software');
$this->deleteImageProperty('Thumb::Image::Width');
$this->deleteImageProperty('Thumb::Image::Height');
$this->deleteImageProperty('Thumb::Document::Pages');
} else {
$this->setImageProperty('comment', '');
$this->setImageProperty('Thumb::URI', '');
$this->setImageProperty('Thumb::MTime', '');
$this->setImageProperty('Thumb::Size', '');
$this->setImageProperty('Thumb::Mimetype', '');
$this->setImageProperty('software', '');
$this->setImageProperty('Thumb::Image::Width', '');
$this->setImageProperty('Thumb::Image::Height', '');
$this->setImageProperty('Thumb::Document::Pages', '');
}
// In case user wants to fill use extent for it rather than creating a new canvas
// …fill out the bounding box
if ($bestfit && $fill && ($new_width != $columns || $new_height != $rows)) {
$extent_x = 0;
$extent_y = 0;
if ($columns > $new_width) {
$extent_x = ($columns - $new_width) / 2;
}
if ($rows > $new_height) {
$extent_y = ($rows - $new_height) / 2;
}
$this->extentImage($columns, $rows, 0 - $extent_x, $extent_y);
}
return true;
}