Reviewed & tested by the community
Project:
Node Images
Component:
Code
Priority:
Minor
Category:
Feature request
Assigned:
Reporter:
Created:
9 May 2007 at 15:35 UTC
Updated:
11 Dec 2007 at 12:34 UTC
Jump to comment: Most recent file
I've made a modification to the thumbnail creation fucntion in order to keep thumbnails in a fixed size. I achieved this by cropping what exceeds the proportion. I added this code to _node_images_create_thumbnail function:
function _node_images_create_thumbnail($path, $suffix='_tn') {
$size = variable_get('node_images_thumb_resolution', '100x100');
list($width, $height) = explode('x', $size);
$dest_path = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $path, 1);
if ($size = getimagesize($path)) {
// Mod: Crop in order to maintain proportions.
$currentWidth = $size[0];
$currentHeight = $size[1];
$currentDiff = $currentWidth/$currentHeight;
$diff = $width/$height;
if($diff >= 1) {
if($currentDiff >= $diff) {
image_scale($path, $dest_path, $currentWidth, $height);
} else {
image_scale($path, $dest_path, $width, $currentHeight);
}
} elseif($diff < 1) {
if($currentDiff >= $diff){
image_scale($path, $dest_path, $currentWidth, $height);
} else {
image_scale($path, $dest_path, $width, $currentHeight);
}
}
$newSize = getimagesize($dest_path);
$newWidth = $newSize[0];
$newHeight = $newSize[1];
$xPos = 0;
$yPos = 0;
if($newWidth > $width){
$xPos = intval(($newWidth - $width)/2);
} elseif($newHeight > $height){
$yPos = intval(($newHeight - $height)/2);
}
image_crop($dest_path, $dest_path, $xPos, $yPos, $width, $height);
//Mod end.
// image_scale($path, $dest_path, $width, $height);
$info = image_get_info($dest_path);
$thumb = new stdClass();
$thumb->filename = basename($dest_path);
$thumb->filepath = $dest_path;
$thumb->filesize = $info['file_size'];
$thumb->filemime = $info['mime_type'];
return $thumb;
}
return NULL;
}
I'd love someone with more expertise in module construction to see if this could be included as an option in Node Images administration.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | node_images.module-crop.patch | 2.95 KB | charly |
Comments
Comment #1
charly commentedThanks!
I made a patch using your code and adding a setting option.
Regards,
Comment #2
soflete commentedWell, thak you very much for making this patch! It worked perfectly. I'll make good use of it.
Ragards,
Comment #3
Vallenwood commentedAttention maintainers: please read my feature request/patch at http://drupal.org/node/147538 before committing this patch. I have expanded on this concept significantly and I believe this patch is no longer necessary. I encourage anyone reading to visit the new patch, and request that the module maintainers consider the one I just added instead of this one. Excellent idea by soflete, but I think I came up with something more robust. Thanks for a great idea, soflete!
Comment #4
soflete commentedSorry I haven't read this before. I'm glad that this minimal contribution I tried to make inspired such an impressive work. I want to apoligize for my spelling mistakes. I'm from Argentina and here we speak spanish. I hope someday I can write such good code and texts in english as you do. You're awesome!