Index: /extensions/derivatives/i.php
===================================================================
--- /extensions/derivatives/i.php	(revision 12777)
+++ /extensions/derivatives/i.php	(revision 12778)
@@ -1,3 +1,23 @@
 <?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based photo gallery                                    |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
 defined('PHPWG_ROOT_PATH') || die('hacking');
 
@@ -58,5 +78,13 @@
     header('Content-Location: '.$url);
     header('Location: '.$url);
-    exit();
+    exit;
+  }
+  if ($code>=400)
+  {
+    $protocol = $_SERVER["SERVER_PROTOCOL"];
+    if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
+      $protocol = 'HTTP/1.0';
+
+    header( "$protocol $code $msg", true, $code );
   }
   //todo improve
@@ -178,4 +206,32 @@
 }
 
+$src_mtime = @filemtime($page['src_path']);
+if ($src_mtime === false)
+{
+  ierror('Source not found', 404);
+}
+
+$need_generate = false;
+$derivative_mtime = @filemtime($page['derivative_path']);
+if ($derivative_mtime === false or
+    $derivative_mtime < $src_mtime or
+    $derivative_mtime < $params->last_mod_time)
+{
+  $need_generate = true;
+}
+
+if (!$need_generate)
+{
+  if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) 
+    and strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $derivative_mtime)
+  {// send the last mod time of the file back
+    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $derivative_mtime).' GMT', true, 304);
+    header('Expires: '.gmdate('D, d M Y H:i:s', time()+10*24*3600).' GMT', true, 304);
+    exit;
+  }
+  // todo send pass-through
+}
+
+
 include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
 $image = new pwg_image($page['src_path']);
@@ -217,6 +273,6 @@
 
 $fstat = fstat($fp);
-$gmt_mtime = gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT';
-header('Last-Modified: '.$gmt_mtime);
+header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT');
+header('Expires: '.gmdate('D, d M Y H:i:s', time()+10*24*3600).' GMT');
 header('Content-length: '.$fstat['size']);
 header('Connection: close');
Index: /extensions/derivatives/include/derivative.inc.php
===================================================================
--- /extensions/derivatives/include/derivative.inc.php	(revision 12777)
+++ /extensions/derivatives/include/derivative.inc.php	(revision 12778)
@@ -1,3 +1,22 @@
 <?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based photo gallery                                    |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
 
 final class SrcImage
@@ -150,5 +169,5 @@
     {
       $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
-      if ($mtime===false)
+      if ($mtime===false or $mtime < $params->last_mod_time)
       {
         $url_style = 2;
Index: /extensions/derivatives/include/derivative_params.inc.php
===================================================================
--- /extensions/derivatives/include/derivative_params.inc.php	(revision 12777)
+++ /extensions/derivatives/include/derivative_params.inc.php	(revision 12778)
@@ -1,3 +1,22 @@
 <?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based photo gallery                                    |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
 
 function size_to_url($s)
@@ -258,4 +277,5 @@
 {
   public $type = IMG_CUSTOM;
+  public $last_mod_time = 0; // used for non-custom images to regenerate the cached files
   public $sizing;
 
