View source
<?php
function image_imagick_convert(stdClass $image, $format, $quality) {
$formats = imagick_file_formats();
if ($format == 'image/jpeg') {
$background = new Imagick();
$background
->newImage($image->info['width'], $image->info['height'], 'white');
$image->resource
->compositeImage($background, Imagick::COMPOSITE_DSTOVER, 0, 0);
}
$image->resource
->setImageProperty('quality', (int) $quality);
$image->resource
->setImageFormat($formats[$format]);
return $image;
}
function imagick_convert($image, $data = array()) {
image_toolkit_invoke('convert', $image, $data);
}
function imagick_convert_form($data) {
$data = array_merge(imagick_convert_defaults(), (array) $data);
$form['format'] = array(
'#title' => t("File format"),
'#type' => 'select',
'#default_value' => $data['format'],
'#options' => imagick_file_formats(),
);
$form['quality'] = array(
'#type' => 'textfield',
'#title' => t('Quality'),
'#description' => t('Override the default image quality. Works for Imagemagick only. Ranges from 0 to 100. For jpg, higher values mean better image quality but bigger files. For png it is a combination of compression and filter'),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => $data['quality'],
'#field_suffix' => '%',
);
return $form;
}
function imagick_convert_defaults() {
return array(
'format' => 'image/jpeg',
'quality' => variable_get('image_jpeg_quality', 80),
);
}