Index: node_images.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_images/node_images.module,v
retrieving revision 1.9
diff -u -r1.9 node_images.module
--- node_images.module	4 Feb 2007 03:05:44 -0000	1.9
+++ node_images.module	1 Aug 2007 19:13:54 -0000
@@ -308,7 +308,7 @@
 }
 
 function _node_images_edit_form_upload($node) {
-  $path = variable_get('node_images_path', 'files');
+  $path = file_directory_path().'/'.variable_get('node_images_path', 'node_images');
   _node_images_check_directory(NULL, $path);
   $extensions = variable_get('node_images_extensions', 'jpg jpeg gif png');
 
@@ -421,15 +421,14 @@
  * Theme node images list.
  */
 function theme_node_images_list($form) {
-  $header = array(t('Thumbnail'), t('Description'), t('Path'), t('Size'), t('Weight'), t('Delete'));
+  $header = array(t('Thumbnail'), t('Description').' / '.t('Path'), t('Size'), t('Weight'), t('Delete'));
   
   $rows = array();
   foreach($form['images']['#value'] as $id=>$image) {
     $row = array();
     $row[] = '<img src="'.file_create_url($image->thumbpath).'" vspace="5" />';
-    $row[] = drupal_render($form['rows'][$image->id]['description']);
-    $row[] = $image->filepath;
-    $row[] = format_size($image->filesize);
+    $row[] = drupal_render($form['rows'][$image->id]['description']).$image->filepath;
+    $row[] = array('data' => format_size($image->filesize), 'style' => 'white-space: nowrap');
     $row[] = drupal_render($form['rows'][$image->id]['weight']);
     $row[] = array('data' => drupal_render($form['rows'][$image->id]['delete']), 'align' => 'center');
     $rows[] = $row;
@@ -487,6 +486,13 @@
 function _node_images_upload($edit, $node) {
   global $user;
   if (($file = file_check_upload()) && user_access('create node images')) {
+    // The uploaded file must be an image
+    $info = image_get_info($file->filepath);
+    if (!$info) {
+      form_set_error('upload', t('The uploaded file !name is not a valid image.', array('!name' => theme('placeholder', $file->filename))));
+      return FALSE;
+    }
+    
     //Munge the filename as needed for security purposes.
     $file->filename = upload_munge_filename($file->filename, NULL, 0);
 
@@ -503,12 +509,27 @@
     $max = variable_get('node_images_max_images', 4);
     $count = db_num_rows($sql);
     if ($count >= $max) {
-      drupal_set_message(t('The selected file !name can not be uploaded, because it exceeds the maximum limit of !max files.', array('!name' => theme('placeholder', $file->filename), '!max' => theme('placeholder', $max))), 'error');
+      form_set_error('upload', t('The selected file !name can not be uploaded, because it exceeds the maximum limit of !max files.', array('!name' => theme('placeholder', $file->filename), '!max' => theme('placeholder', $max))), 'error');
       return FALSE;
     }
 
-    // Scale image
-    $file = _upload_image($file);
+    // Scale image to the maximum allowed resolution (default is 400x300)
+    // (if not set in the node_images admin settings, use the default upload max resolution
+    list($width, $height) = explode('x', variable_get('node_images_large_resolution', '400x300'));
+    if (!$width || !$height) {
+      list($width, $height) = explode('x', variable_get('upload_max_resolution', '400x300'));
+    }
+    if (!$width || !$height) {
+      list($width, $height) = explode('x', '400x300');
+    }
+    if ($width && $height) {
+      $result = image_scale($file->filepath, $file->filepath, $width, $height);
+      if ($result) {
+        $file->filesize = filesize($file->filepath);
+        drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.',
+          array('%resolution' => $width.'x'.$height)));
+      }
+    }
 
     // Validation from upload.module
     $fid = 'upload_'.$user->uid;
@@ -516,15 +537,21 @@
     _upload_validate($node);
     if (!isset($node->files[$fid])) return FALSE;
 
-    // Save uploaded image
+    // Normalize filename and check description
     $dest = _node_images_get_directory();
+    if (trim($edit['description']) == '') {
+      $edit['description'] = basename($file->filename);
+    }
     if (variable_get('node_images_md5name', FALSE)) {
       // set md5 file name
       $extension = substr($file->filename, strrpos($file->filename, '.') + 1);
-      $file->filename = md5($dest.'/'.$file->filename);
+      $file->filename = substr(md5($dest.'/'.$file->filename), 0, 16);
       if ($extension) $file->filename .= '.'.$extension;
     }
-    if ($file = file_save_upload($file, $dest.'/'.$file->filename)) {
+    $file->filename = preg_replace('/ +/', '_', $file->filename);
+
+    // Save uploaded image
+    if ($file = file_save_upload($file, $dest.'/'.strtolower($file->filename))) {
       $thumb = _node_images_create_thumbnail($file->filepath);
       $file->filesize = filesize($file->filepath);
       db_query("INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, weight, description)
@@ -653,7 +680,7 @@
   list($width, $height) = explode('x', variable_get('upload_max_resolution', 0));
 
   $output = '<table id="slideshow-table"><tr>';
-  $output .= '<td style="width:'.$width.'px;">'.drupal_render($slideshow).'</td>';
+  $output .= '<td>'.drupal_render($slideshow).'</td>';
   $output .= '<td>'.theme('node_images_gallery_thumbs', $thumbs).'</td>';
   $output .= '</tr></table>';
   $output .= '<hr />'.node_view($node);
@@ -673,7 +700,8 @@
       '. l(t('Next'), $_GET['q'], array('class' => 'next', 'rel' => 'nofollow'), 'page='. $element['#status']['next']) .'
     </div>
     <img src="'. url($element['#image']['src']) .'" class="polaroid" />
-    <p class="description">'. $element['#title'] .'</p>';
+    <p class="description">'. $element['#title'] .'</p>
+    </div>';
   return $output;
 }
 
@@ -709,11 +737,19 @@
   $form['node_images_path'] = array(
     '#type' => 'textfield',
     '#title' => t('Node images path'),
-    '#default_value' => variable_get('node_images_path', 'files'),
+    '#default_value' => variable_get('node_images_path', 'node_images'),
     '#maxlength' => 255,
-    '#description' => t('A file system path where the node images will be stored. This directory has to exist and be writable by Drupal. You can use the following variables: %uid, %username'),
+    '#description' => t('Subdirectory in the directory %dir where the node images will be stored. This directory has to exist and be writable by Drupal. You can use the following variables: %uid, %username', array('%dir' => file_directory_path() .'/')),
     '#after_build' => array('_node_images_check_directory'),
   );
+  $form['node_images_large_resolution'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Resolution for large images'),
+    '#default_value' => variable_get('node_images_large_resolution', '400x300'),
+    '#size' => 19,
+    '#maxlength' => 9,
+    '#description' => t('The size for large images in the node gallery, expressed as WIDTHxHEIGHT (e.g. 400x300). If not set, the image will be resized to the maximum allowed resolution in the upload module settings.'),
+  );
   $form['node_images_thumb_resolution'] = array(
     '#type' => 'textfield',
     '#title' => t('Resolution for thumbnails'),
@@ -782,7 +818,7 @@
   global $user;
   $variables = array('%uid' => $user->uid, '%username' => $user->name);
   if ($form_element) {
-    $path = strtr($form_element['#value'], $variables);
+    $path = file_directory_path().'/'.strtr($form_element['#value'], $variables);
     _node_images_mkdir_recursive($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
     return $form_element;
   }
@@ -795,8 +831,9 @@
   $dirs = array();
   foreach($folders as $folder) {
     $dirs[] = $folder;
-    if (!file_check_directory(implode("/", $dirs), $mode, $form_item)) {
-      mkdir(implode("/", $dirs));
+    $dir = implode('/', $dirs);
+    if (!file_check_directory($dir, $mode, $form_item)) {
+      mkdir($dir);
     }
   }
 }
@@ -806,27 +843,29 @@
  */
 function _node_images_get_directory() {
   global $user;
-  $path = variable_get('node_images_path', 'files');
+  $path = file_directory_path().'/'.variable_get('node_images_path', 'node_images');
   return strtr($path, array('%uid' => $user->uid, '%username' => $user->name));
 }
 
 /**
  * Create the thumbnail for the uploaded image.
  */
-function _node_images_create_thumbnail($path, $suffix='_tn') {
+function _node_images_create_thumbnail($source, $suffix = '_tn') {
   $size = variable_get('node_images_thumb_resolution', '100x100');
   list($width, $height) = explode('x', $size);
-  $dest_path = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $path, 1);
+  $dest = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $source, 1);
     
-  if ($size = getimagesize($path)) {
-   image_scale($path, $dest_path, $width, $height);
-   $info = image_get_info($dest_path);
-   $thumb = new stdClass();
-   $thumb->filename = basename($dest_path);
-   $thumb->filepath = $dest_path;
-   $thumb->filesize = $info['file_size'];
-   $thumb->filemime = $info['mime_type'];
-   return $thumb;
+  if ($size = @getimagesize($source)) {
+    if (!image_scale($source, $dest, $width, $height)) {
+      file_copy($source, $dest, FILE_EXISTS_REPLACE);
+    }
+    $info = image_get_info($dest);
+    $thumb = new stdClass();
+    $thumb->filename = basename($dest);
+    $thumb->filepath = $dest;
+    $thumb->filesize = $info['file_size'];
+    $thumb->filemime = $info['mime_type'];
+    return $thumb;
   }
   return NULL;
 }
