Hello
I created new action for imagecache for achieving square thumbnails from every image without ignored aspect ratio.
It works by finding the largest square that will fit into the original image, centering it, cropping, then scaling down.
You will lose a small amount from the top & bottom or left & right in the thumbnail, but you will not get any image distortion. Most images look fine with this cropping.
The idea comes from http://drupal.org/project/image_square_thumbnails
Here is the code i added to imagecache module
I added to imagecache_actions.inc file this functions
/**
* ImageCache Crop Square
*/
function imagecache_crop_square_form($data) {
$data += array(
'width' => '',
'height' => '',
);
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $data['width'],
'#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
);
$form['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $data['height'],
'#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
);
return $form;
}
function theme_imagecache_crop_square($element) {
$data = $element['#value'];
return 'width: '. $data['width'] .', height: '. $data['height'];
}
function imagecache_crop_square_image(&$image, $data) {
// Calculate the coordinates to crop from
if ($image->info['height'] < $image->info['width']) {
$cropSize = $image->info['height'];
$cropX = (int) (($image->info['width'] - $image->info['height'])/2);
$cropY = 0;
}
else {
$cropSize = $image->info['width'];
$cropX = 0;
$cropY = (int) (($image->info['height'] - $image->info['width'])/2);
}
// Crop the image to a square as large as possible, centered in the image
$state = imageapi_image_crop($image, $cropX, $cropY, $cropSize, $cropSize);
if ($state) {
// Resize the square image to the 'width' of the thumbnail setting (so height is essentially ignored)
$state = imageapi_image_resize($image, $data['width'], $data['height']);
}
if (!$state) {
watchdog('imagecache', 'imagecache_crop_square failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, true)), WATCHDOG_ERROR);
return false;
}
return true;
}
and two things in imagecache.module
new item in imagecache_theme()
'imagecache_crop_square' => array(
'file' => 'imagecache_actions.inc',
'arguments' => array('element' => NULL),
),
and new action in imagecache_imagecache_actions()
'imagecache_crop_square' => array(
'name' => 'Crop Square',
'description' => 'Crop an image to the rectangle specified by the given offsets and dimensions (saving aspect ratio). ',
'file' => 'imagecache_actions.inc',
),
Then clear the cache and try the new action
Hope this will be in 2.1 release of this great module
Comments
Comment #1
open-keywords commentedLooks like a very sensible feature!
Comment #2
open-keywords commentedHowever, I don't get why you offer inputs for height and width since both are the same for a square ?
I gess you code works for Drupal 6, isn't it ?
Comment #3
drewish commentedYeah i'm missing how this is different than just doing a crop and specifying the dimensions.
Comment #4
drewish commentedwell http://drupal.org/project/image_square_thumbnails says we do it.