image_effect.module in image effect 7
Same filename and directory in other branches
Image Effect module file.
File
image_effect.moduleView source
<?php
/**
* @file
* Image Effect module file.
*/
/**
* Implements hook_help().
*/
function image_effect_help($path, $arg) {
switch ($path) {
case "admin/help#image_effect":
return '<p>' . t('add image effect to image module.') . '</p>';
break;
}
}
/**
* Implements hook_image_effect_info()
*/
function image_effect_image_effect_info() {
$effects = array();
$effects['advance_resize'] = array(
'label' => t('Advance Resize'),
'help' => t('Resize image with white background.'),
'effect callback' => 'image_advance_resize_effect',
'dimensions callback' => 'image_resize_dimensions',
'form callback' => 'image_advance_resize_form',
'summary theme' => 'image_resize_summary',
);
return $effects;
}
function image_advance_resize_effect(&$image, $data) {
if (!image_advance_resize($image, $data['width'], $data['height'], $data['background'])) {
watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array(
'%toolkit' => $image->toolkit,
'%path' => $image->source,
'%mimetype' => $image->info['mime_type'],
'%dimensions' => $image->info['width'] . 'x' . $image->info['height'],
), WATCHDOG_ERROR);
return FALSE;
}
return TRUE;
}
function image_advance_resize(stdClass $image, $width, $height, $background) {
return image_effect_image_toolkit_invoke('advance_resize', $image, array(
$width,
$height,
$background,
));
}
function image_advance_resize_form($data) {
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => isset($data['width']) ? $data['width'] : '',
'#field_suffix' => ' ' . t('pixels'),
'#required' => TRUE,
'#size' => 10,
'#element_validate' => array(
'image_effect_integer_validate',
),
'#allow_negative' => FALSE,
);
$form['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => isset($data['height']) ? $data['height'] : '',
'#field_suffix' => ' ' . t('pixels'),
'#required' => TRUE,
'#size' => 10,
'#element_validate' => array(
'image_effect_integer_validate',
),
'#allow_negative' => FALSE,
);
$form['background'] = array(
'#type' => 'checkbox',
'#title' => t('Use transparent background'),
'#default_value' => isset($data['background']) ? $data['background'] : NULL,
);
return $form;
}
function image_effect_image_toolkit_invoke($method, stdClass $image, array $params = array()) {
$function = 'image_effect_' . $image->toolkit . '_' . $method;
if (function_exists($function)) {
array_unshift($params, $image);
return call_user_func_array($function, $params);
}
watchdog('image', 'The selected image handling toolkit %toolkit can not correctly process %function.', array(
'%toolkit' => $image->toolkit,
'%function' => $function,
), WATCHDOG_ERROR);
return FALSE;
}
function image_effect_gd_advance_resize(stdClass $image, $width, $height, $background) {
$w = $width;
$h = $height;
$src_x = 0;
$src_y = 0;
$src_w = $image->info['width'];
$src_h = $image->info['height'];
$src_image = $image->resource;
$dst_w = $src_w;
$dst_h = $src_h;
$dst_x = 0;
$dst_y = 0;
if ($w > $src_w) {
$dst_x = ($w - $src_w) / 2;
}
if ($h > $src_h) {
$dst_y = ($h - $src_h) / 2;
}
$dst_image = imagecreatetruecolor($w, $h);
if (!empty($background)) {
imagesavealpha($dst_image, true);
$backgroundColor = imagecolorallocatealpha($dst_image, 0, 0, 0, 127);
}
else {
$backgroundColor = imagecolorallocate($dst_image, 255, 255, 255);
}
imagefill($dst_image, 0, 0, $backgroundColor);
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($src_image);
$image->resource = $dst_image;
$image->info['width'] = $w;
$image->info['height'] = $h;
return TRUE;
}
function image_effect_imagick_advance_resize(stdClass $image, $width, $height, $background) {
$w = $width;
$h = $height;
$src_x = 0;
$src_y = 0;
$src_w = $image->info['width'];
$src_h = $image->info['height'];
$src_image = $image->resource;
$dst_w = $src_w;
$dst_h = $src_h;
$dst_x = 0;
$dst_y = 0;
if ($w > $src_w) {
$dst_x = ($w - $src_w) / 2;
}
if ($h > $src_h) {
$dst_y = ($h - $src_h) / 2;
}
$dst_image = new Imagick();
if (!empty($background)) {
$dst_image
->newImage($w, $h, new ImagickPixel("transparent"));
}
else {
$dst_image
->newImage($w, $h, new ImagickPixel("white"));
}
$dst_image
->compositeImage($src_image, Imagick::COMPOSITE_DEFAULT, $dst_x, $dst_y);
$src_image
->destroy();
$image->resource = $dst_image;
$image->info['width'] = $w;
$image->info['height'] = $h;
return TRUE;
}