@@ -17,13 +17,13 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | if (!defined('IMG_SVG')) { |
| 24 | - // complete IMG_BMP | IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM | IMG_WEBP |
|
| 25 | - define('IMG_SVG',128); |
|
| 26 | - define('IMAGETYPE_SVG', 19); |
|
| 24 | + // complete IMG_BMP | IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM | IMG_WEBP |
|
| 25 | + define('IMG_SVG',128); |
|
| 26 | + define('IMAGETYPE_SVG', 19); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -39,39 +39,39 @@ discard block |
||
| 39 | 39 | * false si on a pas pu charger l'image |
| 40 | 40 | */ |
| 41 | 41 | function svg_charger($fichier, $maxlen=null) { |
| 42 | - if (strpos($fichier, "data:image/svg+xml") === 0) { |
|
| 43 | - $image = explode(";", $fichier, 2); |
|
| 44 | - $image = end($image); |
|
| 45 | - if (strpos($image, "base64,") === 0) { |
|
| 46 | - $image = base64_decode(substr($image, 7)); |
|
| 47 | - } |
|
| 48 | - if (strpos($image, "<svg") !== false) { |
|
| 49 | - return $image; |
|
| 50 | - } |
|
| 51 | - // encodage inconnu ou autre format d'image ? |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 54 | - // c'est peut etre deja une image svg ? |
|
| 55 | - if (strpos($fichier, "<svg") !== false) { |
|
| 56 | - return $fichier; |
|
| 57 | - } |
|
| 58 | - if (!file_exists($fichier)) { |
|
| 59 | - $fichier = supprimer_timestamp($fichier); |
|
| 60 | - if (!file_exists($fichier)) { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - if (is_null($maxlen)) { |
|
| 65 | - $image = file_get_contents($fichier); |
|
| 66 | - } |
|
| 67 | - else { |
|
| 68 | - $image = file_get_contents($fichier, false,null,0, $maxlen); |
|
| 69 | - } |
|
| 70 | - // est-ce bien une image svg ? |
|
| 71 | - if (strpos($image, "<svg") !== false) { |
|
| 72 | - return $image; |
|
| 73 | - } |
|
| 74 | - return false; |
|
| 42 | + if (strpos($fichier, "data:image/svg+xml") === 0) { |
|
| 43 | + $image = explode(";", $fichier, 2); |
|
| 44 | + $image = end($image); |
|
| 45 | + if (strpos($image, "base64,") === 0) { |
|
| 46 | + $image = base64_decode(substr($image, 7)); |
|
| 47 | + } |
|
| 48 | + if (strpos($image, "<svg") !== false) { |
|
| 49 | + return $image; |
|
| 50 | + } |
|
| 51 | + // encodage inconnu ou autre format d'image ? |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | + // c'est peut etre deja une image svg ? |
|
| 55 | + if (strpos($fichier, "<svg") !== false) { |
|
| 56 | + return $fichier; |
|
| 57 | + } |
|
| 58 | + if (!file_exists($fichier)) { |
|
| 59 | + $fichier = supprimer_timestamp($fichier); |
|
| 60 | + if (!file_exists($fichier)) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + if (is_null($maxlen)) { |
|
| 65 | + $image = file_get_contents($fichier); |
|
| 66 | + } |
|
| 67 | + else { |
|
| 68 | + $image = file_get_contents($fichier, false,null,0, $maxlen); |
|
| 69 | + } |
|
| 70 | + // est-ce bien une image svg ? |
|
| 71 | + if (strpos($image, "<svg") !== false) { |
|
| 72 | + return $image; |
|
| 73 | + } |
|
| 74 | + return false; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -80,29 +80,29 @@ discard block |
||
| 80 | 80 | * @return array|bool |
| 81 | 81 | */ |
| 82 | 82 | function svg_lire_balise_svg($fichier) { |
| 83 | - if (!$debut_fichier = svg_charger($fichier, 4096)) { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 83 | + if (!$debut_fichier = svg_charger($fichier, 4096)) { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - if (($ps = stripos($debut_fichier, "<svg")) !== false) { |
|
| 87 | + if (($ps = stripos($debut_fichier, "<svg")) !== false) { |
|
| 88 | 88 | |
| 89 | - $pe = stripos($debut_fichier, ">", $ps); |
|
| 90 | - $balise_svg = substr($debut_fichier, $ps, $pe - $ps +1); |
|
| 89 | + $pe = stripos($debut_fichier, ">", $ps); |
|
| 90 | + $balise_svg = substr($debut_fichier, $ps, $pe - $ps +1); |
|
| 91 | 91 | |
| 92 | - if (preg_match_all(",([\w:\-]+)=,Uims", $balise_svg, $matches)) { |
|
| 93 | - if (!function_exists('extraire_attribut')) { |
|
| 94 | - include_spip('inc/filtres'); |
|
| 95 | - } |
|
| 96 | - $attributs = []; |
|
| 97 | - foreach ($matches[1] as $att) { |
|
| 98 | - $attributs[$att] = extraire_attribut($balise_svg, $att); |
|
| 99 | - } |
|
| 92 | + if (preg_match_all(",([\w:\-]+)=,Uims", $balise_svg, $matches)) { |
|
| 93 | + if (!function_exists('extraire_attribut')) { |
|
| 94 | + include_spip('inc/filtres'); |
|
| 95 | + } |
|
| 96 | + $attributs = []; |
|
| 97 | + foreach ($matches[1] as $att) { |
|
| 98 | + $attributs[$att] = extraire_attribut($balise_svg, $att); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - return [$balise_svg, $attributs]; |
|
| 102 | - } |
|
| 103 | - } |
|
| 101 | + return [$balise_svg, $attributs]; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - return false; |
|
| 105 | + return false; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -112,12 +112,12 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | function svg_lire_attributs($img) { |
| 114 | 114 | |
| 115 | - if ($svg_infos = svg_lire_balise_svg($img)) { |
|
| 116 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 117 | - return $attributs; |
|
| 118 | - } |
|
| 115 | + if ($svg_infos = svg_lire_balise_svg($img)) { |
|
| 116 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 117 | + return $attributs; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - return false; |
|
| 120 | + return false; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -127,38 +127,38 @@ discard block |
||
| 127 | 127 | * @return bool|float|int |
| 128 | 128 | */ |
| 129 | 129 | function svg_dimension_to_pixels($dimension) { |
| 130 | - if (preg_match(',^(-?\d+)([^\d]*),i', trim($dimension), $m)){ |
|
| 131 | - switch (strtolower($m[2])) { |
|
| 132 | - case '%': |
|
| 133 | - // on ne sait pas faire :( |
|
| 134 | - return false; |
|
| 135 | - break; |
|
| 136 | - case 'em': |
|
| 137 | - return intval($m[1])*16; // 16px font-size par defaut |
|
| 138 | - break; |
|
| 139 | - case 'ex': |
|
| 140 | - return intval($m[1])*16; // 16px font-size par defaut |
|
| 141 | - break; |
|
| 142 | - case 'pc': |
|
| 143 | - return intval($m[1])*16; // 1/6 inch = 96px/6 in CSS |
|
| 144 | - break; |
|
| 145 | - case 'cm': |
|
| 146 | - return intval(round($m[1]*96/2.54)); // 96px / 2.54cm; |
|
| 147 | - break; |
|
| 148 | - case 'mm': |
|
| 149 | - return intval(round($m[1]*96/25.4)); // 96px / 25.4mm; |
|
| 150 | - break; |
|
| 151 | - case 'in': |
|
| 152 | - return intval($m[1])*96; // 1 inch = 96px in CSS |
|
| 153 | - break; |
|
| 154 | - case 'px': |
|
| 155 | - case 'pt': |
|
| 156 | - default: |
|
| 157 | - return intval($m[1]); |
|
| 158 | - break; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - return false; |
|
| 130 | + if (preg_match(',^(-?\d+)([^\d]*),i', trim($dimension), $m)){ |
|
| 131 | + switch (strtolower($m[2])) { |
|
| 132 | + case '%': |
|
| 133 | + // on ne sait pas faire :( |
|
| 134 | + return false; |
|
| 135 | + break; |
|
| 136 | + case 'em': |
|
| 137 | + return intval($m[1])*16; // 16px font-size par defaut |
|
| 138 | + break; |
|
| 139 | + case 'ex': |
|
| 140 | + return intval($m[1])*16; // 16px font-size par defaut |
|
| 141 | + break; |
|
| 142 | + case 'pc': |
|
| 143 | + return intval($m[1])*16; // 1/6 inch = 96px/6 in CSS |
|
| 144 | + break; |
|
| 145 | + case 'cm': |
|
| 146 | + return intval(round($m[1]*96/2.54)); // 96px / 2.54cm; |
|
| 147 | + break; |
|
| 148 | + case 'mm': |
|
| 149 | + return intval(round($m[1]*96/25.4)); // 96px / 25.4mm; |
|
| 150 | + break; |
|
| 151 | + case 'in': |
|
| 152 | + return intval($m[1])*96; // 1 inch = 96px in CSS |
|
| 153 | + break; |
|
| 154 | + case 'px': |
|
| 155 | + case 'pt': |
|
| 156 | + default: |
|
| 157 | + return intval($m[1]); |
|
| 158 | + break; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + return false; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** |
@@ -169,15 +169,15 @@ discard block |
||
| 169 | 169 | * @return string |
| 170 | 170 | */ |
| 171 | 171 | function svg_change_balise_svg($svg, $old_balise_svg, $attributs) { |
| 172 | - $new_balise_svg = "<svg"; |
|
| 173 | - foreach ($attributs as $k=>$v) { |
|
| 174 | - $new_balise_svg .= " $k=\"".entites_html($v)."\""; |
|
| 175 | - } |
|
| 176 | - $new_balise_svg .= ">"; |
|
| 177 | - |
|
| 178 | - $p = strpos($svg, $old_balise_svg); |
|
| 179 | - $svg = substr_replace($svg, $new_balise_svg, $p, strlen($old_balise_svg)); |
|
| 180 | - return $svg; |
|
| 172 | + $new_balise_svg = "<svg"; |
|
| 173 | + foreach ($attributs as $k=>$v) { |
|
| 174 | + $new_balise_svg .= " $k=\"".entites_html($v)."\""; |
|
| 175 | + } |
|
| 176 | + $new_balise_svg .= ">"; |
|
| 177 | + |
|
| 178 | + $p = strpos($svg, $old_balise_svg); |
|
| 179 | + $svg = substr_replace($svg, $new_balise_svg, $p, strlen($old_balise_svg)); |
|
| 180 | + return $svg; |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | /** |
@@ -189,15 +189,15 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | function svg_insert_shapes($svg, $shapes, $start=true) { |
| 191 | 191 | |
| 192 | - if ($start === false or $start === 'end') { |
|
| 193 | - $svg = str_replace("</svg>", $shapes . "</svg>", $svg); |
|
| 194 | - } |
|
| 195 | - else { |
|
| 196 | - $p = stripos($svg, "<svg"); |
|
| 197 | - $p = strpos($svg, ">", $p); |
|
| 198 | - $svg = substr_replace($svg, $shapes, $p+1, 0); |
|
| 199 | - } |
|
| 200 | - return $svg; |
|
| 192 | + if ($start === false or $start === 'end') { |
|
| 193 | + $svg = str_replace("</svg>", $shapes . "</svg>", $svg); |
|
| 194 | + } |
|
| 195 | + else { |
|
| 196 | + $p = stripos($svg, "<svg"); |
|
| 197 | + $p = strpos($svg, ">", $p); |
|
| 198 | + $svg = substr_replace($svg, $shapes, $p+1, 0); |
|
| 199 | + } |
|
| 200 | + return $svg; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -210,13 +210,13 @@ discard block |
||
| 210 | 210 | * @return string |
| 211 | 211 | */ |
| 212 | 212 | function svg_clip_in_box($svg, $x, $y, $width, $height) { |
| 213 | - $rect = "<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\" />"; |
|
| 214 | - $id = 'clip-'.substr(md5($rect . strlen($svg)),0,8); |
|
| 215 | - $clippath = "<clipPath id=\"$id\">$rect</clipPath>"; |
|
| 216 | - $g = "<g clip-path=\"url(#$id)\">"; |
|
| 217 | - $svg = svg_insert_shapes($svg, $clippath . $g); |
|
| 218 | - $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 219 | - return $svg; |
|
| 213 | + $rect = "<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\" />"; |
|
| 214 | + $id = 'clip-'.substr(md5($rect . strlen($svg)),0,8); |
|
| 215 | + $clippath = "<clipPath id=\"$id\">$rect</clipPath>"; |
|
| 216 | + $g = "<g clip-path=\"url(#$id)\">"; |
|
| 217 | + $svg = svg_insert_shapes($svg, $clippath . $g); |
|
| 218 | + $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 219 | + return $svg; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -227,21 +227,21 @@ discard block |
||
| 227 | 227 | * @return bool|string |
| 228 | 228 | */ |
| 229 | 229 | function svg_redimensionner($img, $new_width, $new_height) { |
| 230 | - if ($svg = svg_charger($img) |
|
| 231 | - and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 230 | + if ($svg = svg_charger($img) |
|
| 231 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 232 | 232 | |
| 233 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 234 | - if (!isset($attributs['viewBox'])) { |
|
| 235 | - $attributs['viewBox'] = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 236 | - } |
|
| 237 | - $attributs['width'] = strval($new_width); |
|
| 238 | - $attributs['height'] = strval($new_height); |
|
| 233 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 234 | + if (!isset($attributs['viewBox'])) { |
|
| 235 | + $attributs['viewBox'] = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 236 | + } |
|
| 237 | + $attributs['width'] = strval($new_width); |
|
| 238 | + $attributs['height'] = strval($new_height); |
|
| 239 | 239 | |
| 240 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 241 | - return $svg; |
|
| 242 | - } |
|
| 240 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 241 | + return $svg; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - return $img; |
|
| 244 | + return $img; |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -250,15 +250,15 @@ discard block |
||
| 250 | 250 | * @return string |
| 251 | 251 | */ |
| 252 | 252 | function svg_couleur_to_hexa($couleur) { |
| 253 | - if (strpos($couleur, "rgb(")===0) { |
|
| 254 | - $c = explode(',', substr($couleur, 4)); |
|
| 255 | - $couleur = _couleur_dec_to_hex(intval($c[0]), intval($c[1]), intval($c[2])); |
|
| 256 | - } |
|
| 257 | - else { |
|
| 258 | - $couleur = couleur_html_to_hex($couleur); |
|
| 259 | - } |
|
| 260 | - $couleur = '#'.ltrim($couleur,'#'); |
|
| 261 | - return $couleur; |
|
| 253 | + if (strpos($couleur, "rgb(")===0) { |
|
| 254 | + $c = explode(',', substr($couleur, 4)); |
|
| 255 | + $couleur = _couleur_dec_to_hex(intval($c[0]), intval($c[1]), intval($c[2])); |
|
| 256 | + } |
|
| 257 | + else { |
|
| 258 | + $couleur = couleur_html_to_hex($couleur); |
|
| 259 | + } |
|
| 260 | + $couleur = '#'.ltrim($couleur,'#'); |
|
| 261 | + return $couleur; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -267,11 +267,11 @@ discard block |
||
| 267 | 267 | * @return array |
| 268 | 268 | */ |
| 269 | 269 | function svg_couleur_to_rgb($couleur) { |
| 270 | - if (strpos($couleur, "rgb(")===0) { |
|
| 271 | - $c = explode(',', substr($couleur, 4)); |
|
| 272 | - return ['red' => intval($c[0]),'green' => intval($c[1]),'blue' => intval($c[2])]; |
|
| 273 | - } |
|
| 274 | - return _couleur_hex_to_dec($couleur); |
|
| 270 | + if (strpos($couleur, "rgb(")===0) { |
|
| 271 | + $c = explode(',', substr($couleur, 4)); |
|
| 272 | + return ['red' => intval($c[0]),'green' => intval($c[1]),'blue' => intval($c[2])]; |
|
| 273 | + } |
|
| 274 | + return _couleur_hex_to_dec($couleur); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | |
@@ -288,54 +288,54 @@ discard block |
||
| 288 | 288 | * @return string |
| 289 | 289 | */ |
| 290 | 290 | function svg_force_viewBox_px($img, $force_width_and_height = false) { |
| 291 | - if ($svg = svg_charger($img) |
|
| 292 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 293 | - |
|
| 294 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 295 | - |
|
| 296 | - // il nous faut une viewBox |
|
| 297 | - if (!isset($attributs['viewBox'])) { |
|
| 298 | - $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 299 | - } |
|
| 300 | - else { |
|
| 301 | - $viewBox = $attributs['viewBox']; |
|
| 302 | - } |
|
| 303 | - // et on la convertit en px |
|
| 304 | - $viewBox = explode(' ', $viewBox); |
|
| 305 | - $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 306 | - $viewBox = array_map('intval', $viewBox); |
|
| 307 | - if (!$viewBox[2]) { |
|
| 308 | - $viewBox[2] = '300'; |
|
| 309 | - } |
|
| 310 | - if (!$viewBox[3]) { |
|
| 311 | - $viewBox[3] = '150'; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - if ($force_width_and_height) { |
|
| 315 | - $width = false; |
|
| 316 | - if (isset($attributs['width'])) { |
|
| 317 | - $width = svg_dimension_to_pixels($attributs['width']); |
|
| 318 | - } |
|
| 319 | - if (!$width) { |
|
| 320 | - $width = $viewBox[2]; |
|
| 321 | - } |
|
| 322 | - $attributs['width'] = $width; |
|
| 323 | - $height = false; |
|
| 324 | - if (isset($attributs['height'])) { |
|
| 325 | - $height = svg_dimension_to_pixels($attributs['height']); |
|
| 326 | - } |
|
| 327 | - if (!$height) { |
|
| 328 | - $height = $viewBox[3]; |
|
| 329 | - } |
|
| 330 | - $attributs['height'] = $height; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 334 | - |
|
| 335 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 336 | - return $svg; |
|
| 337 | - } |
|
| 338 | - return $img; |
|
| 291 | + if ($svg = svg_charger($img) |
|
| 292 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 293 | + |
|
| 294 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 295 | + |
|
| 296 | + // il nous faut une viewBox |
|
| 297 | + if (!isset($attributs['viewBox'])) { |
|
| 298 | + $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 299 | + } |
|
| 300 | + else { |
|
| 301 | + $viewBox = $attributs['viewBox']; |
|
| 302 | + } |
|
| 303 | + // et on la convertit en px |
|
| 304 | + $viewBox = explode(' ', $viewBox); |
|
| 305 | + $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 306 | + $viewBox = array_map('intval', $viewBox); |
|
| 307 | + if (!$viewBox[2]) { |
|
| 308 | + $viewBox[2] = '300'; |
|
| 309 | + } |
|
| 310 | + if (!$viewBox[3]) { |
|
| 311 | + $viewBox[3] = '150'; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + if ($force_width_and_height) { |
|
| 315 | + $width = false; |
|
| 316 | + if (isset($attributs['width'])) { |
|
| 317 | + $width = svg_dimension_to_pixels($attributs['width']); |
|
| 318 | + } |
|
| 319 | + if (!$width) { |
|
| 320 | + $width = $viewBox[2]; |
|
| 321 | + } |
|
| 322 | + $attributs['width'] = $width; |
|
| 323 | + $height = false; |
|
| 324 | + if (isset($attributs['height'])) { |
|
| 325 | + $height = svg_dimension_to_pixels($attributs['height']); |
|
| 326 | + } |
|
| 327 | + if (!$height) { |
|
| 328 | + $height = $viewBox[3]; |
|
| 329 | + } |
|
| 330 | + $attributs['height'] = $height; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 334 | + |
|
| 335 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 336 | + return $svg; |
|
| 337 | + } |
|
| 338 | + return $img; |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** |
@@ -344,12 +344,12 @@ discard block |
||
| 344 | 344 | * @return array|mixed |
| 345 | 345 | */ |
| 346 | 346 | function svg_extract_couleurs($img) { |
| 347 | - if ($svg = svg_charger($img)) { |
|
| 348 | - if (preg_match_all("/(#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])|(rgb\([\s\d]+,[\s\d]+,[\s\d]+\))|(#[0-9a-f][0-9a-f][0-9a-f])/imS", $svg, $matches)) { |
|
| 349 | - return $matches[0]; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - return []; |
|
| 347 | + if ($svg = svg_charger($img)) { |
|
| 348 | + if (preg_match_all("/(#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])|(rgb\([\s\d]+,[\s\d]+,[\s\d]+\))|(#[0-9a-f][0-9a-f][0-9a-f])/imS", $svg, $matches)) { |
|
| 349 | + return $matches[0]; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + return []; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -360,56 +360,56 @@ discard block |
||
| 360 | 360 | * @return bool|string |
| 361 | 361 | */ |
| 362 | 362 | function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color='') { |
| 363 | - if ($svg = svg_force_viewBox_px($img) |
|
| 364 | - and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 365 | - |
|
| 366 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 367 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 368 | - |
|
| 369 | - $viewport_w = $new_width; |
|
| 370 | - $viewport_h = $new_height; |
|
| 371 | - $viewport_ox = $offset_width; |
|
| 372 | - $viewport_oy = $offset_height; |
|
| 373 | - |
|
| 374 | - // si on a un width/height qui rescale, il faut rescaler |
|
| 375 | - if (isset ($attributs['width']) |
|
| 376 | - and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 377 | - and isset ($attributs['height']) |
|
| 378 | - and $h = svg_dimension_to_pixels($attributs['height'])) { |
|
| 379 | - |
|
| 380 | - $xscale = $viewBox[2] / $w; |
|
| 381 | - $viewport_w = intval(round($viewport_w * $xscale)); |
|
| 382 | - $viewport_ox = intval(round($viewport_ox * $xscale)); |
|
| 383 | - $yscale = $viewBox[3] / $h; |
|
| 384 | - $viewport_h = intval(round($viewport_h * $yscale)); |
|
| 385 | - $viewport_oy = intval(round($viewport_oy * $yscale)); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 389 | - $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // maintenant on redefinit la viewBox |
|
| 393 | - $viewBox[0] += $viewport_ox; |
|
| 394 | - $viewBox[1] += $viewport_oy; |
|
| 395 | - $viewBox[2] = $viewport_w; |
|
| 396 | - $viewBox[3] = $viewport_h; |
|
| 397 | - |
|
| 398 | - $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 399 | - $attributs['width'] = strval($new_width); |
|
| 400 | - $attributs['height'] = strval($new_height); |
|
| 401 | - |
|
| 402 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 403 | - |
|
| 404 | - // ajouter un background |
|
| 405 | - if ($background_color and $background_color!=='transparent') { |
|
| 406 | - $svg = svg_ajouter_background($svg, $background_color); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - return $svg; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $img; |
|
| 363 | + if ($svg = svg_force_viewBox_px($img) |
|
| 364 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 365 | + |
|
| 366 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 367 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 368 | + |
|
| 369 | + $viewport_w = $new_width; |
|
| 370 | + $viewport_h = $new_height; |
|
| 371 | + $viewport_ox = $offset_width; |
|
| 372 | + $viewport_oy = $offset_height; |
|
| 373 | + |
|
| 374 | + // si on a un width/height qui rescale, il faut rescaler |
|
| 375 | + if (isset ($attributs['width']) |
|
| 376 | + and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 377 | + and isset ($attributs['height']) |
|
| 378 | + and $h = svg_dimension_to_pixels($attributs['height'])) { |
|
| 379 | + |
|
| 380 | + $xscale = $viewBox[2] / $w; |
|
| 381 | + $viewport_w = intval(round($viewport_w * $xscale)); |
|
| 382 | + $viewport_ox = intval(round($viewport_ox * $xscale)); |
|
| 383 | + $yscale = $viewBox[3] / $h; |
|
| 384 | + $viewport_h = intval(round($viewport_h * $yscale)); |
|
| 385 | + $viewport_oy = intval(round($viewport_oy * $yscale)); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 389 | + $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // maintenant on redefinit la viewBox |
|
| 393 | + $viewBox[0] += $viewport_ox; |
|
| 394 | + $viewBox[1] += $viewport_oy; |
|
| 395 | + $viewBox[2] = $viewport_w; |
|
| 396 | + $viewBox[3] = $viewport_h; |
|
| 397 | + |
|
| 398 | + $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 399 | + $attributs['width'] = strval($new_width); |
|
| 400 | + $attributs['height'] = strval($new_height); |
|
| 401 | + |
|
| 402 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 403 | + |
|
| 404 | + // ajouter un background |
|
| 405 | + if ($background_color and $background_color!=='transparent') { |
|
| 406 | + $svg = svg_ajouter_background($svg, $background_color); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + return $svg; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $img; |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | /** |
@@ -419,25 +419,25 @@ discard block |
||
| 419 | 419 | * @return bool|string |
| 420 | 420 | */ |
| 421 | 421 | function svg_ajouter_background($img, $background_color) { |
| 422 | - if ($svg = svg_charger($img) |
|
| 423 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 424 | - |
|
| 425 | - if ($background_color and $background_color!=='transparent') { |
|
| 426 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 427 | - |
|
| 428 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 429 | - if (isset($attributs['viewBox'])) { |
|
| 430 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 431 | - $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\"/>"; |
|
| 432 | - } |
|
| 433 | - else { |
|
| 434 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 435 | - } |
|
| 436 | - $svg = svg_insert_shapes($svg, $rect); |
|
| 437 | - } |
|
| 438 | - return $svg; |
|
| 439 | - } |
|
| 440 | - return $img; |
|
| 422 | + if ($svg = svg_charger($img) |
|
| 423 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 424 | + |
|
| 425 | + if ($background_color and $background_color!=='transparent') { |
|
| 426 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 427 | + |
|
| 428 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 429 | + if (isset($attributs['viewBox'])) { |
|
| 430 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 431 | + $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\"/>"; |
|
| 432 | + } |
|
| 433 | + else { |
|
| 434 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 435 | + } |
|
| 436 | + $svg = svg_insert_shapes($svg, $rect); |
|
| 437 | + } |
|
| 438 | + return $svg; |
|
| 439 | + } |
|
| 440 | + return $img; |
|
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | |
@@ -448,25 +448,25 @@ discard block |
||
| 448 | 448 | * @return bool|string |
| 449 | 449 | */ |
| 450 | 450 | function svg_ajouter_voile($img, $background_color, $opacity) { |
| 451 | - if ($svg = svg_charger($img) |
|
| 452 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 453 | - |
|
| 454 | - if ($background_color and $background_color!=='transparent') { |
|
| 455 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 456 | - |
|
| 457 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 458 | - if (isset($attributs['viewBox'])) { |
|
| 459 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 460 | - $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 461 | - } |
|
| 462 | - else { |
|
| 463 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 464 | - } |
|
| 465 | - $svg = svg_insert_shapes($svg, $rect, false); |
|
| 466 | - } |
|
| 467 | - return $svg; |
|
| 468 | - } |
|
| 469 | - return $img; |
|
| 451 | + if ($svg = svg_charger($img) |
|
| 452 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 453 | + |
|
| 454 | + if ($background_color and $background_color!=='transparent') { |
|
| 455 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 456 | + |
|
| 457 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 458 | + if (isset($attributs['viewBox'])) { |
|
| 459 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 460 | + $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 461 | + } |
|
| 462 | + else { |
|
| 463 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 464 | + } |
|
| 465 | + $svg = svg_insert_shapes($svg, $rect, false); |
|
| 466 | + } |
|
| 467 | + return $svg; |
|
| 468 | + } |
|
| 469 | + return $img; |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | 472 | |
@@ -477,26 +477,26 @@ discard block |
||
| 477 | 477 | * @return bool|string |
| 478 | 478 | */ |
| 479 | 479 | function svg_transformer($img, $attributs) { |
| 480 | - if ($svg = svg_charger($img) |
|
| 481 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 482 | - |
|
| 483 | - if ($attributs) { |
|
| 484 | - list($balise_svg, ) = $svg_infos; |
|
| 485 | - $g = "<g"; |
|
| 486 | - foreach ($attributs as $k=>$v) { |
|
| 487 | - if (strlen($v)) { |
|
| 488 | - $g .= " $k=\"".attribut_html($v)."\""; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - if (strlen($g) > 2) { |
|
| 492 | - $g .= ">"; |
|
| 493 | - $svg = svg_insert_shapes($svg, $g); |
|
| 494 | - $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 495 | - } |
|
| 496 | - } |
|
| 497 | - return $svg; |
|
| 498 | - } |
|
| 499 | - return $img; |
|
| 480 | + if ($svg = svg_charger($img) |
|
| 481 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 482 | + |
|
| 483 | + if ($attributs) { |
|
| 484 | + list($balise_svg, ) = $svg_infos; |
|
| 485 | + $g = "<g"; |
|
| 486 | + foreach ($attributs as $k=>$v) { |
|
| 487 | + if (strlen($v)) { |
|
| 488 | + $g .= " $k=\"".attribut_html($v)."\""; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + if (strlen($g) > 2) { |
|
| 492 | + $g .= ">"; |
|
| 493 | + $svg = svg_insert_shapes($svg, $g); |
|
| 494 | + $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 495 | + } |
|
| 496 | + } |
|
| 497 | + return $svg; |
|
| 498 | + } |
|
| 499 | + return $img; |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | /** |
@@ -507,20 +507,20 @@ discard block |
||
| 507 | 507 | * @return bool|string |
| 508 | 508 | */ |
| 509 | 509 | function svg_apply_filter($img, $filter_def) { |
| 510 | - if ($svg = svg_charger($img) |
|
| 511 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 512 | - |
|
| 513 | - if ($filter_def) { |
|
| 514 | - list($balise_svg, ) = $svg_infos; |
|
| 515 | - $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 516 | - $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 517 | - $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 518 | - $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 519 | - $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 520 | - } |
|
| 521 | - return $svg; |
|
| 522 | - } |
|
| 523 | - return $img; |
|
| 510 | + if ($svg = svg_charger($img) |
|
| 511 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 512 | + |
|
| 513 | + if ($filter_def) { |
|
| 514 | + list($balise_svg, ) = $svg_infos; |
|
| 515 | + $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 516 | + $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 517 | + $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 518 | + $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 519 | + $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 520 | + } |
|
| 521 | + return $svg; |
|
| 522 | + } |
|
| 523 | + return $img; |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | /** |
@@ -530,8 +530,8 @@ discard block |
||
| 530 | 530 | * @return string |
| 531 | 531 | */ |
| 532 | 532 | function svg_filter_blur($img, $blur_width) { |
| 533 | - $blur_width = intval($blur_width); |
|
| 534 | - return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 533 | + $blur_width = intval($blur_width); |
|
| 534 | + return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | /** |
@@ -541,10 +541,10 @@ discard block |
||
| 541 | 541 | * @return bool|string |
| 542 | 542 | */ |
| 543 | 543 | function svg_filter_grayscale($img, $intensity) { |
| 544 | - $value = round(1.0 - $intensity, 2); |
|
| 545 | - //$filter = "<feColorMatrix type=\"matrix\" values=\"0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\"/>"; |
|
| 546 | - $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 547 | - return svg_apply_filter($img, $filter); |
|
| 544 | + $value = round(1.0 - $intensity, 2); |
|
| 545 | + //$filter = "<feColorMatrix type=\"matrix\" values=\"0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\"/>"; |
|
| 546 | + $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 547 | + return svg_apply_filter($img, $filter); |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | /** |
@@ -554,8 +554,8 @@ discard block |
||
| 554 | 554 | * @return bool|string |
| 555 | 555 | */ |
| 556 | 556 | function svg_filter_sepia($img, $intensity) { |
| 557 | - $filter = "<feColorMatrix type=\"matrix\" values=\"0.30 0.30 0.30 0.0 0 0.25 0.25 0.25 0.0 0 0.20 0.20 0.20 0.0 0 0.00 0.00 0.00 1 0\"/>"; |
|
| 558 | - return svg_apply_filter($img, $filter); |
|
| 557 | + $filter = "<feColorMatrix type=\"matrix\" values=\"0.30 0.30 0.30 0.0 0 0.25 0.25 0.25 0.0 0 0.20 0.20 0.20 0.0 0 0.00 0.00 0.00 1 0\"/>"; |
|
| 558 | + return svg_apply_filter($img, $filter); |
|
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | /** |
@@ -565,30 +565,30 @@ discard block |
||
| 565 | 565 | * @return bool|string |
| 566 | 566 | */ |
| 567 | 567 | function svg_flip($img, $HorV) { |
| 568 | - if ($svg = svg_force_viewBox_px($img) |
|
| 569 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 570 | - |
|
| 571 | - list($balise_svg, $atts) = $svg_infos; |
|
| 572 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 573 | - |
|
| 574 | - if (!in_array($HorV, ['h', 'H'])) { |
|
| 575 | - $transform = "scale(-1,1)"; |
|
| 576 | - |
|
| 577 | - $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 578 | - $mx = -$x; |
|
| 579 | - $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 580 | - } |
|
| 581 | - else { |
|
| 582 | - $transform = "scale(1,-1)"; |
|
| 583 | - |
|
| 584 | - $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 585 | - $my = -$y; |
|
| 586 | - $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 587 | - } |
|
| 588 | - $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 589 | - return $svg; |
|
| 590 | - } |
|
| 591 | - return $img; |
|
| 568 | + if ($svg = svg_force_viewBox_px($img) |
|
| 569 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 570 | + |
|
| 571 | + list($balise_svg, $atts) = $svg_infos; |
|
| 572 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 573 | + |
|
| 574 | + if (!in_array($HorV, ['h', 'H'])) { |
|
| 575 | + $transform = "scale(-1,1)"; |
|
| 576 | + |
|
| 577 | + $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 578 | + $mx = -$x; |
|
| 579 | + $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 580 | + } |
|
| 581 | + else { |
|
| 582 | + $transform = "scale(1,-1)"; |
|
| 583 | + |
|
| 584 | + $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 585 | + $my = -$y; |
|
| 586 | + $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 587 | + } |
|
| 588 | + $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 589 | + return $svg; |
|
| 590 | + } |
|
| 591 | + return $img; |
|
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | /** |
@@ -602,19 +602,19 @@ discard block |
||
| 602 | 602 | * @return bool|string |
| 603 | 603 | */ |
| 604 | 604 | function svg_rotate($img, $angle, $center_x, $center_y) { |
| 605 | - if ($svg = svg_force_viewBox_px($img) |
|
| 606 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 605 | + if ($svg = svg_force_viewBox_px($img) |
|
| 606 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 607 | 607 | |
| 608 | - list($balise_svg, $atts) = $svg_infos; |
|
| 609 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 608 | + list($balise_svg, $atts) = $svg_infos; |
|
| 609 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 610 | 610 | |
| 611 | - $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 612 | - $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 613 | - $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 611 | + $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 612 | + $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 613 | + $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 614 | 614 | |
| 615 | - return $svg; |
|
| 616 | - } |
|
| 617 | - return $img; |
|
| 615 | + return $svg; |
|
| 616 | + } |
|
| 617 | + return $img; |
|
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | /** |
@@ -626,40 +626,40 @@ discard block |
||
| 626 | 626 | * @return bool|mixed|string |
| 627 | 627 | */ |
| 628 | 628 | function svg_filtrer_couleurs($img, $callback_filter) { |
| 629 | - if ($svg = svg_force_viewBox_px($img) |
|
| 630 | - and $colors = svg_extract_couleurs($svg)) { |
|
| 631 | - |
|
| 632 | - $colors = array_unique($colors); |
|
| 633 | - |
|
| 634 | - $short = []; |
|
| 635 | - $long = []; |
|
| 636 | - while (count($colors)) { |
|
| 637 | - $c = array_shift($colors); |
|
| 638 | - if (strlen($c) == 4) { |
|
| 639 | - $short[] = $c; |
|
| 640 | - } |
|
| 641 | - else { |
|
| 642 | - $long[] = $c; |
|
| 643 | - } |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - $colors = array_merge($long, $short); |
|
| 647 | - $new_colors = []; |
|
| 648 | - $colors = array_flip($colors); |
|
| 649 | - foreach ($colors as $c => $k) { |
|
| 650 | - $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - |
|
| 654 | - foreach ($colors as $original => $replace) { |
|
| 655 | - $new = svg_couleur_to_hexa($original); |
|
| 656 | - $new_colors[$replace] = $callback_filter($new); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 660 | - $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 661 | - |
|
| 662 | - return $svg; |
|
| 663 | - } |
|
| 664 | - return $img; |
|
| 629 | + if ($svg = svg_force_viewBox_px($img) |
|
| 630 | + and $colors = svg_extract_couleurs($svg)) { |
|
| 631 | + |
|
| 632 | + $colors = array_unique($colors); |
|
| 633 | + |
|
| 634 | + $short = []; |
|
| 635 | + $long = []; |
|
| 636 | + while (count($colors)) { |
|
| 637 | + $c = array_shift($colors); |
|
| 638 | + if (strlen($c) == 4) { |
|
| 639 | + $short[] = $c; |
|
| 640 | + } |
|
| 641 | + else { |
|
| 642 | + $long[] = $c; |
|
| 643 | + } |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + $colors = array_merge($long, $short); |
|
| 647 | + $new_colors = []; |
|
| 648 | + $colors = array_flip($colors); |
|
| 649 | + foreach ($colors as $c => $k) { |
|
| 650 | + $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + |
|
| 654 | + foreach ($colors as $original => $replace) { |
|
| 655 | + $new = svg_couleur_to_hexa($original); |
|
| 656 | + $new_colors[$replace] = $callback_filter($new); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 660 | + $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 661 | + |
|
| 662 | + return $svg; |
|
| 663 | + } |
|
| 664 | + return $img; |
|
| 665 | 665 | } |
| 666 | 666 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | if (!defined('IMG_SVG')) { |
| 24 | 24 | // complete IMG_BMP | IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM | IMG_WEBP |
| 25 | - define('IMG_SVG',128); |
|
| 25 | + define('IMG_SVG', 128); |
|
| 26 | 26 | define('IMAGETYPE_SVG', 19); |
| 27 | 27 | } |
| 28 | 28 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @return bool|string |
| 39 | 39 | * false si on a pas pu charger l'image |
| 40 | 40 | */ |
| 41 | -function svg_charger($fichier, $maxlen=null) { |
|
| 41 | +function svg_charger($fichier, $maxlen = null) { |
|
| 42 | 42 | if (strpos($fichier, "data:image/svg+xml") === 0) { |
| 43 | 43 | $image = explode(";", $fichier, 2); |
| 44 | 44 | $image = end($image); |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | return $fichier; |
| 57 | 57 | } |
| 58 | 58 | if (!file_exists($fichier)) { |
| 59 | - $fichier = supprimer_timestamp($fichier); |
|
| 59 | + $fichier = supprimer_timestamp($fichier); |
|
| 60 | 60 | if (!file_exists($fichier)) { |
| 61 | 61 | return false; |
| 62 | 62 | } |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | $image = file_get_contents($fichier); |
| 66 | 66 | } |
| 67 | 67 | else { |
| 68 | - $image = file_get_contents($fichier, false,null,0, $maxlen); |
|
| 68 | + $image = file_get_contents($fichier, false, null, 0, $maxlen); |
|
| 69 | 69 | } |
| 70 | 70 | // est-ce bien une image svg ? |
| 71 | 71 | if (strpos($image, "<svg") !== false) { |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | if (($ps = stripos($debut_fichier, "<svg")) !== false) { |
| 88 | 88 | |
| 89 | 89 | $pe = stripos($debut_fichier, ">", $ps); |
| 90 | - $balise_svg = substr($debut_fichier, $ps, $pe - $ps +1); |
|
| 90 | + $balise_svg = substr($debut_fichier, $ps, $pe - $ps + 1); |
|
| 91 | 91 | |
| 92 | 92 | if (preg_match_all(",([\w:\-]+)=,Uims", $balise_svg, $matches)) { |
| 93 | 93 | if (!function_exists('extraire_attribut')) { |
@@ -127,29 +127,29 @@ discard block |
||
| 127 | 127 | * @return bool|float|int |
| 128 | 128 | */ |
| 129 | 129 | function svg_dimension_to_pixels($dimension) { |
| 130 | - if (preg_match(',^(-?\d+)([^\d]*),i', trim($dimension), $m)){ |
|
| 130 | + if (preg_match(',^(-?\d+)([^\d]*),i', trim($dimension), $m)) { |
|
| 131 | 131 | switch (strtolower($m[2])) { |
| 132 | 132 | case '%': |
| 133 | 133 | // on ne sait pas faire :( |
| 134 | 134 | return false; |
| 135 | 135 | break; |
| 136 | 136 | case 'em': |
| 137 | - return intval($m[1])*16; // 16px font-size par defaut |
|
| 137 | + return intval($m[1]) * 16; // 16px font-size par defaut |
|
| 138 | 138 | break; |
| 139 | 139 | case 'ex': |
| 140 | - return intval($m[1])*16; // 16px font-size par defaut |
|
| 140 | + return intval($m[1]) * 16; // 16px font-size par defaut |
|
| 141 | 141 | break; |
| 142 | 142 | case 'pc': |
| 143 | - return intval($m[1])*16; // 1/6 inch = 96px/6 in CSS |
|
| 143 | + return intval($m[1]) * 16; // 1/6 inch = 96px/6 in CSS |
|
| 144 | 144 | break; |
| 145 | 145 | case 'cm': |
| 146 | - return intval(round($m[1]*96/2.54)); // 96px / 2.54cm; |
|
| 146 | + return intval(round($m[1] * 96 / 2.54)); // 96px / 2.54cm; |
|
| 147 | 147 | break; |
| 148 | 148 | case 'mm': |
| 149 | - return intval(round($m[1]*96/25.4)); // 96px / 25.4mm; |
|
| 149 | + return intval(round($m[1] * 96 / 25.4)); // 96px / 25.4mm; |
|
| 150 | 150 | break; |
| 151 | 151 | case 'in': |
| 152 | - return intval($m[1])*96; // 1 inch = 96px in CSS |
|
| 152 | + return intval($m[1]) * 96; // 1 inch = 96px in CSS |
|
| 153 | 153 | break; |
| 154 | 154 | case 'px': |
| 155 | 155 | case 'pt': |
@@ -187,15 +187,15 @@ discard block |
||
| 187 | 187 | * inserer au debut (true) ou a la fin (false) |
| 188 | 188 | * @return string |
| 189 | 189 | */ |
| 190 | -function svg_insert_shapes($svg, $shapes, $start=true) { |
|
| 190 | +function svg_insert_shapes($svg, $shapes, $start = true) { |
|
| 191 | 191 | |
| 192 | 192 | if ($start === false or $start === 'end') { |
| 193 | - $svg = str_replace("</svg>", $shapes . "</svg>", $svg); |
|
| 193 | + $svg = str_replace("</svg>", $shapes."</svg>", $svg); |
|
| 194 | 194 | } |
| 195 | 195 | else { |
| 196 | 196 | $p = stripos($svg, "<svg"); |
| 197 | 197 | $p = strpos($svg, ">", $p); |
| 198 | - $svg = substr_replace($svg, $shapes, $p+1, 0); |
|
| 198 | + $svg = substr_replace($svg, $shapes, $p + 1, 0); |
|
| 199 | 199 | } |
| 200 | 200 | return $svg; |
| 201 | 201 | } |
@@ -211,10 +211,10 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | function svg_clip_in_box($svg, $x, $y, $width, $height) { |
| 213 | 213 | $rect = "<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\" />"; |
| 214 | - $id = 'clip-'.substr(md5($rect . strlen($svg)),0,8); |
|
| 214 | + $id = 'clip-'.substr(md5($rect.strlen($svg)), 0, 8); |
|
| 215 | 215 | $clippath = "<clipPath id=\"$id\">$rect</clipPath>"; |
| 216 | 216 | $g = "<g clip-path=\"url(#$id)\">"; |
| 217 | - $svg = svg_insert_shapes($svg, $clippath . $g); |
|
| 217 | + $svg = svg_insert_shapes($svg, $clippath.$g); |
|
| 218 | 218 | $svg = svg_insert_shapes($svg, "</g>", false); |
| 219 | 219 | return $svg; |
| 220 | 220 | } |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | list($balise_svg, $attributs) = $svg_infos; |
| 234 | 234 | if (!isset($attributs['viewBox'])) { |
| 235 | - $attributs['viewBox'] = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 235 | + $attributs['viewBox'] = "0 0 ".$attributs['width']." ".$attributs['height']; |
|
| 236 | 236 | } |
| 237 | 237 | $attributs['width'] = strval($new_width); |
| 238 | 238 | $attributs['height'] = strval($new_height); |
@@ -250,14 +250,14 @@ discard block |
||
| 250 | 250 | * @return string |
| 251 | 251 | */ |
| 252 | 252 | function svg_couleur_to_hexa($couleur) { |
| 253 | - if (strpos($couleur, "rgb(")===0) { |
|
| 253 | + if (strpos($couleur, "rgb(") === 0) { |
|
| 254 | 254 | $c = explode(',', substr($couleur, 4)); |
| 255 | 255 | $couleur = _couleur_dec_to_hex(intval($c[0]), intval($c[1]), intval($c[2])); |
| 256 | 256 | } |
| 257 | 257 | else { |
| 258 | 258 | $couleur = couleur_html_to_hex($couleur); |
| 259 | 259 | } |
| 260 | - $couleur = '#'.ltrim($couleur,'#'); |
|
| 260 | + $couleur = '#'.ltrim($couleur, '#'); |
|
| 261 | 261 | return $couleur; |
| 262 | 262 | } |
| 263 | 263 | |
@@ -267,9 +267,9 @@ discard block |
||
| 267 | 267 | * @return array |
| 268 | 268 | */ |
| 269 | 269 | function svg_couleur_to_rgb($couleur) { |
| 270 | - if (strpos($couleur, "rgb(")===0) { |
|
| 270 | + if (strpos($couleur, "rgb(") === 0) { |
|
| 271 | 271 | $c = explode(',', substr($couleur, 4)); |
| 272 | - return ['red' => intval($c[0]),'green' => intval($c[1]),'blue' => intval($c[2])]; |
|
| 272 | + return ['red' => intval($c[0]), 'green' => intval($c[1]), 'blue' => intval($c[2])]; |
|
| 273 | 273 | } |
| 274 | 274 | return _couleur_hex_to_dec($couleur); |
| 275 | 275 | } |
@@ -289,13 +289,13 @@ discard block |
||
| 289 | 289 | */ |
| 290 | 290 | function svg_force_viewBox_px($img, $force_width_and_height = false) { |
| 291 | 291 | if ($svg = svg_charger($img) |
| 292 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 292 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 293 | 293 | |
| 294 | 294 | list($balise_svg, $attributs) = $svg_infos; |
| 295 | 295 | |
| 296 | 296 | // il nous faut une viewBox |
| 297 | 297 | if (!isset($attributs['viewBox'])) { |
| 298 | - $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 298 | + $viewBox = "0 0 ".$attributs['width']." ".$attributs['height']; |
|
| 299 | 299 | } |
| 300 | 300 | else { |
| 301 | 301 | $viewBox = $attributs['viewBox']; |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | * @param $new_height |
| 360 | 360 | * @return bool|string |
| 361 | 361 | */ |
| 362 | -function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color='') { |
|
| 362 | +function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color = '') { |
|
| 363 | 363 | if ($svg = svg_force_viewBox_px($img) |
| 364 | 364 | and $svg_infos = svg_lire_balise_svg($svg)) { |
| 365 | 365 | |
@@ -385,7 +385,7 @@ discard block |
||
| 385 | 385 | $viewport_oy = intval(round($viewport_oy * $yscale)); |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 388 | + if ($viewport_w > $viewBox[2] or $viewport_h > $viewBox[3]) { |
|
| 389 | 389 | $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
| 390 | 390 | } |
| 391 | 391 | |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
| 403 | 403 | |
| 404 | 404 | // ajouter un background |
| 405 | - if ($background_color and $background_color!=='transparent') { |
|
| 405 | + if ($background_color and $background_color !== 'transparent') { |
|
| 406 | 406 | $svg = svg_ajouter_background($svg, $background_color); |
| 407 | 407 | } |
| 408 | 408 | |
@@ -420,9 +420,9 @@ discard block |
||
| 420 | 420 | */ |
| 421 | 421 | function svg_ajouter_background($img, $background_color) { |
| 422 | 422 | if ($svg = svg_charger($img) |
| 423 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 423 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 424 | 424 | |
| 425 | - if ($background_color and $background_color!=='transparent') { |
|
| 425 | + if ($background_color and $background_color !== 'transparent') { |
|
| 426 | 426 | list($balise_svg, $attributs) = $svg_infos; |
| 427 | 427 | |
| 428 | 428 | $background_color = svg_couleur_to_hexa($background_color); |
@@ -449,9 +449,9 @@ discard block |
||
| 449 | 449 | */ |
| 450 | 450 | function svg_ajouter_voile($img, $background_color, $opacity) { |
| 451 | 451 | if ($svg = svg_charger($img) |
| 452 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 452 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 453 | 453 | |
| 454 | - if ($background_color and $background_color!=='transparent') { |
|
| 454 | + if ($background_color and $background_color !== 'transparent') { |
|
| 455 | 455 | list($balise_svg, $attributs) = $svg_infos; |
| 456 | 456 | |
| 457 | 457 | $background_color = svg_couleur_to_hexa($background_color); |
@@ -478,10 +478,10 @@ discard block |
||
| 478 | 478 | */ |
| 479 | 479 | function svg_transformer($img, $attributs) { |
| 480 | 480 | if ($svg = svg_charger($img) |
| 481 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 481 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 482 | 482 | |
| 483 | 483 | if ($attributs) { |
| 484 | - list($balise_svg, ) = $svg_infos; |
|
| 484 | + list($balise_svg,) = $svg_infos; |
|
| 485 | 485 | $g = "<g"; |
| 486 | 486 | foreach ($attributs as $k=>$v) { |
| 487 | 487 | if (strlen($v)) { |
@@ -508,14 +508,14 @@ discard block |
||
| 508 | 508 | */ |
| 509 | 509 | function svg_apply_filter($img, $filter_def) { |
| 510 | 510 | if ($svg = svg_charger($img) |
| 511 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 511 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 512 | 512 | |
| 513 | 513 | if ($filter_def) { |
| 514 | - list($balise_svg, ) = $svg_infos; |
|
| 515 | - $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 514 | + list($balise_svg,) = $svg_infos; |
|
| 515 | + $filter_id = "filter-".substr(md5($filter_def.strlen($svg)), 0, 8); |
|
| 516 | 516 | $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
| 517 | 517 | $g = "<g filter=\"url(#$filter_id)\">"; |
| 518 | - $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 518 | + $svg = svg_insert_shapes($svg, $filter.$g); |
|
| 519 | 519 | $svg = svg_insert_shapes($svg, "</g>", false); |
| 520 | 520 | } |
| 521 | 521 | return $svg; |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | */ |
| 567 | 567 | function svg_flip($img, $HorV) { |
| 568 | 568 | if ($svg = svg_force_viewBox_px($img) |
| 569 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 569 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 570 | 570 | |
| 571 | 571 | list($balise_svg, $atts) = $svg_infos; |
| 572 | 572 | $viewBox = explode(' ', $atts['viewBox']); |
@@ -574,14 +574,14 @@ discard block |
||
| 574 | 574 | if (!in_array($HorV, ['h', 'H'])) { |
| 575 | 575 | $transform = "scale(-1,1)"; |
| 576 | 576 | |
| 577 | - $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 577 | + $x = intval($viewBox[0]) + intval($viewBox[2] / 2); |
|
| 578 | 578 | $mx = -$x; |
| 579 | 579 | $transform = "translate($x, 0) $transform translate($mx, 0)"; |
| 580 | 580 | } |
| 581 | 581 | else { |
| 582 | 582 | $transform = "scale(1,-1)"; |
| 583 | 583 | |
| 584 | - $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 584 | + $y = intval($viewBox[1]) + intval($viewBox[3] / 2); |
|
| 585 | 585 | $my = -$y; |
| 586 | 586 | $transform = "translate(0, $y) $transform translate(0, $my)"; |
| 587 | 587 | } |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | function svg_rotate($img, $angle, $center_x, $center_y) { |
| 605 | 605 | if ($svg = svg_force_viewBox_px($img) |
| 606 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 606 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 607 | 607 | |
| 608 | 608 | list($balise_svg, $atts) = $svg_infos; |
| 609 | 609 | $viewBox = explode(' ', $atts['viewBox']); |