Index: extensions/DynamicResize/DynamicResize.class.php
===================================================================
--- extensions/DynamicResize/DynamicResize.class.php	(revision 12179)
+++ extensions/DynamicResize/DynamicResize.class.php	(revision 12179)
@@ -0,0 +1,37 @@
+<?php
+class DynamicResize {
+   var $my_config;
+    function load_config()
+    {
+        $x = @file_get_contents( dirname(__FILE__).'/data.dat' );
+        if ($x!==false)
+        {
+            $c = unserialize($x);
+            // do some more tests here
+            $this->my_config = $c;
+        }
+
+        if ( !isset($this->my_config)
+            or empty($this->my_config['DynamicResize']) )
+        {
+            $this->my_config['DynamicResize'] = 'active';
+            $this->save_config();
+        }
+    }
+    function save_config()
+    {
+        $file = fopen( dirname(__FILE__).'/data.dat', 'w' );
+        fwrite($file, serialize($this->my_config) );
+        fclose( $file );
+    }
+    function plugin_admin_menu($menu) {
+        array_push($menu,
+            array(
+                'NAME' => 'DynamicResize',
+                'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin/dynamic_resize_admin.php')
+            )
+        );
+        return $menu;
+    }
+}
+?>
Index: extensions/DynamicResize/data.dat
===================================================================
--- extensions/DynamicResize/data.dat	(revision 12179)
+++ extensions/DynamicResize/data.dat	(revision 12179)
@@ -0,0 +1,1 @@
+a:1:{s:13:"DynamicResize";s:6:"active";}
Index: extensions/DynamicResize/dynamic_resize.tpl
===================================================================
--- extensions/DynamicResize/dynamic_resize.tpl	(revision 12179)
+++ extensions/DynamicResize/dynamic_resize.tpl	(revision 12179)
@@ -0,0 +1,15 @@
+{html_head}
+	{if isset($DYNAMIC_SRC_IMG) & !empty($DYNAMIC_SRC_IMG)}
+		<script type="text/javascript"> var dyn_src_img='{$DYNAMIC_SRC_IMG}';</script>
+	{/if}      
+     	<script type="text/javascript"> 
+		var dyn_mode='{$DYN_MODE}';
+		var next_name='{$NEXT.TITLE}';
+ 		var next_url='{$NEXT.image_url}';
+		var next_width='{$NEXT.width}';
+		var next_height='{$NEXT.height}';
+		var next_dyn_url='{$NEXT_SRC_IMG}';
+	</script>
+        {combine_script id="jquery" load="header"}
+	{combine_script id="xhrpic" load="header" require="jquery" path="/plugins/DynamicResize/scripts/xhrpic.js"  } 
+{/html_head}
Index: extensions/DynamicResize/main.inc.php
===================================================================
--- extensions/DynamicResize/main.inc.php	(revision 12179)
+++ extensions/DynamicResize/main.inc.php	(revision 12179)
@@ -0,0 +1,227 @@
+<?php
+/*
+Plugin Name: DynamicResize
+Version: 0.2
+Description: Allow to redim a picture server-side based on the client display
+Plugin URI: http://fr.piwigo.org/forum/viewtopic.php?id=19304
+*/
+if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
+if (!defined('ROOT_URL')) define('ROOT_URL',  get_root_url().'/' ); // -> http://piwigo.cumulodata.com
+define('DYN_NAME', 'Dynamic Resize');
+define('DYN_PATH', dirname(__FILE__)); // /home/piwigo/webapp/web/plugin/dynamicResize
+define('DYN_ROOT' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__))); // ./plugin/dynamicResize
+define('ROOT_PATH','/home/piwigo/webapp/web/');
+define('PATH', ROOT_URL.DYN_ROOT);
+
+//load_language('plugin.lang', DYN_ROOT);
+
+include_once DYN_ROOT.'/DynamicResize.class.php';
+
+$obj = new DynamicResize();
+$obj->load_config();
+$debug_mode=0;
+$dyn_mode=1; //load in flow, with cookie setting
+//$dyn_mode=2; // load in XHR, wait for 2nd picture.
+
+//NOT_USED// add_event_handler('loc_begin_picture','dynamic_resize_picture_content',50,2);
+add_event_handler('render_element_content','dynamic_resize_picture_content',60 ,2);
+
+// this is the custom  handler that generates the display for the element
+function dynamic_resize_picture_content($content, $element_info){
+ global $conf, $template,$debug_mode, $dyn_mode,$next_srcimg,$new_srcimg,$new_next_srcimg;
+
+ $dyn_mode=1;
+ $debug_mode=0;
+
+//	$conf['dynamicResize'] = explode(',', $conf['dynamicResize']);
+//	$debug_mode = $conf[0];
+//	echo 'debug Mode'.$debug_mode;
+
+
+ //echo 'dyn_mode:'.$dyn_mode;
+ //NOT_USED//$template->set_prefilter('picture', 'dynamicResizeXHR');
+
+ $srcimg = $template->get_template_vars('SRC_IMG');
+ 
+ $next = $template->get_template_vars('next');
+ $current = $template->get_template_vars('current');
+ $new_srcimg='';
+
+//@todo:deal with alternative methods
+// $dynamic_srcimg = DYN_ROOT.'/resize.php?picture_url='.$srcimg;
+// $dynamic_srcimg= DYN_ROOT.'/slir/w801-h553/'.$srcimg; 
+
+ $template->set_filenames(array('dynamicresize' => DYN_PATH . '/dynamic_resize.tpl'));
+ $template->assign(array(
+			'DYN_ROOT' => DYN_ROOT,
+                        'ROOT_URL' => ROOT_URL,
+                  ));
+
+ if($dyn_mode==1){
+	//FLOW MODE
+	
+	//autosize plugin
+	if (isset($_COOKIE["dynamic_width"])) { $max_width=$_COOKIE["dynamic_width"];}
+        if (isset($_COOKIE["dynamic_height"])) { $max_height=$_COOKIE["dynamic_height"];}  	
+	
+        //stripped plugin
+        if (isset($_COOKIE["stripped_max_width"])) { $max_width=$_COOKIE["stripped_max_width"];}
+        if (isset($_COOKIE["stripped_max_height"])) { $max_height=$_COOKIE["stripped_max_height"];}
+		
+	$img_W0=$current['width'];
+        $img_H0=$current['height'];
+
+        $img_ratio=$img_W0/$img_H0;
+
+        if($debug_mode){
+        	echo 'pic :'.$img_W0.'x'.$img_H0.'<br/>';
+        }
+
+	if(isset($max_width) && isset($max_height)){
+
+		if($debug_mode){
+                	echo 'max size :'.$max_width.'x'.$max_height.'<br/>';
+        	}
+
+                if ($img_H0 >= $max_height) {
+			$dynamic_height=$max_height;
+		}else{
+			$dynamic_height=$img_H0;
+		}
+                //TODO // if (img_H <= Hmin) img_H=Hmin;
+
+                $dynamic_width = round($dynamic_height * $img_ratio);
+
+                if ($dynamic_width >  $max_width)  {
+                $dynamic_height = round($max_width / $img_ratio);
+                //if (img_H <= Hmin) {
+                //        img_H = Hmin;
+                //        img_W = parseInt(img_H * img_ratio);
+                //} else {
+                $dynamic_width = $max_width;
+                //}
+                }
+		if($debug_mode){
+			echo 'computed size pic :'.$dynamic_width.'x'.$dynamic_height.'<br/>';
+		}
+
+   //FOR PREFETCHING
+                if(isset($next)){
+			//NEXT PICTURE URL
+                        if(substr($next['image_url'],0,1)=='.') {
+                                $next_srcimg = substr($next['image_url'],1);
+                        }else{
+                                $next_srcimg = $next['image_url'];
+                        }
+
+			if($debug_mode){
+                                echo 'next pic image url : '.$next_srcimg.'<br/>';
+                        }
+			//NEXT PICTURE SIZE
+			$next_W0=$next['width'];
+                	$next_H0=$next['height'];
+                 	//print_r($next);
+
+               		$next_ratio = $next_W0/$next_H0;
+
+            	    	if($debug_mode){
+                	        echo 'next pic :'.$next_W0.'x'.$next_H0.'<br/>';
+            		}
+
+            		if ($next_H0 >= $max_height) {
+                        	$next_dynamic_height=$max_height;
+               		}else{
+                        	$next_dynamic_height=$img_H0;
+             		}
+                	//TODO // if (img_H <= Hmin) img_H=Hmin;
+
+               		$next_dynamic_width = round($next_dynamic_height * $next_ratio);
+
+               		if ($next_dynamic_width >  $max_width)  {
+                	$next_dynamic_height = round($max_width / $next_ratio);
+                	//if (img_H <= Hmin) {
+                //      	  img_H = Hmin;
+                		//img_W = parseInt(img_H * img_ratio);
+               		 //} else {
+               		$next_dynamic_width = $max_width;
+                //	}
+                	}
+                	if($debug_mode){
+                        	echo 'next pic computed size:'.$next_dynamic_width.'x'.$next_dynamic_height.'<br/>';
+                	}
+
+		}
+//height could be wrong on chrome, see http://bugs.jquery.com/ticket/7427
+	}else{
+		if($debug_mode) echo 'no max_width or height info available, downloading original size<br/>';	
+	}
+
+//GETTING PICTURE URL
+
+	if(isset($dynamic_width) && isset($dynamic_height)){
+		//@todo: deal with alternative methods
+		if(substr($srcimg,0,1)=='.'){ $srcimg = substr($srcimg,1);} //remove leading '.' from path
+		$new_srcimg = DYN_ROOT.'/slir/'.'w'.$dynamic_width.'-h'.$dynamic_height.$srcimg;
+		
+		if(isset($next)){
+			if(substr($next['image_url'],0,1)=='.') {
+				$next_srcimg = substr($next['image_url'],1);
+			}else{
+				$next_srcimg = $next['image_url'];
+			}
+			$new_next_srcimg = DYN_ROOT.'/slir/'.'w'.$next_dynamic_width.'-h'.$next_dynamic_height.$next_srcimg;
+		}
+		$template->assign('DYN_MODE', 'FLOW');
+	}else{
+ 		//get the original picture
+		$new_srcimg = $srcimg;
+		if(isset($next)) { $new_next_srcimg = $next_srcimg; }
+		$template->assign('DYN_MODE', 'FLOW0');
+	}
+ }else if ($dyn_mode==2){
+	//XHR MODE
+ 	$new_srcimg = DYN_ROOT.'/images/spacer.gif';
+	$template->assign('DYN_MODE', 'XHR');
+	//@todo : deal with alternative methods
+	//$template->assign('DYNAMIC_SRC_IMG', $dynamic_srcimg);
+	$template->assign('DYNAMIC_SRC_IMG', $srcimg);
+ }else{
+  	$new_srcimg = $srcimg;
+	$template->assign('DYN_MODE', 'STD');
+	if($debug_mode) echo 'Warning, mode should be 1 or 2. doing nothing';
+ }
+
+ if(isset($next)){
+ 	$template->assign('NEXT',$next);
+ 	$template->assign('NEXT_SRC_IMG',$new_next_srcimg);
+ }
+ $template->parse('dynamicresize',false);
+ 
+ $template->assign('SRC_IMG', $new_srcimg);
+ return $template->parse('default_content',true);
+}
+
+//Ajout du prefiltre
+/*
+add_event_handler('loc_begin_picture', 'dynamicResizePF', 65 );
+
+function dynamicResizePF()
+ {
+$path = get_root_url().'plugins/'. basename(dirname(__FILE__)).'/';      
+  global $template;
+	$srcimg = $template->get_template_vars('SRC_IMG');
+
+
+$template->append('head_elements', '
+<script type="text/javascript">
+var js_srcimg ="'.$srcimg.'";
+</script>
+<script type="text/javascript" src="'.$path.'scripts/xhrpic.js"></script>'
+        );
+}
+*/
+
+if($debug_mode==1 && $dyn_mode==2){
+ echo '<div onclick="fire_ret_autosize();">Clickme to fire ret_autosize</div>';
+}
+?>
Index: extensions/DynamicResize/php_errors.log
===================================================================
--- extensions/DynamicResize/php_errors.log	(revision 12179)
+++ extensions/DynamicResize/php_errors.log	(revision 12179)
@@ -0,0 +1,2 @@
+[29-Jan-2011 19:43:14] PHP Parse error:  syntax error, unexpected '}' in /var/www/clients/client1/web7/web/plugins/dynamicResize/resize.php on line 30
+[29-Jan-2011 19:43:43] PHP Parse error:  syntax error, unexpected '}' in /var/www/clients/client1/web7/web/plugins/dynamicResize/resize.php on line 30
Index: extensions/DynamicResize/resize.php
===================================================================
--- extensions/DynamicResize/resize.php	(revision 12179)
+++ extensions/DynamicResize/resize.php	(revision 12179)
@@ -0,0 +1,69 @@
+<?php
+//bool file_exists ( string $filename )
+//take parameters in priority, then, cookie, then ..todo
+
+ $missing_size=false;
+ $debug=false;
+ if (isset($_GET["picture_url"])) { $picture_url = $_GET["picture_url"];} else { die("pictureUrl parameter missing");} 
+ if (isset($_GET["width"])) { $width = $_GET["width"];} else {$width=0;}
+ if (isset($_GET["height"])) { $height = $_GET["height"];} else {$height=0;}
+ if (isset($_COOKIE["dynamic_width"])) { $dynamic_width=$_COOKIE["dynamic_width"];} 
+ if (isset($_COOKIE["dynamic_height"])) { $dynamic_height=$_COOKIE["dynamic_height"];} 
+ if (isset($_GET["dyn_width"])) {$dynamic_width=($_GET["dyn_width"]);}
+ if (isset($_GET["dyn_height"])) {$dynamic_height=($_GET["dyn_height"]);}
+ if (!isset($dynamic_width) && !isset($dynamic_height)) $missing_size=true;
+ if (isset($_GET["debug"])) $debug=$_GET["debug"];
+ $picture_path='/home/piwigo/webapp/web'.substr($picture_url,1);
+ 
+ if($debug){
+	echo "pictureUrl:".$picture_url."<br/>";
+	echo "width:".$width."<br/>";
+	echo "height:".$height."<br/>";
+	echo "dynamic_width:".$dynamic_width."<br/>";
+	echo "dynamic_heigth:".$dynamic_height."<br/>";	
+	echo "picturePath:".$picture_path."<br/>";
+ 
+	list($usec, $sec) = explode(' ', microtime());
+	$script_start= (float) $sec + (float) $usec;
+}
+if($missing_size){
+	die("pictureUrl parameter missing");
+	//@todo: in a next version ,let it go ? (see test below)
+}
+
+if (extension_loaded('imagick')) {
+ 	$image = new Imagick($picture_path);
+ 	if(! $missing_size){
+		$image->adaptiveResizeImage($dynamic_width,$dynamic_height);
+	}
+	$image->setCompression(Imagick::COMPRESSION_JPEG);
+	$image->setCompressionQuality(80);
+	$image->setImageFormat('jpeg'); 
+	
+	$size = $image->getImageLength(); 
+}else{
+	//todo echo "Imagick not loaded, please check your server or contact your hosting service"; 
+	//try alternate method using GD
+}
+
+if(!$debug){
+	header('Content-type: image/jpeg');
+        //header('Content-Length:'.$image->getImageSize());
+	header('Content-Transfer-Encoding :binary');
+	echo $image;
+}else{
+	echo 'imageSize:'.$size.'<br/>';
+	echo 'compression;'.$image->getCompression();
+	echo 'compressionq:'.$image->getCompressionQuality();
+	
+	list($usec, $sec) = explode(' ', microtime());
+  	$script_end = (float) $sec + (float) $usec;
+  	$elapsed_time = round($script_end - $script_start, 5);
+	if($missing_size){
+		echo "No resize data. Please check that cookie has been set. Image <strong>NOT</strong> resized, computed in ".$elapsed_time." s<br/>";
+	}else{
+		echo "Image resized and computed in ".$elapsed_time." s<br/>";
+	}
+}
+
+?>
Index: extensions/DynamicResize/test.php
===================================================================
--- extensions/DynamicResize/test.php	(revision 12179)
+++ extensions/DynamicResize/test.php	(revision 12179)
@@ -0,0 +1,15 @@
+<?php
+$dyn_mode=2; // load in XHR, wait for 2nd picture.
+$debug_mode=true;
+
+#add_event_handler('loc_begin_picture','dynamic_resize_picture_content',50,2);
+//add_event_handler('render_element_content','dynamic_resize_picture_content',60 ,2);
+
+// this is the custom  handler that generates the display for the element
+function dynamic_resize_picture_content($content, $element_info){
+ global $template;
+ global $dyn_mode;
+ echo 'mode:'.$dyn_mode;
+}
+ dynamic_resize_picture_content();
+?>
