1 | <?php |
---|
2 | /** |
---|
3 | * Fonctions du plugin Filtres Images Vectorise |
---|
4 | * |
---|
5 | * @plugin Filtres Images Vectorise |
---|
6 | * @copyright 2019 |
---|
7 | * @author Cedric |
---|
8 | * @licence GNU/GPL |
---|
9 | * @package SPIP\Filtres Images Vectorise\Filtres |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Generation d'une Image SVG a partir d'un bitmap |
---|
14 | * en utilisant a la fois geometrize pour avoir un background non uniforme |
---|
15 | * et PotRace pour creer un trace de details que l'on superpose en mixant avec la geometrization |
---|
16 | * |
---|
17 | * @param string $img |
---|
18 | * @param int|string $nb_shapes |
---|
19 | * @param array $geometrizeOptions |
---|
20 | * @param array $potraceOptions |
---|
21 | * @return string |
---|
22 | */ |
---|
23 | function image_geopotrize($img, $nb_shapes = 'auto', $opacite_trace = 0.75, $geometrizeOptions = [], $potraceOptions=[]) { |
---|
24 | |
---|
25 | $cache = _image_valeurs_trans($img, "image_vectorise-".json_encode([$nb_shapes, $opacite_trace, $geometrizeOptions, $potraceOptions]), "svg"); |
---|
26 | if (!$cache) { |
---|
27 | return false; |
---|
28 | } |
---|
29 | // facile ! |
---|
30 | if ($cache['format_source'] === 'svg'){ |
---|
31 | return $img; |
---|
32 | } |
---|
33 | |
---|
34 | if ($cache["creer"]) { |
---|
35 | |
---|
36 | include_spip('filtres/image_geometrize'); |
---|
37 | include_spip('filtres/image_potrace'); |
---|
38 | |
---|
39 | if ($nb_shapes === 'auto' or !intval($nb_shapes)) { |
---|
40 | $coeff_quality = 0.4; // par rapport a la qualite auto de geometrize on applique un x0.2 = 20% pour le auto ici |
---|
41 | |
---|
42 | $auto = round($coeff_quality * sqrt($cache['largeur'] * $cache['hauteur']) / 2); |
---|
43 | $max_shapes = (isset($geometrizeOptions['maxShapes']) ? $geometrizeOptions['maxShapes'] : 600); |
---|
44 | $auto = min($auto, $max_shapes); |
---|
45 | if (strpos($nb_shapes, 'x') === 0 and is_numeric($coeff = substr($nb_shapes,1))) { |
---|
46 | $auto = round($auto * $coeff); |
---|
47 | } |
---|
48 | $nb_shapes = $auto; |
---|
49 | } |
---|
50 | |
---|
51 | // on commence par un background geometrize sans trop de details |
---|
52 | $thumbnail = filtrer('image_reduire', $img, 128); |
---|
53 | $img_svg_geo = image_geometrize($thumbnail, $nb_shapes, $geometrizeOptions); |
---|
54 | $file_svg_geo = supprimer_timestamp(extraire_attribut($img_svg_geo, 'src')); |
---|
55 | $svg_geo = file_get_contents($file_svg_geo); |
---|
56 | |
---|
57 | $svg_geo = explode('>', $svg_geo, 2); |
---|
58 | if (strpos($svg_geo[0], "<" . "?xml")===0){ |
---|
59 | $svg_geo = explode('>', trim($svg_geo[1]), 2); |
---|
60 | } |
---|
61 | |
---|
62 | $t = $svg_geo[0] . '>'; |
---|
63 | $viewbox = extraire_attribut($t, 'viewBox'); |
---|
64 | $viewbox = explode(' ', $viewbox); |
---|
65 | $width_geo = $viewbox[2]; |
---|
66 | |
---|
67 | |
---|
68 | // on genere ensuite un trace PotRace plus fin, sans background |
---|
69 | $potraceOptions['rounding'] = "width=$width_geo"; |
---|
70 | $potraceOptions['bgcolor'] = 'transparent'; |
---|
71 | |
---|
72 | $thumbnail = filtrer('image_reduire', $img, 512); |
---|
73 | $thumbnail = filtrer('image_renforcement', $thumbnail); |
---|
74 | $img_svg_pot = image_potrace($thumbnail, $potraceOptions); |
---|
75 | $file_svg_pot = supprimer_timestamp(extraire_attribut($img_svg_pot, 'src')); |
---|
76 | $svg_pot = file_get_contents($file_svg_pot); |
---|
77 | |
---|
78 | $svg_pot = explode('>', $svg_pot, 2); |
---|
79 | if (strpos($svg_pot[0], "<" . "?xml")===0){ |
---|
80 | $svg_pot = explode('>', trim($svg_pot[1]), 2); |
---|
81 | } |
---|
82 | |
---|
83 | // et on superpose |
---|
84 | $opacity = max(min(floatval($opacite_trace),1),0); |
---|
85 | $svg_pot[1] = str_replace("fill=", 'opacity="'.$opacity.'" style="mix-blend-mode: multiply" fill=', $svg_pot[1]); |
---|
86 | |
---|
87 | $svg_geo[1] = str_replace('</svg>', $svg_pot[1], $svg_geo[1]); |
---|
88 | |
---|
89 | $svg_image = $svg_geo[0] . '>' . $svg_geo[1]; |
---|
90 | |
---|
91 | $dest = $cache["fichier_dest"]; |
---|
92 | ecrire_fichier($dest, $svg_image); |
---|
93 | } |
---|
94 | |
---|
95 | if (!@file_exists($dest)) { |
---|
96 | return false; |
---|
97 | } |
---|
98 | |
---|
99 | return _image_ecrire_tag($cache, array('src' => $cache["fichier_dest"])); |
---|
100 | } |
---|