/**
* Given the filepath of a full sized image bank image, return the
* filepath to the thumbnail'd version. This function will first create
* the thumbnail if it doesn't yet exist. The second argument, height,
* can be used to proportionally scale the image to a certain size.
*
* @param $filepath
* The filepath to a full sized image.
* @param $height
* Defaults to 90; the height of the thumbnail.
* @return $thumbnailpath
* The path to the (possibly newly generated) thumbnail.
*/
function cnucustom_imagebank_thumbnail($filepath, $height = 90) {
$filepath_parts = explode('/', $filepath); $filename = array_pop($filepath_parts);
$thumbnailpath = implode('/', $filepath_parts)."/thumbnails/$height-$filename";
// with no thumbnail, we'll create our own. we only know what we want
// the height to be, so we figure out the proper width proportion first.
if (!file_exists($thumbnailpath)) {
$info = image_get_info($filepath);
$ratio_change = $height / $info['height'];
$width = $info['width'] * $ratio_change;
image_resize($filepath, $thumbnailpath, $width, $height);
}
return $thumbnailpath;
}
/**
* Given the filepath of a full sized image bank image, return the filepath
* to a resized/cropped version as determined by the width and height. This
* function will create the resized version if it does not already exist.
*
* @param $filepath
* The filepath to a full sized image.
* @param $final_width
* Defaults to 25; the width of the thumbnail.
* @param $final_height
* Defaults to 25; the height of the thumbnail.
* @return $thumbnailpath
* The path to the (possibly newly generated) image.
*/
function cnucustom_imagebank_resize($filepath, $final_width = 80, $final_height = 80) {
$filepath_parts = explode('/', $filepath); $filename = array_pop($filepath_parts);
$thumbnailpath = implode('/', $filepath_parts)."/thumbnails/${final_width}x${final_height}-$filename";
// ar == aspect ratio.
if (!file_exists($thumbnailpath)) {
$source_info = image_get_info($filepath);
$source_ar = $source_info['width'] / $source_info['height'];
if ($source_info['width'] > $final_width && $source_info['height'] > $final_height) {
// only proceed if we've got a big enough source file... don't stretch a tiny one.
$final_ar = $final_width / $final_height;
if ($source_ar > $final_ar) {
$width = round($source_info['width'] / ($source_ar / $final_ar));
// start crop at halfway point to retain center.
$x = round(($source_info['width'] - $width) / 2);
$y = 0; $height = $source_info['height'];
image_crop($filepath, $thumbnailpath, $x, $y, $width, $height);
} elseif ($source_ar < $final_ar) {
$height = round($source_info['height'] * ($source_ar / $final_ar));
// start crop at halfway point to retain center.
$y = round(($source_info['height'] - $height) / 2);
$x = 0; $width = $source_info['width'];
image_crop($filepath, $thumbnailpath, $x, $y, $width, $height);
}
// resize cropped image (with right AR) to correct size.
image_resize($thumbnailpath, $thumbnailpath, $final_width, $final_height);
}
else { // image was too small to crop, so just resize down.
image_resize($filepath, $thumbnailpath, $final_width, $final_height);
}
}
return $thumbnailpath;
}
/**
* Given a uid and a count, returns an array of that many image bank images
* ready for display in HTML, with thumbnails and links to the full node.
*/
function cnucustom_imagebank_recent($uid, $count = 6) {
$results = db_query_range('SELECT nid FROM {node} n WHERE type = "%s" AND uid = %d ORDER BY n.created DESC', 'content-image_bank', $uid, 0, $count);
$images = array(); while ($result = db_fetch_object($results)) { // grab six images by default since that maps to a 2x3 matrix on member pages.
$node = node_load($result->nid); node_view(&$node); $image = array_shift($node->files);
$images[] = l(theme('image', cnucustom_imagebank_resize($image->filepath), $node->field_image_caption[0]['view'], $node->field_image_caption[0]['view']), 'node/'.$node->nid, NULL
, NULL, NULL, NULL, TRUE);
} return $images;
}