You are here

filefield_meta.module in FileField 5.2

File

filefield_meta/filefield_meta.module
View source
<?php

// $Id$

/**
 * Add Video Support to File Field.
 */

/**
 * Implementation of hook_filefield
 *
 * @param op
 *  - file_save
 *  - file_delete
 *  - file_prepare
 *  - file_validate
 *  - file_load
 *  - file_form
 *  - delete
 *
 * @param node
 * @param field
 * @param file
 * @param form
 */
define('FILEFIELD_META_MINIMUM_PHP', '5.2');

/**
 *  Implementation of hook_requirements().
 */
function filefield_meta_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();
  if (version_compare(phpversion(), FILEFIELD_META_MINIMUM_PHP) < 0) {
    $requirements['filefield_meta_php'] = array(
      'title' => $t('FileField Meta PHP'),
      'description' => $t('FileField Meta requires at least PHP %version.', array(
        '%version' => FILEFIELD_META_MINIMUM_PHP,
      )),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  return $requirements;
}
function filefield_meta_filefield($op, &$node, $field, &$file, &$form = NULL) {
  $args = func_get_args();
  switch ($op) {
    case 'file_form':
      $form['file_preview']['#value'] = '';
      $form['url']['#value'] .= '<br />' . t('width: !w; height: !h; duration: !d', array(
        '!w' => $file['width'],
        '!h' => $file['height'],
        '!d' => $file['duration'],
      ));
      $form['width'] = array(
        '#type' => 'hidden',
        '#value' => $file['width'],
      );
      $form['height'] = array(
        '#type' => 'hidden',
        '#value' => $file['height'],
      );
      $form['duration'] = array(
        '#type' => 'hidden',
        '#value' => $file['duration'],
      );
      break;
    case 'file_prepare':
      filefield_meta($file);
      return $file;
      break;
    case 'file_save':
      db_query('INSERT INTO {filefield_meta} (fid, width, height, duration) VALUES (%d, %d, %d, %d)', $file['fid'], $file['width'], $file['height'], $file['duration']);
      break;
    case 'file_delete':
      db_query('DELETE FROM {filefield_meta} WHERE fid = %d', $file['fid']);
      break;
    case 'file_load':
      $meta = db_fetch_array(db_query('SELECT * FROM {filefield_meta} WHERE fid = %d', $file['fid']));
      if ($meta) {
        return $meta;
      }
      break;
  }
}
function filefield_meta(&$file) {
  $info = getid3_analyze($file['filepath']);
  $file['width'] = $file['height'] = $file['duration'] = 0;
  if (isset($info['video']['resolution_x'])) {
    $file['width'] = $info['video']['resolution_x'];
    $file['height'] = $info['video']['resolution_y'];
  }
  else {
    if (isset($info['video']['streams'])) {
      foreach ($info['video']['streams'] as $stream) {
        $file['width'] = max($file['width'], $stream['resolution_x']);
        $file['height'] = max($file['height'], $stream['resolution_y']);
      }
    }
  }
  $file['duration'] = $info['playtime_seconds'];
}

Functions

Constants

Namesort descending Description
FILEFIELD_META_MINIMUM_PHP Implementation of hook_filefield