[Решено] Пропускаем remote миниатюру emfield video через imagecache

Главные вкладки

Аватар пользователя FORTIS FORTIS 27 марта 2010 в 15:38

Понадобилось обработать imagecache'ом миниатюру видео-ролика, который вставляется через emfield (с ютюба, гуглвидео, ...)
Миниатюра берется с удаленного ресурса, поэтому потребовалось:

  1. Функция которая скачает миниатюру - phptemplate_get_image($path)
  2. Функция темизации, которая будет выводить миниатюру будет через imagecache - phptemplate_emvideo_video_thumbnail (изменена всего одна строчка)

Вот эти две функции, вставлять в template.php:

function phptemplate_get_image($path) {
    $return = "";
    $base_path = file_directory_path()."/video-gallery-thumb";
    if (!file_exists($base_path)) {
      mkdir($base_path, 0777);
    }

    $cut_path = str_ireplace("http://", "", $path);
    $cut_path = preg_replace("/^[^\/]*\//", "", $path);
    $image_name = str_ireplace("/", "-", $cut_path);
               
    $image_path = $base_path."/".$image_name;
    if (file_exists($image_path)) {
        $return = $image_path;
    } else {
        $remote_image = file_get_contents($path) or die('Could not grab the file');
        $local_image  = fopen($image_path, 'w+') or die('Could not create the file');

        fputs($local_image, $remote_image) or die('Could not write to the file');
        fclose($local_image);
        unset($remote_image);
       
        $return = $image_path;
    }
   
    return $return;
}

function phptemplate_emvideo_video_thumbnail($field, $item, $formatter, $node, $no_link = FALSE, $options = array()) {
  // Thickbox requires some output, so make sure we have at least a blank string.
  $output = '';

  $options['node'] = $node;
  $width = isset($options['width']) ? $options['width'] : ($field['widget']['thumbnail_width'] ? $field['widget']['thumbnail_width'] : variable_get('emvideo_default_video_width', EMVIDEO_DEFAULT_THUMBNAIL_WIDTH));
  $height = isset($options['height']) ? $options['height'] : ($field['widget']['thumbnail_height'] ? $field['widget']['thumbnail_height'] : variable_get('emvideo_default_video_height', EMVIDEO_DEFAULT_THUMBNAIL_HEIGHT));

  // Get a thumbnail URL. This can be an override through $options['thumbnail_url'],
  // defined by the provider (the usual case), or a default image.
  if (isset($options['thumbnail_url']) || ($item['value'] && $item['provider'])) {
    // If we've set $options['thumbnail_url'], then we'll just use that.
    $thumbnail_url = isset($options['thumbnail_url']) ? $options['thumbnail_url'] : '';

    // Otherwise, if we have emthumb installed, then give it a chance to override our thumbnail.
    if (empty($thumbnail_url) && function_exists('emthumb_thumbnail_path')) {
      $thumbnail_url = emthumb_thumbnail_path($item);
    }

    // If we don't have a custom thumbnail, then see if the provider gives us a thumbnail.
    if (empty($thumbnail_url)) {
      $thumbnail_url = emfield_include_invoke('emvideo', $item['provider'], 'thumbnail', $field, $item, $formatter, $node, $width, $height, $options);
    }
  }

  // If we still don't have a thumbnail, then apply a default thumbnail, if it exists.
  if (empty($thumbnail_url)) {
    $default_thumbnail_url = $field['widget']['thumbnail_default_path'] ? $field['widget']['thumbnail_default_path'] : variable_get('emvideo_default_thumbnail_path', NULL);
    if ($default_thumbnail_url) {
      $thumbnail_url = $default_thumbnail_url;
    }
  }

  // Define the thumbnail link's path.
  $link_url = isset($options['link_url']) ? $options['link_url'] : 'node/'. $node->nid;

  // Define the title/alt to display for the link hover.
  $link_title = isset($options['link_title']) ? $options['link_title'] : (isset($options['title']) ? $options['title'] : (isset($field['widget']['thumbnail_link_title']) ? $field['widget']['thumbnail_link_title'] : variable_get('emvideo_default_thumbnail_link_title', t('See video'))));
  if (module_exists('token')) {
    // Allow the editor to use [title] tokens.
    $link_title = token_replace($link_title, 'global', $node);
  }
  $image_title = isset($options['image_title']) ? $options['image_title'] : $link_title;
  $image_alt = isset($options['image_alt']) ? $options['image_alt'] : $link_title;

  if (isset($thumbnail_url)) {
    if (isset($options['return_url'])) {
      return $options['raw'] ? $thumbnail_url : url($thumbnail_url);
    }
    $width = isset($options['width']) ? $options['width'] : NULL;
    $width = isset($width) ? $width : ($field['widget']['thumbnail_width'] ? $field['widget']['thumbnail_width'] : variable_get('emvideo_default_thumbnail_width', EMVIDEO_DEFAULT_THUMBNAIL_WIDTH));
    $height = isset($options['height']) ? $options['height'] : NULL;
    $height = isset($height) ? $height : ($field['widget']['thumbnail_height'] ? $field['widget']['thumbnail_height'] : variable_get('emvideo_default_thumbnail_height', EMVIDEO_DEFAULT_THUMBNAIL_HEIGHT));
    $attributes = isset($options['attributes']) ? $options['attributes'] : array();
    $attributes['width'] = isset($attributes['width']) ? $attributes['width'] : $width;
    $attributes['height'] = isset($attributes['height']) ? $attributes['height'] : $height;
    /*$image = theme('image', $thumbnail_url, $image_alt, $image_title, $attributes, FALSE);*/
                $image = theme('imagecache', 'gallery-thumb', phptemplate_get_image($thumbnail_url), $image_alt, $image_title, $attributes); /* <= ДОБАВИЛ */
    if ($no_link) {
      // Thickbox requires the thumbnail returned without the link.
      $output = $image;
    }
    else {
      $output = l($image, $link_url, array('html'=> TRUE));
    }
  }
  else {
    if ($options['return_url']) {
      return $options['raw'] ? $thumbnail_url : url($link_url);
    }

    // If all else fails, then just print a 'see video' type link.
    if (!$no_link) {
      $output = l($link_title, $link_url);
    }
  }
  return $output;
}

Комментарии