Index: video/plugins/video_image/video_image.module =================================================================== --- video.orig/plugins/video_image/video_image.module 2006-09-19 10:52:14.000000000 -0700 +++ video/plugins/video_image/video_image.module 2006-09-19 11:23:46.000000000 -0700 @@ -19,31 +19,79 @@ } } +/** + * Implementation of hook_menu() + */ +function video_image_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/settings/video_image', + 'title' => t('Video_image settings'), + 'description' => t('administer video_image module settings'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('video_image_admin_settings'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); + } + return $items; +} + +/** + * Settings form + */ +function video_image_admin_settings() { + $form = array(); + $form['video_image_promote_thumbnail'] = array( + '#type' => 'checkbox', + '#title' => t('Promote the thumbnails to the front page'), + '#default_value' => variable_get('video_image_promote_thumbnail', true), + ); + $form['video_image_auto_thumbnail'] = array( + '#type' => 'checkbox', + '#title' => t('Auto thumbnail for videos (using mplayer)'), + '#description' => t('This requires setting the path to the mplayer executable below. If set up correctly, this will auto-generate a thumbnail for each video created.'), + '#default_value' => variable_get('video_image_auto_thumbnail', false), + ); + $form['video_image_auto_thumbnail_only'] = array( + '#type' => 'checkbox', + '#title' => t('Use auto-thumbnailer exclusively for video images'), + '#description' => t('If checked, this will disable the file upload box for the user-supplied thumbnail. Only check this if you have checked to be sure that auto-thumbnailing works.'), + '#default_value' => variable_get('video_image_auto_thumbnail_only', false), + ); + $form['video_image_mplayer_path'] = array( + '#type' => 'textfield', + '#title' => t('mplayer executable path'), + '#description' => t('Set the full path to the mplayer executable here to enable automatic thumbnailing of videos'), + '#default_value' => variable_get('video_image_mplayer_path', '/usr/bin/mplayer'), + ); + return system_settings_form($form); +} /** * Implementation of hook_form_alter() */ function video_image_form_alter($form_id, &$form) { - if($form_id == 'video_node_form' && isset($form['video'])) { + if($form_id == 'video_node_form') { - $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.')); + //drupal_set_message(dump($form)); + $node = $form['#node']; + $value = ($node->new_image) ? '#value' : '#default_value'; + $form['iid'] = array('#type' => 'hidden', $value => $node->iid); + + if (function_exists('_image_check_settings')) { + _image_check_settings(); + $form['#attributes'] = array("enctype" => "multipart/form-data"); + if (!variable_get('video_image_auto_thumbnail_only', false)) { + $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.')); - if (function_exists('_image_check_settings')) { - _image_check_settings(); - $node = $form['#node']; - $form['#attributes'] = array("enctype" => "multipart/form-data"); - - if ($node->iid) { - $image = node_load($node->iid); - $form['image']['image_thumbnail'] = array('#type' => 'item', '#title' => t('Thumbnail'), '#value' => image_display($image, 'thumbnail')); - } - $value = ($node->new_image) ? '#value' : '#default_value'; - $form['image']['iid'] = array('#type' => 'hidden' , $value => $node->iid); $form['image']['image'] = array('#type' => 'file', '#title' => t('Image')); - $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => ''); - } + $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => $node->image->image_title); + } + } } } @@ -68,38 +116,49 @@ drupal_set_message(t('video_image module requires the %module module.
To prevent system errors video_image module has been disabled.
Please install the image module and then reactivate the video_image module.', array('%module' => l('image', 'http://drupal.org/project/image'))), 'error'); break; } - $image->title = $_POST['edit']['image_title']; + $field_name = file_check_upload('image'); $image->uid = $node->uid; $image->name = $node->name; $image->created = $node->created; $image->type = 'image'; - image_prepare($image, 'image'); + $image->promote = variable_get('video_image_promote_thumbnail', true); + if (!$field_name && variable_get('video_image_auto_thumbnail', false)) { + $image->title = $_SESSION['video_upload_file']->filename; + $field_name = _video_image_auto_thumbnail($node); + } + else { + $image->title = $_POST['edit']['image_title']; + $field_name = 'image'; + } + image_prepare($image, $field_name); if ($image->images) { node_validate($image); if (!form_get_errors()) { $image = node_submit($image); node_save($image); - $node->iid = $image->nid; + $node->iid = $node->serial_data['iid'] = $image->nid; + $_SESSION['video_upload_file']->iid = $image->nid; $node->new_image = TRUE; } } - elseif ($_POST['edit']['iid']) { - $node->iid = $_POST['edit']['iid']; - } + else if (isset($_SESSION['video_upload_file']->iid)) { + $node->iid = $_SESSION['video_upload_file']->iid; + } break; case 'view': - if($teaser) { - if ($node->serial_data['image_teaser'] || $node->serial_data['iid']) { //If we are dealing with a teaser. - $node->teaser = theme('video_image_teaser', $node); + if ($node->iid) { + if($teaser) { + $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_teaser', $node)); } - } - else { - if ($node->serial_data['image_view'] || $node->serial_data['iid']) { - $node->body = theme('video_image_body', $node) . $node->body; + else { + $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_body', $node)); } } break; + case 'delete': + node_delete(array('nid' => $node->iid)); + break; } } } @@ -150,4 +209,105 @@ return $output; } +function _video_image_get_a_number_or_pid() { + if (function_exists('posix_getpid')) { + return posix_getpid(); + } + mt_srand(microtime()); + return mt_rand(mt_getrandmax()/10); +} + +/* some helper functions for auto-thumbnailing */ +function _video_image_mktmpdir($name) { + $tmp = file_directory_temp(); + // I realize that while this is not completely race-free, it would be a tough nut to crack... + $tmpfile = tempnam($tmp, $name); + if (!$tmpfile) + return false; + $tmpdir = $tmpfile.substr(md5(microtime()._video_image_get_a_number_or_pid()), 0, 8); + $success = mkdir($tmpdir) ? $tmpdir : false; + unlink($tmpfile); + return $success; +} +function _video_image_mktmpfile($name) { + $tmp = file_directory_temp(); + return tempnam($tmp, $name); +} +function _video_image_rmdir_rec($path) { + $dir = opendir($path); + while ($file = readdir($dir)) { + if ($file == '.' || $file == '..') + continue; + $filename = "{$path}/{$file}"; + if (is_dir($filename)) { + _video_image_rmdir_rec($filename); + } else { + unlink($filename); + } + } + closedir($dir); + return rmdir($path); +} + +/* Generates a thumbnail from the video file using mplayer + * + * @param $node + * object with node information + * + * @return + * a drupal file object + */ +function _video_image_auto_thumbnail(&$node) { + if(empty($_SESSION['video_upload_file']) || + !$_SESSION['video_upload_file']->newfile || + $node->iid || $_SESSION['video_upload_file']->iid || + $_SESSION['video_upload_file']->thumbnailed) { + return null; + } + else { + $videofile = $_SESSION['video_upload_file']->filepath; + } + $filepath = _video_image_mktmpfile('mplayer'); + $tmpdir = _video_image_mktmpdir('mplayer'); + if (strchr($tmpdir, ':')) { + /* XXX: the chdir/getcwd crap is because mplayer doesn't know how + * to deal with the c:\ at the front of a windows path when put + * in the suboption list for -vo jpeg */ + $oldcwd = getcwd(); + if (!chdir($tmpdir)) { + drupal_set_message("could not change dir to temp dir", 'error'); + } + $tmpdirarg = ''; + } + else { + $tmpdirarg = ":outdir=$tmpdir"; + } + $mplayer = variable_get('video_image_path_to_mplayer', '/usr/bin/mplayer'); + $command = "$mplayer -ao null -vo jpeg:maxfiles=1:quality=100{$tmpdirarg} -frames 1 $videofile"; + //drupal_set_message("mplayer command: ".$command); + $mplayeroutput = shell_exec($command); + //drupal_set_message("mplayer output: ".$mplayeroutput); + if ($oldcwd) { + chdir($oldcwd); + } + $mplayer_out = $tmpdir.DIRECTORY_SEPARATOR."00000001.jpg"; + if (!file_exists($mplayer_out)) { + drupal_set_message("file $mplayer_out does not exist", 'error'); + //lsfiles(dirname(_acidfree_lockfile())); + } + else { + copy($mplayer_out, $filepath); + // lsfiles(dirname(_acidfree_lockfile())); + } + _video_image_rmdir_rec($tmpdir); + $file = array( + 'filename' => preg_replace("/[^-_ .a-z0-9]/i", "", $node->title) . ".video-thumb.jpg", + 'filemime' => 'image/jpeg', + 'filesize' => filesize($filepath), + 'filepath' => $filepath, + 'nid' => $node->nid, + ); + $_SESSION['video_upload_file']->thumbnailed = TRUE; + return (object)$file; +} ?>