1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: DynamicResize |
---|
4 | Version: 0.3 |
---|
5 | Description: Allow to redim a picture server-side based on the client display |
---|
6 | Plugin URI: http://fr.piwigo.org/forum/viewtopic.php?id=19304 |
---|
7 | */ |
---|
8 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
9 | if (!defined('ROOT_URL')) define('ROOT_URL', get_root_url().'/' ); // -> http://piwigo.cumulodata.com |
---|
10 | define('DYN_NAME', 'Dynamic Resize'); |
---|
11 | define('DYN_PATH', dirname(__FILE__)); // /home/piwigo/webapp/web/plugin/dynamicResize |
---|
12 | define('DYN_ROOT' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__))); // ./plugin/dynamicResize |
---|
13 | define('ROOT_PATH','/home/piwigo/webapp/web/'); |
---|
14 | define('PATH', ROOT_URL.DYN_ROOT); |
---|
15 | |
---|
16 | //load_language('plugin.lang', DYN_ROOT); |
---|
17 | |
---|
18 | include_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); |
---|
27 | add_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 |
---|
30 | function 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 | $isNext = isset($next) && isset($next['image_url']) && isset($next['width']) && isset($next['height']); |
---|
110 | if($isNext){ |
---|
111 | //NEXT PICTURE URL |
---|
112 | if(substr($next['image_url'],0,1)=='.') { |
---|
113 | $next_srcimg = substr($next['image_url'],1); |
---|
114 | }else{ |
---|
115 | $next_srcimg = $next['image_url']; |
---|
116 | } |
---|
117 | |
---|
118 | if($debug_mode){ |
---|
119 | echo 'next pic image url : '.$next_srcimg.'<br/>'; |
---|
120 | } |
---|
121 | //NEXT PICTURE SIZE |
---|
122 | $next_W0=$next['width']; |
---|
123 | $next_H0=$next['height']; |
---|
124 | //print_r($next); |
---|
125 | |
---|
126 | $next_ratio = $next_W0/$next_H0; |
---|
127 | |
---|
128 | if($debug_mode){ |
---|
129 | echo 'next pic :'.$next_W0.'x'.$next_H0.'<br/>'; |
---|
130 | } |
---|
131 | |
---|
132 | if ($next_H0 >= $max_height) { |
---|
133 | $next_dynamic_height=$max_height; |
---|
134 | }else{ |
---|
135 | $next_dynamic_height=$img_H0; |
---|
136 | } |
---|
137 | //TODO // if (img_H <= Hmin) img_H=Hmin; |
---|
138 | |
---|
139 | $next_dynamic_width = round($next_dynamic_height * $next_ratio); |
---|
140 | |
---|
141 | if ($next_dynamic_width > $max_width) { |
---|
142 | $next_dynamic_height = round($max_width / $next_ratio); |
---|
143 | //if (img_H <= Hmin) { |
---|
144 | // img_H = Hmin; |
---|
145 | //img_W = parseInt(img_H * img_ratio); |
---|
146 | //} else { |
---|
147 | $next_dynamic_width = $max_width; |
---|
148 | // } |
---|
149 | } |
---|
150 | if($debug_mode){ |
---|
151 | echo 'next pic computed size:'.$next_dynamic_width.'x'.$next_dynamic_height.'<br/>'; |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|
155 | //height could be wrong on chrome, see http://bugs.jquery.com/ticket/7427 |
---|
156 | }else{ |
---|
157 | if($debug_mode) echo 'no max_width or height info available, downloading original size<br/>'; |
---|
158 | } |
---|
159 | |
---|
160 | //GETTING PICTURE URL |
---|
161 | |
---|
162 | if(isset($dynamic_width) && isset($dynamic_height)){ |
---|
163 | //@todo: deal with alternative methods |
---|
164 | if(substr($srcimg,0,1)=='.'){ $srcimg = substr($srcimg,1);} //remove leading '.' from path |
---|
165 | $new_srcimg = DYN_ROOT.'/slir/'.'w'.$dynamic_width.'-h'.$dynamic_height.$srcimg; |
---|
166 | |
---|
167 | if($isNext){ |
---|
168 | if(substr($next['image_url'],0,1)=='.') { |
---|
169 | $next_srcimg = substr($next['image_url'],1); |
---|
170 | }else{ |
---|
171 | $next_srcimg = $next['image_url']; |
---|
172 | } |
---|
173 | $new_next_srcimg = DYN_ROOT.'/slir/'.'w'.$next_dynamic_width.'-h'.$next_dynamic_height.$next_srcimg; |
---|
174 | } |
---|
175 | $template->assign('DYN_MODE', 'FLOW'); |
---|
176 | }else{ |
---|
177 | //get the original picture |
---|
178 | $new_srcimg = $srcimg; |
---|
179 | if(isset($next)) { $new_next_srcimg = $next_srcimg; } |
---|
180 | $template->assign('DYN_MODE', 'FLOW0'); |
---|
181 | } |
---|
182 | }else if ($dyn_mode==2){ |
---|
183 | //XHR MODE |
---|
184 | $new_srcimg = DYN_ROOT.'/images/spacer.gif'; |
---|
185 | $template->assign('DYN_MODE', 'XHR'); |
---|
186 | //@todo : deal with alternative methods |
---|
187 | //$template->assign('DYNAMIC_SRC_IMG', $dynamic_srcimg); |
---|
188 | $template->assign('DYNAMIC_SRC_IMG', $srcimg); |
---|
189 | }else{ |
---|
190 | $new_srcimg = $srcimg; |
---|
191 | $template->assign('DYN_MODE', 'STD'); |
---|
192 | if($debug_mode) echo 'Warning, mode should be 1 or 2. doing nothing'; |
---|
193 | } |
---|
194 | |
---|
195 | if(isset($next)){ |
---|
196 | $template->assign('NEXT',$next); |
---|
197 | $template->assign('NEXT_SRC_IMG',$new_next_srcimg); |
---|
198 | } |
---|
199 | $template->parse('dynamicresize',false); |
---|
200 | |
---|
201 | $template->assign('SRC_IMG', $new_srcimg); |
---|
202 | return $template->parse('default_content',true); |
---|
203 | } |
---|
204 | |
---|
205 | //Ajout du prefiltre |
---|
206 | /* |
---|
207 | add_event_handler('loc_begin_picture', 'dynamicResizePF', 65 ); |
---|
208 | |
---|
209 | function dynamicResizePF() |
---|
210 | { |
---|
211 | $path = get_root_url().'plugins/'. basename(dirname(__FILE__)).'/'; |
---|
212 | global $template; |
---|
213 | $srcimg = $template->get_template_vars('SRC_IMG'); |
---|
214 | |
---|
215 | |
---|
216 | $template->append('head_elements', ' |
---|
217 | <script type="text/javascript"> |
---|
218 | var js_srcimg ="'.$srcimg.'"; |
---|
219 | </script> |
---|
220 | <script type="text/javascript" src="'.$path.'scripts/xhrpic.js"></script>' |
---|
221 | ); |
---|
222 | } |
---|
223 | */ |
---|
224 | |
---|
225 | if($debug_mode==1 && $dyn_mode==2){ |
---|
226 | echo '<div onclick="fire_ret_autosize();">Clickme to fire ret_autosize</div>'; |
---|
227 | } |
---|
228 | ?> |
---|