--- C:/Documents and Settings/Boki/Desktop/head/patches/imagefield head/imagefield.module	Wed Jul 25 15:48:18 2007
+++ C:/Documents and Settings/Boki/Desktop/head/patches/thumbnails support/imagefield.module	Thu Jul 26 20:47:24 2007
@@ -1,5 +1,5 @@
 <?php
-// $Id: imagefield.module,v 1.30.2.6.2.22 2007/07/14 22:36:41 dopry Exp $
+// $Id: imagefield.module,v 1.30.2.6.2.21 2007/06/16 06:07:31 dopry Exp $
 
 /**
  * @file
@@ -60,8 +60,14 @@
   foreach ($_SESSION['imagefield'] as $fieldname => $files) {
     foreach ($files as $delta => $file) {
       if ($file['preview'] == $filepath) {
+        if($file['preview_thumbnail'] == TRUE) {
+          file_transfer($file['thumbpath'], array('Content-Type: '. mime_header_encode($file['filemime']),
+                                                 'Content-Length: '. $file['thumbsize']));  
+        }
+        else {
         file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']),
                                                'Content-Length: '. $file['filesize']));
+        }
         exit();
       }
     }
@@ -99,6 +105,7 @@
         'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
         'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
         'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
+        'thumbpath' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
       );
       return $columns;
   }
@@ -109,6 +116,7 @@
     'fid' => 0,
     'title' => '',
     'alt' => '',
+    'thumbpath' => '',
   );
 }
 
@@ -136,6 +144,18 @@
 
   if (imagefield_check_directory($widget_image_path) && $file = file_save_upload((object)$file, $filepath)) {
     $file = (array)$file;
+    
+    $thumbpath = file_create_path($widget_image_path) . '/' . _imagefield_create_thumbname($file['filename']);
+        
+    // Move the thumbnail from the temp folder to the images folder.
+    if(is_file($file['thumbpath'])) {
+      file_move($file['thumbpath'], $thumbpath);
+      $file['thumbpath'] = $thumbpath;
+    }
+    else {
+      $file['thumbpath'] = '';  
+    }
+    
     $file['fid'] = db_next_id('{files}_fid');
     db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)
              VALUES (%d, %d, '%s','%s','%s',%d)",
@@ -278,6 +298,15 @@
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+      $form['thumbnail_resolution'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum resolution for thumbnails'), 
+        '#default_value' => $widget['thumbnail_resolution'] ? $widget['thumbnail_resolution'] : '120x120',
+        '#size' => 15, 
+        '#maxlength' => 10, 
+        '#description' => 
+        t('The maximum thumbnails size resolution expressed as WIDTHxHEIGHT (e.g. 100x50).')
+      );
       $form['image_path'] = array(
         '#type' => 'textfield', 
         '#title' => t('Image path'), 
@@ -353,7 +382,7 @@
       break;
 
     case 'save':
-      return array('max_resolution', 'image_path', 'file_extensions', 'custom_alt', 'custom_title', 'use_default_image', 'default_image');
+      return array('max_resolution', 'thumbnail_resolution', 'image_path', 'file_extensions', 'custom_alt', 'custom_title', 'use_default_image', 'default_image');
   }
 }
 
@@ -431,6 +460,9 @@
       if (is_file($file['filepath'])) {
         file_delete($file['filepath']);
       }
+      if (is_file($file['thumbpath'])) {
+	  file_delete($file['thumbpath']);
+      }
     }
     unset($_SESSION['imagefield'][$fieldname]);
   }
@@ -443,6 +475,7 @@
   else {
     unset($_SESSION['imagefield'][$fieldname][$file['sessionid']]);
   }
+  file_delete($file['thumbpath']);
   return file_delete($file['filepath']);
 }
 
@@ -486,19 +519,52 @@
 
     if ($valid_image) { 
       $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
+      
+      $file['preview_thumbnail'] = TRUE;
+      $file['thumbpath'] = $file['filepath'] . '_thumb';
 
-      // Create the filepath for the image preview
+      // Create the filepath and thumbpath for the image preview
       if (function_exists('token_replace')) {
         // use tokenized paths if token module is installed.
         global $user;
-        $filepath = file_create_filename($file['filename'], file_create_path(token_replace($field['widget']['image_path'], 'user', $user)));
+        $path = file_create_path(token_replace($field['widget']['image_path'], 'user', $user));
       }
       else {
-        $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['image_path']));
+        $path = file_create_path($field['widget']['image_path']);
+      }
+      
+      $filepath = file_create_filename($file['filename'], $path);
+      $thumbpath = file_create_filename(_imagefield_create_thumbname($file['filename']), $path);
+      
+      // Sometimes thumbnail_resolution is empty, if after module update the field settings aren't resaved
+      $thumbnail_resolution = $field['widget']['thumbnail_resolution'] ? $field['widget']['thumbnail_resolution'] : '120x120';
+      list($twidth, $theight) = explode('x', str_replace('X', 'x', $thumbnail_resolution));
+      list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath']);
+      
+       // only attempt to create the thumb if the image is bigger than the thumb size
+      if($twidth < $width && $theight < $height) {
+        // Create the thumbnail and place it in the temp folder
+        if(!image_scale($file['filepath'], $file['thumbpath'], $twidth, $theight)) {
+          drupal_set_message('The thumbnail for ' . $file['filename'] . ' could not be created.', 'error');
+        }
+        else {
+          $file['thumbsize'] = filesize($file['thumbpath']);
+        }
+      }
+      
+      if(!is_file($file['thumbpath'])) {
+        $file['preview_thumbnail'] = FALSE;
+        unset($file['thumbpath']);
       }
 
       $file['fid'] = 'upload';
-      $file['preview'] = $filepath;
+      
+      if($file['preview_thumbnail'] == TRUE) {
+        $file['preview'] = $thumbpath;
+      }
+      else {
+        $file['preview'] = $filepath;
+      }
           
       // If a single field, mark any other images for deletion and delete files in session
       if (!$field['multiple']) {
@@ -607,7 +673,7 @@
         
         $form[$fieldname][$delta]['preview'] = array(
           '#type' => 'markup',
-          '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE),
+          '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE, TRUE),
         );
         
         $form[$fieldname][$delta]['description'] = array(
@@ -668,6 +734,7 @@
       $form[$fieldname][$delta]['filepath'] = array('#type' => 'value',  '#value' => $file['filepath']);
       $form[$fieldname][$delta]['filemime'] = array('#type' => 'value',  '#value' => $file['filemime']);
       $form[$fieldname][$delta]['filesize'] = array('#type' => 'value',  '#value' => $file['filesize']);
+      $form[$fieldname][$delta]['thumbpath'] = array('#type' => 'value',  '#value' => $file['thumbpath']);
       $form[$fieldname][$delta]['fid'] = array('#type' => 'value',  '#value' => $file['fid']);
     }
   }
@@ -814,9 +881,30 @@
   return $output;  
 }
 
-function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
+function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $show_thumb = FALSE) {
   $file = (array)$file;
-  if (is_file($file['filepath']) && (!$getsize || (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
+  
+  if (is_file($file['thumbpath']) && (!$getsize || (list($width, $height, $type, $image_attributes) = @getimagesize($file['thumbpath']))) && $show_thumb == TRUE) {
+     // strings to be added before and after the image tag
+    $prefix = '';
+    $sufix = '';
+  
+    if($file['fid'] != 'upload') {
+      $big_image_url = file_create_url($file['filepath']);
+      $prefix = '<a href="' . $big_image_url . '">';
+      $sufix = '</a>';
+    }
+    
+    $attributes = drupal_attributes($attributes);
+
+    $path = $file['fid'] == 'upload' ? $file['preview'] : $file['thumbpath'];
+    $alt = empty($alt) ? $file['alt'] : $alt;
+    $title = empty($title) ? $file['title'] : $title;
+    
+    $url = file_create_url($path);
+    return $prefix . '<img src="'. check_url($url) .'" alt="'.
+        check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />' . $sufix;
+  } else if (is_file($file['filepath']) && (!$getsize || (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
     $attributes = drupal_attributes($attributes);
     
     $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];
@@ -932,4 +1020,8 @@
   print drupal_to_js(array('status' => TRUE, 'data' => $output));
   exit;
 
+}
+
+function _imagefield_create_thumbname($filename) {
+  return preg_replace('/(.+)\.([a-zA-Z]+)/','${1}_thumbnail.${2}',$filename);
 }
\ No newline at end of file
