Index: modules/node_images/node_images.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_images/node_images.info,v
retrieving revision 1.1
diff -u -p -r1.1 node_images.info
--- modules/node_images/node_images.info	19 Nov 2006 13:36:17 -0000	1.1
+++ modules/node_images/node_images.info	19 May 2007 00:38:04 -0000
@@ -1,3 +1,8 @@
 ; $Id: node_images.info,v 1.1 2006/11/19 13:36:17 stefano73 Exp $
 name = Node images
-description = Allows users to upload images and asociate them to nodes.
\ No newline at end of file
+description = Allows users to upload images and asociate them to nodes.
+; Information added by drupal.org packaging script on 2007-04-24
+version = "5.x-1.x-dev"
+project = "node_images"
+dependencies = upload
+
Index: modules/node_images/node_images.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_images/node_images.module,v
retrieving revision 1.9
diff -u -p -r1.9 node_images.module
--- modules/node_images/node_images.module	4 Feb 2007 03:05:44 -0000	1.9
+++ modules/node_images/node_images.module	19 May 2007 00:38:05 -0000
@@ -308,7 +308,7 @@ function _node_images_edit_form($node) {
 }
 
 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');
 
@@ -507,32 +507,49 @@ function _node_images_upload($edit, $nod
       return FALSE;
     }
 
-    // Scale image
-    $file = _upload_image($file);
-
     // Validation from upload.module
+    $file = _upload_image($file);
     $fid = 'upload_'.$user->uid;
     $node->files = array($fid => $file);
     _upload_validate($node);
     if (!isset($node->files[$fid])) return FALSE;
 
-    // Save uploaded image
-    $dest = _node_images_get_directory();
+    // Normalize filename and check description
+    $destination = file_directory_path().'/'._node_images_get_directory().'/'.preg_replace('/ +/', '_', strtolower($file->filename));
+    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);
-      if ($extension) $file->filename .= '.'.$extension;
-    }
-    if ($file = file_save_upload($file, $dest.'/'.$file->filename)) {
-      $thumb = _node_images_create_thumbnail($file->filepath);
-      $file->filesize = filesize($file->filepath);
+      $destination = preg_replace('/^(.+)\/[^\/]+(\.[^\·]+)$/', '\\1/'.substr(md5($file->filepath), 0, 8).'\\2', $destination);
+    }
+      
+    if ($uploaded = file_save_upload($file, $destination)) {
+    
+      // Secure filename
+      $destination = $uploaded->filepath;
+    
+      // Create thumbnail
+      list($width, $height) = explode('x', variable_get('node_images_thumb_resolution', '100x100'));
+      $destination_tn = preg_replace('/^(.+\/[^\/]+)(\.[^\·]+)$/', '\\1_tn\\2', $destination);
+      $thumb = _node_images_resize($uploaded->filepath, $destination_tn, $width, $height);
+      $thumb->filesize = filesize($thumb->filepath);
+      
+      // Resize original uploaded image
+      if ($size = variable_get('node_images_resize_original', '')) {    
+        list($width, $height) = explode('x', $size);
+        rename($destination, $destination.'_ori');
+        $uploaded = _node_images_resize($destination.'_ori', $destination, $width, $height);
+        unlink($destination.'_ori');
+      }
+
+      // Insert record
       db_query("INSERT INTO {node_images} (nid, uid, filename, filepath, filemime, filesize, thumbpath, thumbsize, weight, description)
         VALUES (%d, %d, '%s', '%s', '%s', %d, '%s', %d, %d, '%s')",
-	$edit['nid'], $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize,
-	$thumb->filepath, $thumb->filesize, $edit['weight'], $edit['description']);
+        $edit['nid'], $user->uid, $uploaded->filename, $uploaded->filepath, $uploaded->filemime, $uploaded->filesize,
+        $thumb->filepath, $thumb->filesize, $edit['weight'], $edit['description']);
     }
-    return $file;
+    
+    return $uploaded;
   }
 }
 
@@ -709,7 +726,7 @@ function node_images_admin_settings() {
   $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'),
     '#after_build' => array('_node_images_check_directory'),
@@ -722,6 +739,14 @@ function node_images_admin_settings() {
     '#maxlength' => 7,
     '#description' => t('The thumbnail size expressed as WIDTHxHEIGHT (e.g. 100x75).'),
   );
+  $form['node_images_resize_original'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Maximum resolution to uploaded image'),
+    '#default_value' => variable_get('node_images_resize_original', ''),
+    '#size' => 19,
+    '#maxlength' => 9,
+    '#description' => t('Resize original uploaded image if greater than specified size. It reduces disk space in server, reescaling hipersized uploaded images. The size expressed as WIDTHxHEIGHT (e.g. 640x480). Left blank to skip resize on uploads.'),
+  );
   $form['node_images_extensions'] = array(
     '#type' => 'textfield',
     '#title' => t('Default permitted image extensions'),
@@ -754,10 +779,6 @@ function node_images_admin_settings() {
  *   The form element containing the name of the directory to check.
  */
 function _node_images_check_settings() {
-  // Make sure upload module is enabled
-  if (!module_exists('upload')) {
-    drupal_set_message(t('node_images.module require upload.module to be enabled.'), 'error');
-  }
 
   // Make sure we've got a working toolkit
   if (!image_get_toolkit()) {
@@ -782,7 +803,7 @@ function _node_images_check_settings() {
   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;
   }
@@ -806,27 +827,24 @@ function _node_images_mkdir_recursive($p
  */
 function _node_images_get_directory() {
   global $user;
-  $path = variable_get('node_images_path', 'files');
+  $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.
+ * Create a new resized image for the uploaded image.
  */
-function _node_images_create_thumbnail($path, $suffix='_tn') {
-  $size = variable_get('node_images_thumb_resolution', '100x100');
-  list($width, $height) = explode('x', $size);
-  $dest_path = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $path, 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;
+function _node_images_resize($src, $des, $w, $h) {
+  if (!image_scale($src, $des, $w, $h)) {
+    copy($src, $des);
+  }  
+  if ($info = image_get_info($des)) {
+    $img = new stdClass();
+    $img->filename = basename($des);
+    $img->filepath = $des;
+    $img->filesize = $info['file_size'];
+    $img->filemime = $info['mime_type'];
+    return $img;
   }
   return NULL;
 }
@@ -883,4 +901,4 @@ function node_images_views_tables() {
  */
 function node_images_views_pre_query() {
   require_once './'. drupal_get_path('module', 'node_images') .'/node_images.views.inc';
-}
\ No newline at end of file
+}
