source: extensions/DynamicResize/main.inc.php @ 12179

Last change on this file since 12179 was 12179, checked in by arno, 13 years ago

feature : commit initial, suite

File size: 7.4 KB
Line 
1<?php
2/*
3Plugin Name: DynamicResize
4Version: 0.2
5Description: Allow to redim a picture server-side based on the client display
6Plugin URI: http://fr.piwigo.org/forum/viewtopic.php?id=19304
7*/
8if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
9if (!defined('ROOT_URL')) define('ROOT_URL',  get_root_url().'/' ); // -> http://piwigo.cumulodata.com
10define('DYN_NAME', 'Dynamic Resize');
11define('DYN_PATH', dirname(__FILE__)); // /home/piwigo/webapp/web/plugin/dynamicResize
12define('DYN_ROOT' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__))); // ./plugin/dynamicResize
13define('ROOT_PATH','/home/piwigo/webapp/web/');
14define('PATH', ROOT_URL.DYN_ROOT);
15
16//load_language('plugin.lang', DYN_ROOT);
17
18include_once DYN_ROOT.'/DynamicResize.class.php';
19
20$obj = new DynamicResize();
21$obj->load_config();
22$debug_mode=0;
23$dyn_mode=1; //load in flow, with cookie setting
24//$dyn_mode=2; // load in XHR, wait for 2nd picture.
25
26//NOT_USED// add_event_handler('loc_begin_picture','dynamic_resize_picture_content',50,2);
27add_event_handler('render_element_content','dynamic_resize_picture_content',60 ,2);
28
29// this is the custom  handler that generates the display for the element
30function dynamic_resize_picture_content($content, $element_info){
31 global $conf, $template,$debug_mode, $dyn_mode,$next_srcimg,$new_srcimg,$new_next_srcimg;
32
33 $dyn_mode=1;
34 $debug_mode=0;
35
36//      $conf['dynamicResize'] = explode(',', $conf['dynamicResize']);
37//      $debug_mode = $conf[0];
38//      echo 'debug Mode'.$debug_mode;
39
40
41 //echo 'dyn_mode:'.$dyn_mode;
42 //NOT_USED//$template->set_prefilter('picture', 'dynamicResizeXHR');
43
44 $srcimg = $template->get_template_vars('SRC_IMG');
45 
46 $next = $template->get_template_vars('next');
47 $current = $template->get_template_vars('current');
48 $new_srcimg='';
49
50//@todo:deal with alternative methods
51// $dynamic_srcimg = DYN_ROOT.'/resize.php?picture_url='.$srcimg;
52// $dynamic_srcimg= DYN_ROOT.'/slir/w801-h553/'.$srcimg;
53
54 $template->set_filenames(array('dynamicresize' => DYN_PATH . '/dynamic_resize.tpl'));
55 $template->assign(array(
56                        'DYN_ROOT' => DYN_ROOT,
57                        'ROOT_URL' => ROOT_URL,
58                  ));
59
60 if($dyn_mode==1){
61        //FLOW MODE
62       
63        //autosize plugin
64        if (isset($_COOKIE["dynamic_width"])) { $max_width=$_COOKIE["dynamic_width"];}
65        if (isset($_COOKIE["dynamic_height"])) { $max_height=$_COOKIE["dynamic_height"];}       
66       
67        //stripped plugin
68        if (isset($_COOKIE["stripped_max_width"])) { $max_width=$_COOKIE["stripped_max_width"];}
69        if (isset($_COOKIE["stripped_max_height"])) { $max_height=$_COOKIE["stripped_max_height"];}
70               
71        $img_W0=$current['width'];
72        $img_H0=$current['height'];
73
74        $img_ratio=$img_W0/$img_H0;
75
76        if($debug_mode){
77                echo 'pic :'.$img_W0.'x'.$img_H0.'<br/>';
78        }
79
80        if(isset($max_width) && isset($max_height)){
81
82                if($debug_mode){
83                        echo 'max size :'.$max_width.'x'.$max_height.'<br/>';
84                }
85
86                if ($img_H0 >= $max_height) {
87                        $dynamic_height=$max_height;
88                }else{
89                        $dynamic_height=$img_H0;
90                }
91                //TODO // if (img_H <= Hmin) img_H=Hmin;
92
93                $dynamic_width = round($dynamic_height * $img_ratio);
94
95                if ($dynamic_width >  $max_width)  {
96                $dynamic_height = round($max_width / $img_ratio);
97                //if (img_H <= Hmin) {
98                //        img_H = Hmin;
99                //        img_W = parseInt(img_H * img_ratio);
100                //} else {
101                $dynamic_width = $max_width;
102                //}
103                }
104                if($debug_mode){
105                        echo 'computed size pic :'.$dynamic_width.'x'.$dynamic_height.'<br/>';
106                }
107
108   //FOR PREFETCHING
109                if(isset($next)){
110                        //NEXT PICTURE URL
111                        if(substr($next['image_url'],0,1)=='.') {
112                                $next_srcimg = substr($next['image_url'],1);
113                        }else{
114                                $next_srcimg = $next['image_url'];
115                        }
116
117                        if($debug_mode){
118                                echo 'next pic image url : '.$next_srcimg.'<br/>';
119                        }
120                        //NEXT PICTURE SIZE
121                        $next_W0=$next['width'];
122                        $next_H0=$next['height'];
123                        //print_r($next);
124
125                        $next_ratio = $next_W0/$next_H0;
126
127                        if($debug_mode){
128                                echo 'next pic :'.$next_W0.'x'.$next_H0.'<br/>';
129                        }
130
131                        if ($next_H0 >= $max_height) {
132                                $next_dynamic_height=$max_height;
133                        }else{
134                                $next_dynamic_height=$img_H0;
135                        }
136                        //TODO // if (img_H <= Hmin) img_H=Hmin;
137
138                        $next_dynamic_width = round($next_dynamic_height * $next_ratio);
139
140                        if ($next_dynamic_width >  $max_width)  {
141                        $next_dynamic_height = round($max_width / $next_ratio);
142                        //if (img_H <= Hmin) {
143                //                img_H = Hmin;
144                                //img_W = parseInt(img_H * img_ratio);
145                         //} else {
146                        $next_dynamic_width = $max_width;
147                //      }
148                        }
149                        if($debug_mode){
150                                echo 'next pic computed size:'.$next_dynamic_width.'x'.$next_dynamic_height.'<br/>';
151                        }
152
153                }
154//height could be wrong on chrome, see http://bugs.jquery.com/ticket/7427
155        }else{
156                if($debug_mode) echo 'no max_width or height info available, downloading original size<br/>';   
157        }
158
159//GETTING PICTURE URL
160
161        if(isset($dynamic_width) && isset($dynamic_height)){
162                //@todo: deal with alternative methods
163                if(substr($srcimg,0,1)=='.'){ $srcimg = substr($srcimg,1);} //remove leading '.' from path
164                $new_srcimg = DYN_ROOT.'/slir/'.'w'.$dynamic_width.'-h'.$dynamic_height.$srcimg;
165               
166                if(isset($next)){
167                        if(substr($next['image_url'],0,1)=='.') {
168                                $next_srcimg = substr($next['image_url'],1);
169                        }else{
170                                $next_srcimg = $next['image_url'];
171                        }
172                        $new_next_srcimg = DYN_ROOT.'/slir/'.'w'.$next_dynamic_width.'-h'.$next_dynamic_height.$next_srcimg;
173                }
174                $template->assign('DYN_MODE', 'FLOW');
175        }else{
176                //get the original picture
177                $new_srcimg = $srcimg;
178                if(isset($next)) { $new_next_srcimg = $next_srcimg; }
179                $template->assign('DYN_MODE', 'FLOW0');
180        }
181 }else if ($dyn_mode==2){
182        //XHR MODE
183        $new_srcimg = DYN_ROOT.'/images/spacer.gif';
184        $template->assign('DYN_MODE', 'XHR');
185        //@todo : deal with alternative methods
186        //$template->assign('DYNAMIC_SRC_IMG', $dynamic_srcimg);
187        $template->assign('DYNAMIC_SRC_IMG', $srcimg);
188 }else{
189        $new_srcimg = $srcimg;
190        $template->assign('DYN_MODE', 'STD');
191        if($debug_mode) echo 'Warning, mode should be 1 or 2. doing nothing';
192 }
193
194 if(isset($next)){
195        $template->assign('NEXT',$next);
196        $template->assign('NEXT_SRC_IMG',$new_next_srcimg);
197 }
198 $template->parse('dynamicresize',false);
199 
200 $template->assign('SRC_IMG', $new_srcimg);
201 return $template->parse('default_content',true);
202}
203
204//Ajout du prefiltre
205/*
206add_event_handler('loc_begin_picture', 'dynamicResizePF', 65 );
207
208function dynamicResizePF()
209 {
210$path = get_root_url().'plugins/'. basename(dirname(__FILE__)).'/';     
211  global $template;
212        $srcimg = $template->get_template_vars('SRC_IMG');
213
214
215$template->append('head_elements', '
216<script type="text/javascript">
217var js_srcimg ="'.$srcimg.'";
218</script>
219<script type="text/javascript" src="'.$path.'scripts/xhrpic.js"></script>'
220        );
221}
222*/
223
224if($debug_mode==1 && $dyn_mode==2){
225 echo '<div onclick="fire_ret_autosize();">Clickme to fire ret_autosize</div>';
226}
227?>
Note: See TracBrowser for help on using the repository browser.