function image_imagick_save in Imagick 7
Imagick helper to write an image resource to a destination file.
Parameters
$image: An image object.
$destination: A string file URI or path where the image should be saved.
Return value
TRUE or FALSE, based on success.
See also
File
- ./
imagick.module, line 232 - Imagick toolkit for image manipulation within Drupal.
Code
function image_imagick_save(stdClass $image, $destination) {
$res = $image->resource;
$scheme = file_uri_scheme($destination);
// Work around lack of stream wrapper support in imagejpeg() and imagepng().
if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
// If destination is not local, save image to temporary local file.
$local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
if (!isset($local_wrappers[$scheme])) {
$permanent_destination = $destination;
$destination = drupal_tempnam('temporary://', 'imagick_');
}
// Convert stream wrapper URI to normal path.
$destination = drupal_realpath($destination);
}
// If preferred format is set, use it as prefix for writeImage
// If not this will throw a ImagickException exception
try {
$image_format = strtolower($res
->getImageFormat());
$destination = implode(':', array(
$image_format,
$destination,
));
} catch (ImagickException $e) {
}
// Only compress JPEG files because other filetypes will increase in filesize
if (isset($image_format) && in_array($image_format, array(
'jpeg',
'jpg',
))) {
// Use image quality if it has been set by an effect
$effect_quality = $res
->getImageProperty('quality');
if (!empty($effect_quality)) {
$res
->setImageCompressionQuality($effect_quality);
}
else {
$quality = variable_get('image_jpeg_quality', 80);
$res
->setImageCompressionQuality($quality);
}
if (variable_get('imagick_image_optimize', TRUE)) {
// Using recommendations from Google's Page Speed docs:
// https://developers.google.com/speed/docs/insights/OptimizeImages .
$res
->setSamplingFactors(array(
'2x2',
'1x1',
'1x1',
));
$res
->setColorspace(imagick::COLORSPACE_RGB);
$res
->setInterlaceScheme(imagick::INTERLACE_JPEG);
}
}
// Strip metadata
if (variable_get('imagick_strip_metadata', FALSE)) {
$res
->stripImage();
}
// Write image to destination
if (isset($image_format) && in_array($image_format, array(
'gif',
))) {
if (!$res
->writeImages($destination, TRUE)) {
return FALSE;
}
}
else {
if (!$res
->writeImage($destination)) {
return FALSE;
}
}
// Move temporary local file to remote destination.
if (isset($permanent_destination)) {
return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
}
return TRUE;
}