function iek_gd_resize in Image effect kit 7
Resize an image by using the GD toolkit.
Parameters
object $image: An image object. The $image->resource, $image->info['width'], and $image->info['height'] values will be modified by this call.
array $data: An array that contains all the effect parameters. $data['width']: destination image width, in pixels. $data['height']: destination image height, in pixels. $data['blank_margin']: TRUE to show blank margin, FALSE to fulfill bg. $data['blank_margin_bg_color']: the background color of the blank margin. $data['position']: the starting position to crop the image.
Return value
bool TRUE or FALSE, based on success.
2 calls to iek_gd_resize()
- iek_gd_border in ./
iek.gd.inc - Add an image border by using the GD toolkit.
- iek_gd_padding in ./
iek.gd.inc - Add an image padding by using the GD toolkit.
File
- ./
iek.gd.inc, line 212 - GD2 toolkit for image manipulation within Drupal.
Code
function iek_gd_resize(stdClass $image, $data) {
$debug = FALSE;
// Resets width and height to zero if they are negative values.
// Sees the issue: https://drupal.org/node/2141463.
if ($data['width'] < 0 || $data['height'] < 0) {
$image->info['width'] = 0;
$image->info['height'] = 0;
return TRUE;
}
$width = $data['width'];
$height = $data['height'];
$blank_margin = $data['blank_margin'];
$blank_margin_bg_color = !empty($data['blank_margin_bg_color']) ? $data['blank_margin_bg_color'] : '#ffffff';
$position = $data['position'];
$dst = imagecreatetruecolor($width, $height);
$bg_rgb = iek_image_hex2rgb($blank_margin_bg_color);
$bg = imagecolorallocate($dst, $bg_rgb['red'], $bg_rgb['green'], $bg_rgb['blue']);
imagefilledrectangle($dst, 0, 0, $width, $height, $bg);
if ($blank_margin) {
image_scale($image, $width, $height, TRUE);
}
else {
$src_ratio = round($image->info['width'] / $image->info['height'], 8);
$dst_ratio = round($width / $height, 8);
if ($src_ratio >= 1) {
if ($dst_ratio >= 1) {
if ($src_ratio > $dst_ratio) {
if ($debug) {
dpm('Case 1');
}
$scaled_height = $width;
$scaled_width = $src_ratio * $width;
}
else {
if ($debug) {
dpm('Case 2');
}
$scaled_width = $width;
$scaled_height = $width / $src_ratio;
}
}
else {
if ($debug) {
dpm('Case 3');
}
$scaled_height = $height;
$scaled_width = $src_ratio * $height;
}
}
else {
if ($dst_ratio >= 1) {
if ($debug) {
dpm('Case 11');
}
$scaled_width = $width;
$scaled_height = $width / $src_ratio;
}
else {
if ($src_ratio < $dst_ratio) {
if ($debug) {
dpm('Case 12');
}
$scaled_width = $height;
$scaled_height = $height / $src_ratio;
}
else {
if ($debug) {
dpm('Case 13');
}
$scaled_height = $height;
$scaled_width = $height * $src_ratio;
}
}
}
$scaled_ratio = round($scaled_width / $scaled_height, 8);
if ($debug) {
dpm('$src : ' . $image->info['width'] . 'x' . $image->info['height']);
dpm('$dst : ' . $width . 'x' . $height);
dpm('$scaled : ' . $scaled_width . 'x' . $scaled_height);
if ($src_ratio == $scaled_ratio && $scaled_width - $width >= 0 && $scaled_height - $height >= 0) {
dpm('Good scaled:');
dpm('$src_ratio : ' . $src_ratio);
dpm('$scaled_ratio : ' . $scaled_ratio);
}
else {
dpm('Bad scaled:');
dpm('$src_ratio : ' . $src_ratio);
dpm('$scaled_ratio : ' . $scaled_ratio);
}
}
image_scale($image, $scaled_width, $scaled_height, TRUE);
}
switch ($position) {
case 'coordinate':
$src_x = $data['x'];
$src_y = $data['y'];
$dst_x = 0;
$dst_y = 0;
break;
case 'top_left':
$src_x = 0;
$src_y = 0;
$dst_x = 0;
$dst_y = 0;
break;
case 'top_center':
$src_x = -(($image->info['width'] - $width) / 2);
$src_y = 0;
$dst_x = 0;
$dst_y = 0;
break;
case 'top_right':
$src_x = -($image->info['width'] - $width);
$src_y = 0;
$dst_x = 0;
$dst_y = 0;
break;
case 'middle_left':
$src_x = 0;
$src_y = -(($image->info['height'] - $height) / 2);
$dst_x = 0;
$dst_y = 0;
break;
case 'middle_center':
$src_x = -(($image->info['width'] - $width) / 2);
$src_y = -(($image->info['height'] - $height) / 2);
$dst_x = 0;
$dst_y = 0;
break;
case 'middle_right':
$src_x = -($image->info['width'] - $width);
$src_y = -(($image->info['height'] - $height) / 2);
$dst_x = 0;
$dst_y = 0;
break;
case 'bottom_left':
$src_x = 0;
$src_y = -($image->info['height'] - $height);
$dst_x = 0;
$dst_y = 0;
break;
case 'bottom_center':
$src_x = -(($image->info['width'] - $width) / 2);
$src_y = -($image->info['height'] - $height);
$dst_x = 0;
$dst_y = 0;
break;
case 'bottom_right':
$src_x = -($image->info['width'] - $width);
$src_y = -($image->info['height'] - $height);
$dst_x = 0;
$dst_y = 0;
break;
default:
$src_x = 0;
$src_y = 0;
$dst_x = 0;
$dst_y = 0;
}
if (!imagecopy($dst, $image->resource, $src_x, $src_y, $dst_x, $dst_y, $image->info['width'], $image->info['height'])) {
return FALSE;
}
imagedestroy($image->resource);
// Update image object.
$image->resource = $dst;
$image->info['width'] = $width;
$image->info['height'] = $height;
return TRUE;
}