1 | <?php |
---|
2 | |
---|
3 | function image_responsive_insert_head_css($flux) { |
---|
4 | |
---|
5 | $flux .= "\n<link rel='stylesheet' type='text/css' media='all' href='".find_in_path("image_responsive.css")."'>\n"; |
---|
6 | |
---|
7 | return $flux; |
---|
8 | } |
---|
9 | |
---|
10 | function image_responsive_insert_head($flux) { |
---|
11 | $type_urls = lire_meta("type_urls"); |
---|
12 | $htactif = 0; |
---|
13 | if (preg_match(",^(arbo|libres|html|propres|propres2)$,", $type_urls)) { |
---|
14 | $htactif = 1; |
---|
15 | } |
---|
16 | |
---|
17 | $flux .= "<script>htactif=$htactif;document.createElement('picture');</script>"; |
---|
18 | $flux .= " |
---|
19 | <script type='text/javascript' src='".find_in_path("javascript/rAF.js")."'></script> |
---|
20 | <script type='text/javascript' src='".find_in_path("javascript/jquery.smartresize.js")."'></script> |
---|
21 | <script type='text/javascript' src='".find_in_path("javascript/image_responsive.js")."'></script> |
---|
22 | <script type='text/javascript' src='".find_in_path("javascript/picturefill.js")."'></script> |
---|
23 | "; |
---|
24 | |
---|
25 | return $flux; |
---|
26 | } |
---|
27 | |
---|
28 | function image_responsive_header_prive($flux) { |
---|
29 | $flux .= "\n<link rel='stylesheet' type='text/css' media='all' href='".find_in_path("image_responsive.css")."'>\n"; |
---|
30 | $flux .= "<script>htactif=false;document.createElement('picture');</script>"; |
---|
31 | |
---|
32 | $flux .= " |
---|
33 | <script type='text/javascript' src='".find_in_path("javascript/rAF.js")."'></script> |
---|
34 | <script type='text/javascript' src='".find_in_path("javascript/jquery.smartresize.js")."'></script> |
---|
35 | <script type='text/javascript' src='".find_in_path("javascript/image_responsive.js")."'></script> |
---|
36 | <script type='text/javascript' src='".find_in_path("javascript/picturefill.js")."'></script> |
---|
37 | "; |
---|
38 | |
---|
39 | return $flux; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | function _image_responsive($img, $taille=-1, $lazy=0, $vertical = 0, $medias="", $proportions="") { |
---|
44 | $taille_defaut = -1; |
---|
45 | |
---|
46 | if ($taille == -1) { |
---|
47 | $taille_defaut = 120; |
---|
48 | $taille = ""; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | if (preg_match(",^0$|^0\/,", $taille)) { |
---|
53 | $taille_defaut = 0; |
---|
54 | $taille = preg_replace(",^0$|^0\/,", "", $taille); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | $tailles = explode("/", $taille); |
---|
59 | |
---|
60 | if ($taille_defaut < 0) { |
---|
61 | if (count($tailles) > 0) $taille_defaut = $tailles[0]; |
---|
62 | else $taille_defaut = $taille; |
---|
63 | } |
---|
64 | |
---|
65 | // $img = $img[0]; |
---|
66 | $type_urls = lire_meta("type_urls"); |
---|
67 | if (preg_match(",^(arbo|libres|html|propres|propres2)$,", $type_urls)) { |
---|
68 | $htactif = true; |
---|
69 | } |
---|
70 | $source = extraire_attribut($img, "src"); |
---|
71 | $source = preg_replace(",\?[0-9]*$,", "", $source); |
---|
72 | if (file_exists($source)) { |
---|
73 | $l = largeur($source); |
---|
74 | $h = hauteur($source); |
---|
75 | |
---|
76 | $img = vider_attribut($img, "width"); |
---|
77 | $img = vider_attribut($img, "height"); |
---|
78 | $img = vider_attribut($img, "style"); |
---|
79 | |
---|
80 | $alt = extraire_attribut($img, "alt"); |
---|
81 | if (strlen($alt) == 0) $img = inserer_attribut($img, "alt", ""); |
---|
82 | |
---|
83 | |
---|
84 | // Récupérer les proportions et éventuellement recadrer |
---|
85 | $proportions = explode("/", $proportions); |
---|
86 | if (count($proportions) > 0) { |
---|
87 | $i = 0; |
---|
88 | foreach($proportions as $prop) { |
---|
89 | $i++; |
---|
90 | $prop = trim ($prop); |
---|
91 | $regs_l = false; |
---|
92 | $regs_h = false; |
---|
93 | if (preg_match(",^([0-9\.]+\%?)(x([0-9\.]+\%?))?(x([a-z]*))?(x([0-9\.]*))?$,", $prop, $regs)) { |
---|
94 | |
---|
95 | if ($regs[1] == "0") $regs[1] = $l; |
---|
96 | if ($regs[3] == "0") $regs[3] = $h; |
---|
97 | |
---|
98 | $p[$i]["l"] = $regs[1]; |
---|
99 | $p[$i]["h"] = $regs[3]; |
---|
100 | $p[$i]["f"] = $regs[5]; |
---|
101 | $p[$i]["z"] = $regs[7]; |
---|
102 | |
---|
103 | // Gérer les dimensions en pourcentages |
---|
104 | preg_match(",([0-9\.]+)\%$,", $regs[1],$regs_l); |
---|
105 | preg_match(",([0-9\.]+)\%$,", $regs[3],$regs_h); |
---|
106 | |
---|
107 | if ($regs_l[1]>0 OR $regs_h[1]>0) { |
---|
108 | if ($regs_l[1] > 0) $p[$i]["l"] = $l * $regs_l[1] / 100; |
---|
109 | else $p[$i]["l"] = $l; |
---|
110 | if ($regs_h[1] > 0) $p[$i]["h"] = $h * $regs_h[1] / 100; |
---|
111 | else $p[$i]["h"] = $h; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | if (!$regs[5]) $p[$i]["f"] = "center"; |
---|
116 | if (!$regs[7]) $p[$i]["z"] = 1; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | if (count($p) == 1) { |
---|
121 | $source = image_proportions($source, $p[1]["l"], $p[1]["h"], $p[1]["f"], $p[1]["z"]); |
---|
122 | $source = extraire_attribut($source,"src"); |
---|
123 | } |
---|
124 | |
---|
125 | $medias = explode("/", $medias); |
---|
126 | if (count($p) > 1) { |
---|
127 | $i = 0; |
---|
128 | foreach($tailles as $t) { |
---|
129 | $m = trim($medias[$i]); |
---|
130 | $i++; |
---|
131 | if (count($p[$i]) > 1) { |
---|
132 | $pad_bot_styles[$m] = "padding-bottom:" .(($p[$i]["h"]/$p[$i]["l"])*100)."%!important"; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | //$img = inserer_attribut($img, "src", $src); |
---|
138 | $img = inserer_attribut($img, "data-src", $source); |
---|
139 | $classe = "image_responsive"; |
---|
140 | |
---|
141 | if ($vertical == 1) { |
---|
142 | $classe .= " image_responsive_v"; |
---|
143 | $v = "v"; |
---|
144 | if ($h < $taille_defaut) $taille_defaut = $h; |
---|
145 | } else { |
---|
146 | $v = ""; |
---|
147 | if ($l < $taille_defaut) $taille_defaut = $l; |
---|
148 | } |
---|
149 | |
---|
150 | if ($taille_defaut == 0) { |
---|
151 | $src = find_in_path("rien.gif"); |
---|
152 | } else { |
---|
153 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
154 | $src = retour_image_responsive($source, $taille_defaut, 1, 0, "file"); |
---|
155 | } else { |
---|
156 | if ($htactif) { |
---|
157 | $src = preg_replace(",\.(jpg|png|gif)$,", "-resp$taille_defaut$v.$1", $source); |
---|
158 | } |
---|
159 | else { |
---|
160 | $src = "index.php?action=image_responsive&img=$source&taille=$taille_defaut$v"; |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | if ($lazy == 1) $classe .= " lazy"; |
---|
166 | $img = inserer_attribut($img, "data-l", $l); |
---|
167 | $img = inserer_attribut($img, "data-h", $h); |
---|
168 | |
---|
169 | // Gérer les tailles autorisées |
---|
170 | if (count($tailles) > 0) { |
---|
171 | sort($tailles); |
---|
172 | include_spip("inc/json"); |
---|
173 | |
---|
174 | $img = inserer_attribut($img, "data-tailles", addslashes(json_encode($tailles))); |
---|
175 | |
---|
176 | |
---|
177 | $i = 0; |
---|
178 | |
---|
179 | foreach($tailles as $t) { |
---|
180 | $m = trim($medias[$i]); |
---|
181 | $i++; |
---|
182 | $source_tmp = $source; |
---|
183 | |
---|
184 | if (count($p) > 1 && count($p[$i]) > 1) { |
---|
185 | $source_tmp = image_proportions($source_tmp, $p[$i]["l"], $p[$i]["h"], $p[$i]["f"], $p[$i]["z"]); |
---|
186 | $source_tmp = extraire_attribut($source_tmp,"src"); |
---|
187 | } |
---|
188 | |
---|
189 | if ($vertical && $t > $h) $t = $h; |
---|
190 | else if (!$vertical && $t > $l) $t = $l; |
---|
191 | |
---|
192 | |
---|
193 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
194 | $fichiers[$t][1] = retour_image_responsive($source_tmp, "$t$v", 1, 0, "file"); |
---|
195 | $fichiers[$t][2] = retour_image_responsive($source_tmp, "$t$v", 2, 0, "file"); |
---|
196 | } else { |
---|
197 | if ($htactif) { |
---|
198 | $fichiers[$t][1] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t$v.$1", $source_tmp); |
---|
199 | $fichiers[$t][2] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t$v-2.$1", $source_tmp); |
---|
200 | } |
---|
201 | else { |
---|
202 | $fichiers[$t][1] = "index.php?action=image_responsive&img=$source_tmp&taille=$t$v"; |
---|
203 | $fichiers[$t][2] = "index.php?action=image_responsive&img=$source_tmp&taille=$t$v&dpr=2"; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | // Fabriquer automatiquement un srcset s'il n'y a qu'une seule taille d'image (pour 1x et 2x) |
---|
210 | if (count($tailles) == 1 && $lazy != 1) { // Pas de srcset sur les images lazy |
---|
211 | $t = $tailles[0]; |
---|
212 | if ($t != 0 && $t <= $l) { |
---|
213 | $srcset[] = $fichiers[$t][1]." 1x"; |
---|
214 | $srcset[] = $fichiers[$t][2]." 2x"; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | // Fabriquer des <source> s'il y a plus d'une taille associée à des sizes |
---|
220 | if (count($tailles) > 1) { |
---|
221 | if (count($tailles) == count($medias) && $lazy != 1) { |
---|
222 | $i = 0; |
---|
223 | foreach($tailles as $t) { |
---|
224 | $m = trim($medias[$i]); |
---|
225 | $i++; |
---|
226 | |
---|
227 | if ($vertical) $t = min($t, $h); |
---|
228 | else $t = min ($t, $l); |
---|
229 | |
---|
230 | |
---|
231 | $source_tmp = $source; |
---|
232 | $set = $fichiers[$t][1]." 1x"; |
---|
233 | $set .= ",".$fichiers[$t][2]. " 2x"; |
---|
234 | |
---|
235 | if (strlen($m) > 0) { |
---|
236 | $insm = " media='$m'"; |
---|
237 | $sources .= "<source$insm srcset='$set'>"; |
---|
238 | } else { |
---|
239 | |
---|
240 | //$sources .= "<source srcset='$set'>"; |
---|
241 | //$set = find_in_path("rien.gif"); |
---|
242 | $srcset[] = $set; |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | } |
---|
247 | } else { |
---|
248 | // Tailles déterminées, pas de @media |
---|
249 | // dans le cas où l'on force précalcule |
---|
250 | foreach($tailles as $t) { |
---|
251 | if ($vertical && $t > $h) $t = $h; |
---|
252 | else if (!$vertical && $t > $l) $t = $l; |
---|
253 | |
---|
254 | |
---|
255 | $autorisees[$t][1] = $fichiers[$t][1]; |
---|
256 | $autorisees[$t][2] = $fichiers[$t][2]; |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | // Gérer le srcset |
---|
264 | if ($sources || $srcset) $classe .= " avec_picturefill"; |
---|
265 | |
---|
266 | if ($autorisees) { |
---|
267 | $autorisees = json_encode($autorisees); |
---|
268 | $img = inserer_attribut($img, "data-autorisees", $autorisees); |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | $img = inserer_attribut($img, "src", $src); |
---|
273 | if ($lazy) $img = inserer_attribut($img, "data-src-lazy", $src); |
---|
274 | |
---|
275 | $img = inserer_attribut($img, "class", $classe); |
---|
276 | if ($srcset) { |
---|
277 | $srcset = join($srcset, ","); |
---|
278 | $img = inserer_attribut($img, "srcset", $srcset); |
---|
279 | } |
---|
280 | |
---|
281 | if ($sources) { |
---|
282 | $sources = "<!--[if IE 9]><video style='display: none;'><![endif]-->$sources<!--[if IE 9]></video><![endif]-->"; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | if ($pad_bot_styles) { |
---|
287 | |
---|
288 | ksort($pad_bot_styles); |
---|
289 | |
---|
290 | foreach($pad_bot_styles as $m=>$pad) { |
---|
291 | $style = "##classe##{".$pad."}"; |
---|
292 | if ($m) $style = "\n@media $m {".$style."}"; |
---|
293 | $styles .= $style; |
---|
294 | } |
---|
295 | $styles = "<style>$styles</style>"; |
---|
296 | $nom_class = "class".md5($styles); |
---|
297 | $styles = str_replace("##classe##", "picture.".$nom_class, $styles); |
---|
298 | // pour affichage dans la classe de picture |
---|
299 | $nom_class = " ".$nom_class; |
---|
300 | } |
---|
301 | |
---|
302 | if ($vertical == 0) { |
---|
303 | if (count($p) == 1) $r = ($p[1]["h"]/$p[1]["l"]) * 100; |
---|
304 | else if (count($p) == 0) $r = (($h/$l)*100); |
---|
305 | |
---|
306 | if ($r) $aff_r = "padding-bottom:$r%"; |
---|
307 | $img = "<picture style='padding:0;$aff_r' class='conteneur_image_responsive_h$nom_class'>$sources$img</picture>"; |
---|
308 | } else { |
---|
309 | $r = (($h/$l)*100); |
---|
310 | $img = "<picture class='conteneur_image_responsive_v$nom_class'>$sources$img</picture>"; |
---|
311 | } |
---|
312 | $img = $img.$styles; |
---|
313 | } |
---|
314 | |
---|
315 | if (_SPIP_LIER_RESSOURCES && $fichiers) { |
---|
316 | foreach($fichiers as $f) { |
---|
317 | $img .= "<link href='".$f[1]."' rel='attachment' property='url'>" |
---|
318 | ."<link href='".$f[2]."' rel='attachment' property='url'>"; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | return $img; |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | |
---|
328 | |
---|
329 | function image_responsive($texte, $taille=-1, $lazy=0, $vertical=0, $medias='', $proportions='') { |
---|
330 | if (!preg_match("/^<img /i", $texte)) { |
---|
331 | if (strlen($texte) < 256 && file_exists($texte)) $texte = "<img src='$texte'>"; |
---|
332 | else return $texte; |
---|
333 | } |
---|
334 | return preg_replace_callback(",(<img\ [^>]*>),", create_function('$matches', 'return _image_responsive($matches[0],"'.$taille.'",'.$lazy.','.$vertical.',"'.$medias.'","'.$proportions.'");'), $texte); |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | function image_responsive_svg($img, $taille=-1, $lazy=0, $vertical=0) { |
---|
339 | $taille_defaut = -1; |
---|
340 | |
---|
341 | if ($taille == -1) { |
---|
342 | $taille_defaut = 120; |
---|
343 | $taille = ""; |
---|
344 | } |
---|
345 | if (preg_match(",^0$|^0\/,", $taille)) { |
---|
346 | $taille_defaut = 0; |
---|
347 | $taille = preg_replace(",^0$|^0\/,", "", $taille); |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | $tailles = explode("/", $taille); |
---|
352 | |
---|
353 | if ($taille_defaut < 0) { |
---|
354 | if (count($tailles) > 0) $taille_defaut = $tailles[0]; |
---|
355 | else $taille_defaut = $taille; |
---|
356 | } |
---|
357 | |
---|
358 | $type_urls = lire_meta("type_urls"); |
---|
359 | if (preg_match(",^(arbo|libres|html|propres|propres2)$,", $type_urls)) { |
---|
360 | $htactif = true; |
---|
361 | } |
---|
362 | |
---|
363 | |
---|
364 | if (preg_match("/^<img /i", $img)) { |
---|
365 | $img = extraire_attribut($img, "src"); |
---|
366 | } |
---|
367 | $img = preg_replace(",\?[0-9]*$,", "", $img); |
---|
368 | |
---|
369 | $source = $img; |
---|
370 | |
---|
371 | $classe = "image_responsive_svg"; |
---|
372 | |
---|
373 | |
---|
374 | if (file_exists($source)) { |
---|
375 | $l = largeur($source); |
---|
376 | $h = hauteur($source); |
---|
377 | if ($vertical == 1) { |
---|
378 | $classe .= " image_responsive_svg_v"; |
---|
379 | $v = "v"; |
---|
380 | if ($h < $taille_defaut) $taille_defaut = $h; |
---|
381 | } else { |
---|
382 | $v = ""; |
---|
383 | if ($l < $taille_defaut) $taille_defaut = $l; |
---|
384 | } |
---|
385 | if ($taille_defaut == 0) { |
---|
386 | $src = find_in_path("rien.gif"); |
---|
387 | } else { |
---|
388 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
389 | $src = retour_image_responsive($source, $taille_defaut, 1, 0, "file"); |
---|
390 | } else { |
---|
391 | if ($htactif) { |
---|
392 | $src = preg_replace(",\.(jpg|png|gif)$,", "-resp$taille_defaut$v.$1", $source); |
---|
393 | } |
---|
394 | else { |
---|
395 | $src = "index.php?action=image_responsive&img=$source&taille=$taille_defaut$v"; |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | if ($lazy == 1) $classe .= " lazy"; |
---|
401 | |
---|
402 | if (count($tailles) > 0) { |
---|
403 | sort($tailles); |
---|
404 | include_spip("inc/json"); |
---|
405 | |
---|
406 | $data_tailles = " data-tailles='".addslashes(json_encode($tailles))."'"; |
---|
407 | |
---|
408 | |
---|
409 | $i = 0; |
---|
410 | |
---|
411 | foreach($tailles as $t) { |
---|
412 | $m = trim($medias[$i]); |
---|
413 | $i++; |
---|
414 | $source_tmp = $source; |
---|
415 | |
---|
416 | if (count($p) > 1 && count($p[$i]) > 1) { |
---|
417 | $source_tmp = image_proportions($source_tmp, $p[$i]["l"], $p[$i]["h"], $p[$i]["f"], $p[$i]["z"]); |
---|
418 | $source_tmp = extraire_attribut($source_tmp,"src"); |
---|
419 | } |
---|
420 | |
---|
421 | if ($vertical && $t > $h) $t = $h; |
---|
422 | else if (!$vertical && $t > $l) $t = $l; |
---|
423 | |
---|
424 | |
---|
425 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
426 | $fichiers[$t][1] = retour_image_responsive($source_tmp, "$t$v", 1, 0, "file"); |
---|
427 | $fichiers[$t][2] = retour_image_responsive($source_tmp, "$t$v", 2, 0, "file"); |
---|
428 | } else { |
---|
429 | if ($htactif) { |
---|
430 | $fichiers[$t][1] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t$v.$1", $source_tmp); |
---|
431 | $fichiers[$t][2] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t$v-2.$1", $source_tmp); |
---|
432 | } |
---|
433 | else { |
---|
434 | $fichiers[$t][1] = "index.php?action=image_responsive&img=$source_tmp&taille=$t$v"; |
---|
435 | $fichiers[$t][2] = "index.php?action=image_responsive&img=$source_tmp&taille=$t$v&dpr=2"; |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | } |
---|
440 | foreach($tailles as $t) { |
---|
441 | if ($vertical && $t > $h) $t = $h; |
---|
442 | else if (!$vertical && $t > $l) $t = $l; |
---|
443 | |
---|
444 | |
---|
445 | $autorisees[$t][1] = $fichiers[$t][1]; |
---|
446 | $autorisees[$t][2] = $fichiers[$t][2]; |
---|
447 | } |
---|
448 | |
---|
449 | if ($autorisees) { |
---|
450 | $data_autorisees = " data-autorisees='".json_encode($autorisees)."'"; |
---|
451 | } |
---|
452 | |
---|
453 | } |
---|
454 | |
---|
455 | $ret = "<image class='$classe' width='100%' height='100%' xlink:href='$src' data-src='$source' data-l='$l' data-h='$h'$data_tailles$data_autorisees></image>"; |
---|
456 | |
---|
457 | } |
---|
458 | |
---|
459 | |
---|
460 | |
---|
461 | return $ret; |
---|
462 | } |
---|
463 | |
---|
464 | |
---|
465 | function background_responsive($src, $taille=120, $lazy=0, $align="") { |
---|
466 | |
---|
467 | if (preg_match("/^<img /i", $src)) { |
---|
468 | $src = extraire_attribut($src, "src"); |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | |
---|
473 | $tailles = explode("/", $taille); |
---|
474 | if (count($tailles) > 1) $taille_defaut = $tailles[0]; |
---|
475 | else $taille_defaut = $taille; |
---|
476 | |
---|
477 | // $img = $img[0]; |
---|
478 | $type_urls = lire_meta("type_urls"); |
---|
479 | if (preg_match(",^(arbo|libres|html|propres|propres2)$,", $type_urls)) { |
---|
480 | $htactif = true; |
---|
481 | } |
---|
482 | $src = preg_replace(",\?[0-9]*$,", "", $src); |
---|
483 | |
---|
484 | if (file_exists($src)) { |
---|
485 | include_spip("filtres/images_transforme"); |
---|
486 | |
---|
487 | $l = largeur($src); |
---|
488 | $h = hauteur($src); |
---|
489 | |
---|
490 | $mtime = filemtime($src); |
---|
491 | $ins .= " data-mtime='$mtime'"; |
---|
492 | |
---|
493 | |
---|
494 | $img = $src; |
---|
495 | |
---|
496 | if ($l > $h) { |
---|
497 | $ins .= " data-italien-src='$src'"; |
---|
498 | $ins .= " data-italien-l='$l'"; |
---|
499 | $ins .= " data-italien-h='$h'"; |
---|
500 | |
---|
501 | $srcp = image_proportions($srcp, 3, 4, $align); |
---|
502 | $srcp = image_reduire($src, 0, 2400); |
---|
503 | $srcp = extraire_attribut($srcp, "src"); |
---|
504 | $lp = largeur($srcp); |
---|
505 | $hp = hauteur($srcp); |
---|
506 | |
---|
507 | $ins .= " data-portrait-src='$srcp'"; |
---|
508 | $ins .= " data-portrait-l='$lp'"; |
---|
509 | $ins .= " data-portrait-h='$hp'"; |
---|
510 | |
---|
511 | $l_italien = $l; |
---|
512 | $s_italien = $src; |
---|
513 | $l_portrait = $lp; |
---|
514 | $s_portrait = $srcp; |
---|
515 | |
---|
516 | } else { |
---|
517 | $ins .= " data-portrait-src='$src'"; |
---|
518 | $ins .= " data-portrait-l='$l'"; |
---|
519 | $ins .= " data-portrait-h='$h'"; |
---|
520 | |
---|
521 | |
---|
522 | $srcp = image_proportions($srcp, 4, 3, $align); |
---|
523 | $srcp = image_reduire($src, 2400, 0); |
---|
524 | $srcp = extraire_attribut($srcp, "src"); |
---|
525 | $lp = largeur($srcp); |
---|
526 | $hp = hauteur($srcp); |
---|
527 | |
---|
528 | $ins .= " data-italien-src='$srcp'"; |
---|
529 | $ins .= " data-italien-l='$lp'"; |
---|
530 | $ins .= " data-italien-h='$hp'"; |
---|
531 | |
---|
532 | $l_italien = $lp; |
---|
533 | $s_italien = $srcp; |
---|
534 | $l_portrait = $l; |
---|
535 | $s_portrait = $src; |
---|
536 | |
---|
537 | } |
---|
538 | |
---|
539 | $ins .= " data-responsive='background'"; |
---|
540 | |
---|
541 | |
---|
542 | if ($l < $taille_defaut) $taille_defaut = $l; |
---|
543 | $v = ""; |
---|
544 | |
---|
545 | |
---|
546 | if ($htactif) { |
---|
547 | $src = preg_replace(",\.(jpg|png|gif)$,", "-resp$taille_defaut$v.$1", $src); |
---|
548 | } |
---|
549 | else { |
---|
550 | $src = "index.php?action=image_responsive&img=$src&taille=$taille_defaut$v"; |
---|
551 | } |
---|
552 | |
---|
553 | |
---|
554 | if ($taille_defaut == 0) $src = find_in_path("rien.gif"); |
---|
555 | if ($lazy == 1) $ins .= " data-lazy='lazy'"; |
---|
556 | |
---|
557 | if ($class) $ins .= " class='$class'"; |
---|
558 | |
---|
559 | if (count($tailles) > 1) { |
---|
560 | sort($tailles); |
---|
561 | include_spip("inc/json"); |
---|
562 | |
---|
563 | |
---|
564 | foreach($tailles as $t) { |
---|
565 | |
---|
566 | $t_italien = min($t, $l_italien); |
---|
567 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
568 | $fichiers[$t_italien]["i"][1] = retour_image_responsive($s_italien, "$t_italien", 1, 0, "file"); |
---|
569 | $fichiers[$t_italien]["i"][2] = retour_image_responsive($s_italien, "$t_italien", 2, 0, "file"); |
---|
570 | } else { |
---|
571 | if ($htactif) { |
---|
572 | $fichiers[$t_italien]["i"][1] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t_italien.$1", $s_italien)."?$mtime"; |
---|
573 | $fichiers[$t_italien]["i"][2] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t_italien-2.$1", $s_italien)."?$mtime"; |
---|
574 | } |
---|
575 | else { |
---|
576 | $fichiers[$t_italien]["i"][1] = "index.php?action=image_responsive&img=$s_italien&taille=$t_italien&mtime=$mtime"; |
---|
577 | $fichiers[$t_italien]["i"][2] = "index.php?action=image_responsive&img=$s_italien&taille=$t_italien&dpr=2&mtime=$mtime"; |
---|
578 | } |
---|
579 | } |
---|
580 | |
---|
581 | $t_portrait = min($t, $l_portrait); |
---|
582 | if(_IMAGE_RESPONSIVE_CALCULER) { |
---|
583 | $fichiers[$t_portrait]["p"][1] = retour_image_responsive($s_portrait, "$t_portrait", 1, 0, "file"); |
---|
584 | $fichiers[$t_portrait]["p"][2] = retour_image_responsive($s_portrait, "$t_portrait", 2, 0, "file"); |
---|
585 | } else { |
---|
586 | if ($htactif) { |
---|
587 | $fichiers[$t_portrait]["p"][1] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t_portrait.$1", $s_portrait)."?$mtime"; |
---|
588 | $fichiers[$t_portrait]["p"][2] = preg_replace(",\.(jpg|png|gif)$,", "-resp$t_portrait-2.$1", $s_portrait)."?$mtime"; |
---|
589 | } |
---|
590 | else { |
---|
591 | $fichiers[$t_portrait]["p"][1] = "index.php?action=image_responsive&img=$s_portrait&taille=$t_portrait&mtime=$mtime"; |
---|
592 | $fichiers[$t_portrait]["p"][2] = "index.php?action=image_responsive&img=$s_portrait&taille=$t_portrait&dpr=2&mtime=$mtime"; |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | } |
---|
597 | |
---|
598 | $ins .= " data-tailles='".addslashes(json_encode($tailles)) ."'"; |
---|
599 | $ins .= " data-autorisees='".addslashes(json_encode($fichiers)) ."'"; |
---|
600 | |
---|
601 | if (_SPIP_LIER_RESSOURCES && $fichiers) { |
---|
602 | foreach($fichiers as $f) { |
---|
603 | $links .= "background-image:url(".$f["i"][1].");" |
---|
604 | ."background-image:url(".$f["i"][2].");" |
---|
605 | ."background-image:url(".$f["p"][1].");" |
---|
606 | ."background-image:url(".$f["p"][2].");"; |
---|
607 | } |
---|
608 | } |
---|
609 | |
---|
610 | if ($align) { |
---|
611 | if ($align == "focus") { |
---|
612 | $style_align = "background-position: ".(centre_image_x($img)*100)."% ".(centre_image_y($img)*100)."%;"; |
---|
613 | } else { |
---|
614 | $style_align = "background-position: $align;"; |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | } |
---|
619 | |
---|
620 | $ins .= " style='".$style_align.$links."background-image:url($src)'"; |
---|
621 | |
---|
622 | return $ins; |
---|
623 | } |
---|
624 | |
---|
625 | |
---|
626 | } |
---|
627 | |
---|
628 | |
---|
629 | function image_proportions($img, $largeur=16, $hauteur=9, $align="center", $zoom=1) { |
---|
630 | $mode = $align; |
---|
631 | |
---|
632 | if (!$img) return; |
---|
633 | |
---|
634 | |
---|
635 | |
---|
636 | $l_img = largeur ($img); |
---|
637 | $h_img = hauteur($img); |
---|
638 | |
---|
639 | if ($largeur == 0 OR $hauteur == 0) { |
---|
640 | $largeur = $l_img; |
---|
641 | $hauteur = $h_img; |
---|
642 | } |
---|
643 | |
---|
644 | |
---|
645 | if ($l_img == 0 OR $h_img == 0) return $img; |
---|
646 | |
---|
647 | $r_img = $h_img / $l_img; |
---|
648 | $r = $hauteur / $largeur; |
---|
649 | |
---|
650 | if ($r_img < $r) { |
---|
651 | $l_dest = $h_img/$r; |
---|
652 | $h_dest = $h_img; |
---|
653 | } else if ($r_img > $r) { |
---|
654 | $l_dest = $l_img; |
---|
655 | $h_dest = $l_img*$r; |
---|
656 | } |
---|
657 | |
---|
658 | |
---|
659 | // Si align est "focus" ou "focus-center", on va aller chercher le «point d'intérêt» de l'image |
---|
660 | // avec la fonction centre_image du plugin «centre_image» |
---|
661 | |
---|
662 | // Avec "focus", point d'intérêt reste décentré |
---|
663 | // Avec "focus-center", point d'intérêt aussi centré que possible |
---|
664 | if (($align == "focus" || $align == "focus-center") && function_exists('centre_image')) { |
---|
665 | $dx = centre_image_x($img); |
---|
666 | $dy = centre_image_y($img); |
---|
667 | |
---|
668 | if ($r_img > $r) { |
---|
669 | $h_dest = round(($l_img * $r)/$zoom); |
---|
670 | $l_dest = round($l_img/$zoom); |
---|
671 | } else { |
---|
672 | $h_dest = round($h_img/$zoom); |
---|
673 | $l_dest = round(($h_img / $r)/$zoom); |
---|
674 | } |
---|
675 | $h_centre = $h_img * $dy; |
---|
676 | $l_centre = $l_img * $dx; |
---|
677 | if ($align == "focus-center") $top = round($h_centre - ($h_dest*0.5)); |
---|
678 | // ici on n'applique pas *$dy directement, car effet exagéré, |
---|
679 | // alors on pondère |
---|
680 | else $top = round($h_centre - ($h_dest*((2*$dy+0.5)/3))); |
---|
681 | |
---|
682 | $l_centre = $l_img * $dx; |
---|
683 | if ($align == "focus-center") $left = round($l_centre - ($l_dest*0.5)); |
---|
684 | else $left = round($l_centre - ($l_dest*((2*$dx+0.5)/3))); |
---|
685 | |
---|
686 | |
---|
687 | if ($top < 0) $top = 0; |
---|
688 | if ($top + $h_dest > $h_img ) $top = $h_img - $h_dest; |
---|
689 | if ($left < 0) $left = 0; |
---|
690 | if ($left + $l_dest > $l_img ) $left = $l_img - $l_dest; |
---|
691 | |
---|
692 | //echo "<li>$dx x $dy - $l_img x $h_img - $l_dest x $h_dest - $l_centre x $h_centre - $left x $top</li>"; |
---|
693 | $align = "top=$top, left=$left"; |
---|
694 | } |
---|
695 | |
---|
696 | include_spip("filtres/images_transforme"); |
---|
697 | $img = image_recadre($img, $l_dest, $h_dest, $align); |
---|
698 | |
---|
699 | // Second passage si $zoom (on verra plus tard si c'est intéressant de le traiter en amont) |
---|
700 | if ($zoom > 1 && $mode != "focus" && $mode != "focus-center") { |
---|
701 | $l_img = largeur ($img)/2; |
---|
702 | $h_img = hauteur($img)/2; |
---|
703 | |
---|
704 | $img = image_recadre($img, $l_img, $h_img); |
---|
705 | |
---|
706 | } |
---|
707 | |
---|
708 | return $img; |
---|
709 | } |
---|
710 | |
---|
711 | |
---|
712 | function image_responsive_affiche_milieu($flux, $effacer=false) { |
---|
713 | |
---|
714 | $exec = $flux["args"]["exec"]; |
---|
715 | |
---|
716 | |
---|
717 | if ($exec == "admin_vider") { |
---|
718 | $retour = recuperer_fond("squelettes/admin_vider_responsive"); |
---|
719 | |
---|
720 | $flux["data"] .= $retour; |
---|
721 | } |
---|
722 | |
---|
723 | return $flux; |
---|
724 | } |
---|
725 | |
---|
726 | |
---|