Index: /trunk/admin/include/functions.php
===================================================================
--- /trunk/admin/include/functions.php	(revision 12916)
+++ /trunk/admin/include/functions.php	(revision 12917)
@@ -3,5 +3,5 @@
 // | Piwigo - a PHP based photo gallery                                    |
 // +-----------------------------------------------------------------------+
-// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
 // | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
 // | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
@@ -176,6 +176,4 @@
   }
 
-  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
-  
   $new_ids = array();
 
@@ -184,6 +182,4 @@
     id,
     path,
-    tn_ext,
-    has_high,
     representative_ext
   FROM '.IMAGES_TABLE.'
@@ -197,5 +193,5 @@
       continue;
     }
-    
+
     $files = array();
     $files[] = get_element_path($row);
@@ -216,7 +212,8 @@
       }
     }
-    
+
     if ($ok)
     {
+      delete_element_derivatives($row);
       $new_ids[] += $row['id'];
     }
@@ -389,5 +386,5 @@
 {
   $orphan_tags = get_orphan_tags();
-  
+
   if (count($orphan_tags) > 0)
   {
@@ -413,5 +410,5 @@
 {
   $orphan_tags = array();
-  
+
   $query = '
 SELECT
@@ -1390,5 +1387,5 @@
     return false;
   }
-  
+
   $query = '
 DELETE
@@ -1397,5 +1394,5 @@
 ;';
   pwg_query($query);
-  
+
   $query = '
 DELETE
@@ -2039,5 +2036,5 @@
 {
   $result = pwg_query($query);
-  
+
   $taglist = array();
   while ($row = pwg_db_fetch_assoc($result))
@@ -2058,5 +2055,5 @@
       $row['name'] = preg_replace('#\[lang=(.*?)\](.*?)\[/lang\]#is', null, $row['name']);
     }
-    
+
     if (strlen($row['name']) > 0)
     {
@@ -2070,5 +2067,5 @@
     }
   }
-  
+
   $cmp = create_function('$a,$b', 'return strcasecmp($a["name"], $b["name"]);');
   usort($taglist, $cmp);
@@ -2137,5 +2134,5 @@
     return;
   }
-  
+
   // make sure categories are private and select uppercats or subcats
   $cat_ids = (isset($_POST['apply_on_sub'])) ? implode(',', get_subcat_ids($category_ids)).",".implode(',', get_uppercat_ids($category_ids)) : implode(',', get_uppercat_ids($category_ids));
@@ -2153,5 +2150,5 @@
     return;
   }
-  
+
   // We must not reinsert already existing lines in user_access table
   $granteds = array();
@@ -2160,5 +2157,5 @@
     $granteds[$cat_id] = array();
   }
-  
+
   $query = '
 SELECT
@@ -2176,9 +2173,9 @@
 
   $inserts = array();
-  
+
   foreach ($private_cats as $cat_id)
   {
     $grant_to_users = array_diff($user_ids, $granteds[$cat_id]);
-    
+
     foreach ($grant_to_users as $user_id)
     {
@@ -2208,5 +2205,5 @@
     $status_list[] = 'webmaster';
   }
-  
+
   $query = '
 SELECT
@@ -2246,5 +2243,5 @@
     $pattern .= derivative_to_url($types[0]);
   }
-  
+
   $pattern.='(_[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{3,4}$#';
   if ($contents = opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR))
@@ -2295,5 +2292,5 @@
     }
     closedir($contents);
-    
+
     if ($rmdir)
     {
@@ -2309,15 +2306,21 @@
 }
 
-function delete_element_derivatives($ids)
-{
-  // todo
-  if (!is_array($ids))
-  {
-    $ids = array($ids);
-  }
-
-  // for now I do a massive clear, to be removed once the function is
-  // properly implemented
-  clear_derivative_cache();
+function delete_element_derivatives($infos)
+{
+  $path = $infos['path'];
+  if (!empty($infos['representative_ext']))
+  {
+    $path = original_to_representative( $path, $infos['representative_ext']);
+  }
+  if (substr_compare($path, '../', 0, 3)==0)
+  {
+    $path = substr($path, 3);
+  }
+  $dot = strpos($path, '.');
+  $path = substr_replace($path, '-*', $dot, 0);
+  foreach( glob(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$path) as $file)
+  {
+    @unlink($file);
+  }
 }
 ?>
Index: /trunk/admin/include/functions_upload.inc.php
===================================================================
--- /trunk/admin/include/functions_upload.inc.php	(revision 12916)
+++ /trunk/admin/include/functions_upload.inc.php	(revision 12917)
@@ -3,5 +3,5 @@
 // | Piwigo - a PHP based photo gallery                                    |
 // +-----------------------------------------------------------------------+
-// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
 // | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
 // | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
@@ -37,5 +37,5 @@
       'can_be_null' => false,
       ),
-  
+
     'original_resize_maxwidth' => array(
       'default' => 2000,
@@ -46,5 +46,5 @@
       'error_message' => l10n('The original maximum width must be a number between %d and %d'),
       ),
-  
+
     'original_resize_maxheight' => array(
       'default' => 2000,
@@ -55,5 +55,5 @@
       'error_message' => l10n('The original maximum height must be a number between %d and %d'),
       ),
-  
+
     'original_resize_quality' => array(
       'default' => 95,
@@ -113,5 +113,5 @@
       $max = $upload_form_config[$field]['max'];
       $pattern = $upload_form_config[$field]['pattern'];
-      
+
       if (preg_match($pattern, $value) and $value >= $min and $value <= $max)
       {
@@ -158,8 +158,8 @@
   //
   // 3) register in database
-  
+
   // TODO
   // * check md5sum (already exists?)
-  
+
   global $conf, $user;
 
@@ -172,7 +172,7 @@
     $md5sum = md5_file($source_filepath);
   }
-  
+
   $file_path = null;
-  
+
   if (isset($image_id))
   {
@@ -189,5 +189,5 @@
       $file_path = $row['path'];
     }
-    
+
     if (!isset($file_path))
     {
@@ -201,9 +201,9 @@
   {
     // this photo is new
-    
+
     // current date
     list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
-  
+
     // upload directory hierarchy
     $upload_dir = sprintf(
@@ -251,9 +251,9 @@
     {
       $need_resize = need_resize($file_path, $conf['original_resize_maxwidth'], $conf['original_resize_maxheight']);
-      
+
       if ($need_resize)
       {
         $img = new pwg_image($file_path);
-            
+
         $img->pwg_resize(
           $file_path,
@@ -264,5 +264,5 @@
           false
           );
-        
+
         $img->destroy();
       }
@@ -271,5 +271,5 @@
 
   $file_infos = pwg_image_infos($file_path);
-  
+
   if (isset($image_id))
   {
@@ -282,5 +282,5 @@
       'added_by' => $user['id'],
       );
-    
+
     if (isset($level))
     {
@@ -293,6 +293,4 @@
       array('id' => $image_id)
       );
-
-    delete_element_derivatives($image_id);
   }
   else
@@ -309,5 +307,5 @@
       'added_by' => $user['id'],
       );
-    
+
     if (isset($level))
     {
@@ -316,5 +314,5 @@
 
     single_insert(IMAGES_TABLE, $insert);
-  
+
     $image_id = pwg_db_insert_id(IMAGES_TABLE);
   }
@@ -327,5 +325,5 @@
       );
   }
-  
+
   // update metadata from the uploaded file (exif/iptc)
   if ($conf['use_exif'] and !function_exists('read_exif_data'))
@@ -375,5 +373,5 @@
   // invert width and height.
   list($width, $height) = getimagesize($image_filepath);
-  
+
   if ($width > $max_width or $height > $max_height)
   {
@@ -388,5 +386,5 @@
   list($width, $height) = getimagesize($path);
   $filesize = floor(filesize($path)/1024);
-  
+
   return array(
     'width'  => $width,
@@ -434,5 +432,5 @@
     $size = convert_shorthand_notation_to_bytes($size);
   }
-  
+
   return $size;
 }
@@ -442,5 +440,5 @@
   $suffix = substr($value, -1);
   $multiply_by = null;
-  
+
   if ('K' == $suffix)
   {
@@ -455,5 +453,5 @@
     $multiply_by = 1024*1024*1024;
   }
-  
+
   if (isset($multiply_by))
   {
@@ -500,5 +498,5 @@
     {
       @chmod($conf['upload_dir'], 0777);
-      
+
       if (!is_writable($conf['upload_dir']))
       {
Index: /trunk/include/functions_notification.inc.php
===================================================================
--- /trunk/include/functions_notification.inc.php	(revision 12916)
+++ /trunk/include/functions_notification.inc.php	(revision 12917)
@@ -3,5 +3,5 @@
 // | Piwigo - a PHP based photo gallery                                    |
 // +-----------------------------------------------------------------------+
-// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
 // | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
 // | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
@@ -459,9 +459,8 @@
     { // get some thumbnails ...
       $query = '
-SELECT DISTINCT id, path, name, representative_ext, file
+SELECT DISTINCT i.*
   FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
   '.$where_sql.'
     AND date_available=\''.$dates[$i]['date_available'].'\'
-    AND tn_ext IS NOT NULL
   ORDER BY '.DB_RANDOM_FUNCTION.'()
   LIMIT '.$max_elements.'
