You are here

function imceimage_field_formatter in Imce CCK Image 6.2

Same name and namespace in other branches
  1. 6 imceimage.module \imceimage_field_formatter()
1 call to imceimage_field_formatter()
theme_imceimage_formatter in ./imceimage.module
Generic callback for all formatters based on imagecache presets. See imceimage_theme()

File

./imceimage.module, line 451

Code

function imceimage_field_formatter($field, $item, $formatter, $node) {
  $parts = explode('_', $formatter);
  $style = array_pop($parts);
  $presetname = implode('_', $parts);
  $class = "imagecache imagecache-{$presetname} imagecache-{$style} imagecache-{$formatter}";
  $caption = $item['imceimage_alt'];
  $title = $item['imceimage_title'];
  $image_url = $item['imceimage_path'];
  if (empty($image_url)) {
    return '';
  }

  // $file_path_url = $server . $file_directory . $subdir . $filename;
  // $server can be different when there are multi-sites
  $path_parts = parse_url($item['imceimage_path']);

  // $file_directory can be different for public / private download
  $file_directory = variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE ? 'system/files' : file_directory_path();
  $filename = basename($item['imceimage_path']);
  $subdir = str_replace("/" . $file_directory . "/", '', str_replace('/' . $filename, '', $path_parts['path']));
  $orig_filepath = $subdir . '/' . $filename;
  $imagecache_url = imagecache_create_url($presetname, $orig_filepath);

  //"subdir/subdir2/imageabc.jpg"
  $full_img_url = file_create_url($orig_filepath);
  if ($preset = imagecache_preset_by_name($presetname)) {
    $dst = imagecache_create_path($presetname, $orig_filepath);
    if (!file_exists($dst)) {
      imagecache_build_derivative($preset['actions'], $filepath, $dst);
    }
    switch ($style) {
      case 'linked':
        $imagetag = theme('imagecache', $presetname, $orig_filepath, $caption, $title);
        $img = l($imagetag, 'node/' . $node->nid, array(
          'attributes' => array(
            'class' => $class,
          ),
          'html' => true,
        ));
        return $img;
      case 'imagelink':
        $imagetag = theme('imagecache', $presetname, $orig_filepath, $caption, $title);
        $img = l($imagetag, $full_img_url, array(
          'attributes' => array(
            'class' => $class,
            'target' => '_blank',
          ),
          'html' => true,
        ));
        return $img;
      case 'url':
        return $imagecache_url;
      case 'path':
        return imagecache_create_path($presetname, $orig_filepath);
      default:
        return theme('imagecache', $presetname, $orig_filepath, $caption, $title);
    }
  }
  return '<!-- imagecache formatter preset(' . $presetname . ') not found! -->';
}