All pastes #91522 Raw Edit

Someone

public text v1 · immutable
#91522 ·published 2006-07-18 18:08 UTC
rendered paste body
/**
 * 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.
 * @param $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/'.$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;
}