@@ -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 | |
@@ -287,35 +287,35 @@ discard block |
||
| 287 | 287 | * @return string |
| 288 | 288 | */ |
| 289 | 289 | function svg_force_viewBox_px($img) { |
| 290 | - if ($svg = svg_charger($img) |
|
| 291 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 292 | - |
|
| 293 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 294 | - |
|
| 295 | - // il nous faut une viewBox |
|
| 296 | - if (!isset($attributs['viewBox'])) { |
|
| 297 | - $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 298 | - } |
|
| 299 | - else { |
|
| 300 | - $viewBox = $attributs['viewBox']; |
|
| 301 | - } |
|
| 302 | - // et on la convertit en px |
|
| 303 | - $viewBox = explode(' ', $viewBox); |
|
| 304 | - $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 305 | - $viewBox = array_map('intval', $viewBox); |
|
| 306 | - if (!$viewBox[2]) { |
|
| 307 | - $viewBox[2] = '300'; |
|
| 308 | - } |
|
| 309 | - if (!$viewBox[3]) { |
|
| 310 | - $viewBox[3] = '150'; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 314 | - |
|
| 315 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 316 | - return $svg; |
|
| 317 | - } |
|
| 318 | - return $img; |
|
| 290 | + if ($svg = svg_charger($img) |
|
| 291 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 292 | + |
|
| 293 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 294 | + |
|
| 295 | + // il nous faut une viewBox |
|
| 296 | + if (!isset($attributs['viewBox'])) { |
|
| 297 | + $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 298 | + } |
|
| 299 | + else { |
|
| 300 | + $viewBox = $attributs['viewBox']; |
|
| 301 | + } |
|
| 302 | + // et on la convertit en px |
|
| 303 | + $viewBox = explode(' ', $viewBox); |
|
| 304 | + $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 305 | + $viewBox = array_map('intval', $viewBox); |
|
| 306 | + if (!$viewBox[2]) { |
|
| 307 | + $viewBox[2] = '300'; |
|
| 308 | + } |
|
| 309 | + if (!$viewBox[3]) { |
|
| 310 | + $viewBox[3] = '150'; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 314 | + |
|
| 315 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 316 | + return $svg; |
|
| 317 | + } |
|
| 318 | + return $img; |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /** |
@@ -324,12 +324,12 @@ discard block |
||
| 324 | 324 | * @return array|mixed |
| 325 | 325 | */ |
| 326 | 326 | function svg_extract_couleurs($img) { |
| 327 | - if ($svg = svg_charger($img)) { |
|
| 328 | - 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)) { |
|
| 329 | - return $matches[0]; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - return []; |
|
| 327 | + if ($svg = svg_charger($img)) { |
|
| 328 | + 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)) { |
|
| 329 | + return $matches[0]; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + return []; |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | /** |
@@ -340,56 +340,56 @@ discard block |
||
| 340 | 340 | * @return bool|string |
| 341 | 341 | */ |
| 342 | 342 | function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color='') { |
| 343 | - if ($svg = svg_force_viewBox_px($img) |
|
| 344 | - and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 345 | - |
|
| 346 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 347 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 348 | - |
|
| 349 | - $viewport_w = $new_width; |
|
| 350 | - $viewport_h = $new_height; |
|
| 351 | - $viewport_ox = $offset_width; |
|
| 352 | - $viewport_oy = $offset_height; |
|
| 353 | - |
|
| 354 | - // si on a un width/height qui rescale, il faut rescaler |
|
| 355 | - if (isset ($attributs['width']) |
|
| 356 | - and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 357 | - and isset ($attributs['height']) |
|
| 358 | - and $h = svg_dimension_to_pixels($attributs['height'])) { |
|
| 359 | - |
|
| 360 | - $xscale = $viewBox[2] / $w; |
|
| 361 | - $viewport_w = intval(round($viewport_w * $xscale)); |
|
| 362 | - $viewport_ox = intval(round($viewport_ox * $xscale)); |
|
| 363 | - $yscale = $viewBox[3] / $h; |
|
| 364 | - $viewport_h = intval(round($viewport_h * $yscale)); |
|
| 365 | - $viewport_oy = intval(round($viewport_oy * $yscale)); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 369 | - $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - // maintenant on redefinit la viewBox |
|
| 373 | - $viewBox[0] += $viewport_ox; |
|
| 374 | - $viewBox[1] += $viewport_oy; |
|
| 375 | - $viewBox[2] = $viewport_w; |
|
| 376 | - $viewBox[3] = $viewport_h; |
|
| 377 | - |
|
| 378 | - $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 379 | - $attributs['width'] = strval($new_width); |
|
| 380 | - $attributs['height'] = strval($new_height); |
|
| 381 | - |
|
| 382 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 383 | - |
|
| 384 | - // ajouter un background |
|
| 385 | - if ($background_color and $background_color!=='transparent') { |
|
| 386 | - $svg = svg_ajouter_background($svg, $background_color); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return $svg; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return $img; |
|
| 343 | + if ($svg = svg_force_viewBox_px($img) |
|
| 344 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 345 | + |
|
| 346 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 347 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 348 | + |
|
| 349 | + $viewport_w = $new_width; |
|
| 350 | + $viewport_h = $new_height; |
|
| 351 | + $viewport_ox = $offset_width; |
|
| 352 | + $viewport_oy = $offset_height; |
|
| 353 | + |
|
| 354 | + // si on a un width/height qui rescale, il faut rescaler |
|
| 355 | + if (isset ($attributs['width']) |
|
| 356 | + and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 357 | + and isset ($attributs['height']) |
|
| 358 | + and $h = svg_dimension_to_pixels($attributs['height'])) { |
|
| 359 | + |
|
| 360 | + $xscale = $viewBox[2] / $w; |
|
| 361 | + $viewport_w = intval(round($viewport_w * $xscale)); |
|
| 362 | + $viewport_ox = intval(round($viewport_ox * $xscale)); |
|
| 363 | + $yscale = $viewBox[3] / $h; |
|
| 364 | + $viewport_h = intval(round($viewport_h * $yscale)); |
|
| 365 | + $viewport_oy = intval(round($viewport_oy * $yscale)); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 369 | + $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + // maintenant on redefinit la viewBox |
|
| 373 | + $viewBox[0] += $viewport_ox; |
|
| 374 | + $viewBox[1] += $viewport_oy; |
|
| 375 | + $viewBox[2] = $viewport_w; |
|
| 376 | + $viewBox[3] = $viewport_h; |
|
| 377 | + |
|
| 378 | + $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 379 | + $attributs['width'] = strval($new_width); |
|
| 380 | + $attributs['height'] = strval($new_height); |
|
| 381 | + |
|
| 382 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 383 | + |
|
| 384 | + // ajouter un background |
|
| 385 | + if ($background_color and $background_color!=='transparent') { |
|
| 386 | + $svg = svg_ajouter_background($svg, $background_color); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return $svg; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return $img; |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -399,25 +399,25 @@ discard block |
||
| 399 | 399 | * @return bool|string |
| 400 | 400 | */ |
| 401 | 401 | function svg_ajouter_background($img, $background_color) { |
| 402 | - if ($svg = svg_charger($img) |
|
| 403 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 404 | - |
|
| 405 | - if ($background_color and $background_color!=='transparent') { |
|
| 406 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 407 | - |
|
| 408 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 409 | - if (isset($attributs['viewBox'])) { |
|
| 410 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 411 | - $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\"/>"; |
|
| 412 | - } |
|
| 413 | - else { |
|
| 414 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 415 | - } |
|
| 416 | - $svg = svg_insert_shapes($svg, $rect); |
|
| 417 | - } |
|
| 418 | - return $svg; |
|
| 419 | - } |
|
| 420 | - return $img; |
|
| 402 | + if ($svg = svg_charger($img) |
|
| 403 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 404 | + |
|
| 405 | + if ($background_color and $background_color!=='transparent') { |
|
| 406 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 407 | + |
|
| 408 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 409 | + if (isset($attributs['viewBox'])) { |
|
| 410 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 411 | + $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\"/>"; |
|
| 412 | + } |
|
| 413 | + else { |
|
| 414 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 415 | + } |
|
| 416 | + $svg = svg_insert_shapes($svg, $rect); |
|
| 417 | + } |
|
| 418 | + return $svg; |
|
| 419 | + } |
|
| 420 | + return $img; |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | 423 | |
@@ -428,25 +428,25 @@ discard block |
||
| 428 | 428 | * @return bool|string |
| 429 | 429 | */ |
| 430 | 430 | function svg_ajouter_voile($img, $background_color, $opacity) { |
| 431 | - if ($svg = svg_charger($img) |
|
| 432 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 433 | - |
|
| 434 | - if ($background_color and $background_color!=='transparent') { |
|
| 435 | - list($balise_svg, $attributs) = $svg_infos; |
|
| 436 | - |
|
| 437 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 438 | - if (isset($attributs['viewBox'])) { |
|
| 439 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 440 | - $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 441 | - } |
|
| 442 | - else { |
|
| 443 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 444 | - } |
|
| 445 | - $svg = svg_insert_shapes($svg, $rect, false); |
|
| 446 | - } |
|
| 447 | - return $svg; |
|
| 448 | - } |
|
| 449 | - return $img; |
|
| 431 | + if ($svg = svg_charger($img) |
|
| 432 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 433 | + |
|
| 434 | + if ($background_color and $background_color!=='transparent') { |
|
| 435 | + list($balise_svg, $attributs) = $svg_infos; |
|
| 436 | + |
|
| 437 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 438 | + if (isset($attributs['viewBox'])) { |
|
| 439 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 440 | + $rect = "<rect x=\"".$viewBox[0]."\" y=\"".$viewBox[1]."\" width=\"".$viewBox[2]."\" height=\"".$viewBox[3]."\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 441 | + } |
|
| 442 | + else { |
|
| 443 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 444 | + } |
|
| 445 | + $svg = svg_insert_shapes($svg, $rect, false); |
|
| 446 | + } |
|
| 447 | + return $svg; |
|
| 448 | + } |
|
| 449 | + return $img; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | |
@@ -457,26 +457,26 @@ discard block |
||
| 457 | 457 | * @return bool|string |
| 458 | 458 | */ |
| 459 | 459 | function svg_transformer($img, $attributs) { |
| 460 | - if ($svg = svg_charger($img) |
|
| 461 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 462 | - |
|
| 463 | - if ($attributs) { |
|
| 464 | - list($balise_svg, ) = $svg_infos; |
|
| 465 | - $g = "<g"; |
|
| 466 | - foreach ($attributs as $k=>$v) { |
|
| 467 | - if (strlen($v)) { |
|
| 468 | - $g .= " $k=\"".attribut_html($v)."\""; |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - if (strlen($g) > 2) { |
|
| 472 | - $g .= ">"; |
|
| 473 | - $svg = svg_insert_shapes($svg, $g); |
|
| 474 | - $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - return $svg; |
|
| 478 | - } |
|
| 479 | - return $img; |
|
| 460 | + if ($svg = svg_charger($img) |
|
| 461 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 462 | + |
|
| 463 | + if ($attributs) { |
|
| 464 | + list($balise_svg, ) = $svg_infos; |
|
| 465 | + $g = "<g"; |
|
| 466 | + foreach ($attributs as $k=>$v) { |
|
| 467 | + if (strlen($v)) { |
|
| 468 | + $g .= " $k=\"".attribut_html($v)."\""; |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + if (strlen($g) > 2) { |
|
| 472 | + $g .= ">"; |
|
| 473 | + $svg = svg_insert_shapes($svg, $g); |
|
| 474 | + $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + return $svg; |
|
| 478 | + } |
|
| 479 | + return $img; |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -487,20 +487,20 @@ discard block |
||
| 487 | 487 | * @return bool|string |
| 488 | 488 | */ |
| 489 | 489 | function svg_apply_filter($img, $filter_def) { |
| 490 | - if ($svg = svg_charger($img) |
|
| 491 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 492 | - |
|
| 493 | - if ($filter_def) { |
|
| 494 | - list($balise_svg, ) = $svg_infos; |
|
| 495 | - $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 496 | - $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 497 | - $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 498 | - $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 499 | - $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 500 | - } |
|
| 501 | - return $svg; |
|
| 502 | - } |
|
| 503 | - return $img; |
|
| 490 | + if ($svg = svg_charger($img) |
|
| 491 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 492 | + |
|
| 493 | + if ($filter_def) { |
|
| 494 | + list($balise_svg, ) = $svg_infos; |
|
| 495 | + $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 496 | + $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 497 | + $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 498 | + $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 499 | + $svg = svg_insert_shapes($svg, "</g>", false); |
|
| 500 | + } |
|
| 501 | + return $svg; |
|
| 502 | + } |
|
| 503 | + return $img; |
|
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | /** |
@@ -510,8 +510,8 @@ discard block |
||
| 510 | 510 | * @return string |
| 511 | 511 | */ |
| 512 | 512 | function svg_filter_blur($img, $blur_width) { |
| 513 | - $blur_width = intval($blur_width); |
|
| 514 | - return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 513 | + $blur_width = intval($blur_width); |
|
| 514 | + return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | /** |
@@ -521,10 +521,10 @@ discard block |
||
| 521 | 521 | * @return bool|string |
| 522 | 522 | */ |
| 523 | 523 | function svg_filter_grayscale($img, $intensity) { |
| 524 | - $value = round(1.0 - $intensity, 2); |
|
| 525 | - //$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\"/>"; |
|
| 526 | - $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 527 | - return svg_apply_filter($img, $filter); |
|
| 524 | + $value = round(1.0 - $intensity, 2); |
|
| 525 | + //$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\"/>"; |
|
| 526 | + $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 527 | + return svg_apply_filter($img, $filter); |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | /** |
@@ -534,8 +534,8 @@ discard block |
||
| 534 | 534 | * @return bool|string |
| 535 | 535 | */ |
| 536 | 536 | function svg_filter_sepia($img, $intensity) { |
| 537 | - $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\"/>"; |
|
| 538 | - return svg_apply_filter($img, $filter); |
|
| 537 | + $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\"/>"; |
|
| 538 | + return svg_apply_filter($img, $filter); |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | /** |
@@ -545,30 +545,30 @@ discard block |
||
| 545 | 545 | * @return bool|string |
| 546 | 546 | */ |
| 547 | 547 | function svg_flip($img, $HorV) { |
| 548 | - if ($svg = svg_force_viewBox_px($img) |
|
| 549 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 550 | - |
|
| 551 | - list($balise_svg, $atts) = $svg_infos; |
|
| 552 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 553 | - |
|
| 554 | - if (!in_array($HorV, ['h', 'H'])) { |
|
| 555 | - $transform = "scale(-1,1)"; |
|
| 556 | - |
|
| 557 | - $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 558 | - $mx = -$x; |
|
| 559 | - $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 560 | - } |
|
| 561 | - else { |
|
| 562 | - $transform = "scale(1,-1)"; |
|
| 563 | - |
|
| 564 | - $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 565 | - $my = -$y; |
|
| 566 | - $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 567 | - } |
|
| 568 | - $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 569 | - return $svg; |
|
| 570 | - } |
|
| 571 | - return $img; |
|
| 548 | + if ($svg = svg_force_viewBox_px($img) |
|
| 549 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 550 | + |
|
| 551 | + list($balise_svg, $atts) = $svg_infos; |
|
| 552 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 553 | + |
|
| 554 | + if (!in_array($HorV, ['h', 'H'])) { |
|
| 555 | + $transform = "scale(-1,1)"; |
|
| 556 | + |
|
| 557 | + $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 558 | + $mx = -$x; |
|
| 559 | + $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 560 | + } |
|
| 561 | + else { |
|
| 562 | + $transform = "scale(1,-1)"; |
|
| 563 | + |
|
| 564 | + $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 565 | + $my = -$y; |
|
| 566 | + $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 567 | + } |
|
| 568 | + $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 569 | + return $svg; |
|
| 570 | + } |
|
| 571 | + return $img; |
|
| 572 | 572 | } |
| 573 | 573 | |
| 574 | 574 | /** |
@@ -582,19 +582,19 @@ discard block |
||
| 582 | 582 | * @return bool|string |
| 583 | 583 | */ |
| 584 | 584 | function svg_rotate($img, $angle, $center_x, $center_y) { |
| 585 | - if ($svg = svg_force_viewBox_px($img) |
|
| 586 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 585 | + if ($svg = svg_force_viewBox_px($img) |
|
| 586 | + and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 587 | 587 | |
| 588 | - list($balise_svg, $atts) = $svg_infos; |
|
| 589 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 588 | + list($balise_svg, $atts) = $svg_infos; |
|
| 589 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 590 | 590 | |
| 591 | - $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 592 | - $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 593 | - $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 591 | + $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 592 | + $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 593 | + $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 594 | 594 | |
| 595 | - return $svg; |
|
| 596 | - } |
|
| 597 | - return $img; |
|
| 595 | + return $svg; |
|
| 596 | + } |
|
| 597 | + return $img; |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | /** |
@@ -606,40 +606,40 @@ discard block |
||
| 606 | 606 | * @return bool|mixed|string |
| 607 | 607 | */ |
| 608 | 608 | function svg_filtrer_couleurs($img, $callback_filter) { |
| 609 | - if ($svg = svg_force_viewBox_px($img) |
|
| 610 | - and $colors = svg_extract_couleurs($svg)) { |
|
| 611 | - |
|
| 612 | - $colors = array_unique($colors); |
|
| 613 | - |
|
| 614 | - $short = []; |
|
| 615 | - $long = []; |
|
| 616 | - while (count($colors)) { |
|
| 617 | - $c = array_shift($colors); |
|
| 618 | - if (strlen($c) == 4) { |
|
| 619 | - $short[] = $c; |
|
| 620 | - } |
|
| 621 | - else { |
|
| 622 | - $long[] = $c; |
|
| 623 | - } |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - $colors = array_merge($long, $short); |
|
| 627 | - $new_colors = []; |
|
| 628 | - $colors = array_flip($colors); |
|
| 629 | - foreach ($colors as $c => $k) { |
|
| 630 | - $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - foreach ($colors as $original => $replace) { |
|
| 635 | - $new = svg_couleur_to_hexa($original); |
|
| 636 | - $new_colors[$replace] = $callback_filter($new); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 640 | - $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 641 | - |
|
| 642 | - return $svg; |
|
| 643 | - } |
|
| 644 | - return $img; |
|
| 609 | + if ($svg = svg_force_viewBox_px($img) |
|
| 610 | + and $colors = svg_extract_couleurs($svg)) { |
|
| 611 | + |
|
| 612 | + $colors = array_unique($colors); |
|
| 613 | + |
|
| 614 | + $short = []; |
|
| 615 | + $long = []; |
|
| 616 | + while (count($colors)) { |
|
| 617 | + $c = array_shift($colors); |
|
| 618 | + if (strlen($c) == 4) { |
|
| 619 | + $short[] = $c; |
|
| 620 | + } |
|
| 621 | + else { |
|
| 622 | + $long[] = $c; |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + $colors = array_merge($long, $short); |
|
| 627 | + $new_colors = []; |
|
| 628 | + $colors = array_flip($colors); |
|
| 629 | + foreach ($colors as $c => $k) { |
|
| 630 | + $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + foreach ($colors as $original => $replace) { |
|
| 635 | + $new = svg_couleur_to_hexa($original); |
|
| 636 | + $new_colors[$replace] = $callback_filter($new); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 640 | + $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 641 | + |
|
| 642 | + return $svg; |
|
| 643 | + } |
|
| 644 | + return $img; |
|
| 645 | 645 | } |
| 646 | 646 | \ 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 | } |
@@ -288,13 +288,13 @@ discard block |
||
| 288 | 288 | */ |
| 289 | 289 | function svg_force_viewBox_px($img) { |
| 290 | 290 | if ($svg = svg_charger($img) |
| 291 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 291 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 292 | 292 | |
| 293 | 293 | list($balise_svg, $attributs) = $svg_infos; |
| 294 | 294 | |
| 295 | 295 | // il nous faut une viewBox |
| 296 | 296 | if (!isset($attributs['viewBox'])) { |
| 297 | - $viewBox = "0 0 " . $attributs['width'] . " " . $attributs['height']; |
|
| 297 | + $viewBox = "0 0 ".$attributs['width']." ".$attributs['height']; |
|
| 298 | 298 | } |
| 299 | 299 | else { |
| 300 | 300 | $viewBox = $attributs['viewBox']; |
@@ -339,7 +339,7 @@ discard block |
||
| 339 | 339 | * @param $new_height |
| 340 | 340 | * @return bool|string |
| 341 | 341 | */ |
| 342 | -function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color='') { |
|
| 342 | +function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color = '') { |
|
| 343 | 343 | if ($svg = svg_force_viewBox_px($img) |
| 344 | 344 | and $svg_infos = svg_lire_balise_svg($svg)) { |
| 345 | 345 | |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | $viewport_oy = intval(round($viewport_oy * $yscale)); |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if ($viewport_w>$viewBox[2] or $viewport_h>$viewBox[3]) { |
|
| 368 | + if ($viewport_w > $viewBox[2] or $viewport_h > $viewBox[3]) { |
|
| 369 | 369 | $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
| 370 | 370 | } |
| 371 | 371 | |
@@ -382,7 +382,7 @@ discard block |
||
| 382 | 382 | $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
| 383 | 383 | |
| 384 | 384 | // ajouter un background |
| 385 | - if ($background_color and $background_color!=='transparent') { |
|
| 385 | + if ($background_color and $background_color !== 'transparent') { |
|
| 386 | 386 | $svg = svg_ajouter_background($svg, $background_color); |
| 387 | 387 | } |
| 388 | 388 | |
@@ -400,9 +400,9 @@ discard block |
||
| 400 | 400 | */ |
| 401 | 401 | function svg_ajouter_background($img, $background_color) { |
| 402 | 402 | if ($svg = svg_charger($img) |
| 403 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 403 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 404 | 404 | |
| 405 | - if ($background_color and $background_color!=='transparent') { |
|
| 405 | + if ($background_color and $background_color !== 'transparent') { |
|
| 406 | 406 | list($balise_svg, $attributs) = $svg_infos; |
| 407 | 407 | |
| 408 | 408 | $background_color = svg_couleur_to_hexa($background_color); |
@@ -429,9 +429,9 @@ discard block |
||
| 429 | 429 | */ |
| 430 | 430 | function svg_ajouter_voile($img, $background_color, $opacity) { |
| 431 | 431 | if ($svg = svg_charger($img) |
| 432 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 432 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 433 | 433 | |
| 434 | - if ($background_color and $background_color!=='transparent') { |
|
| 434 | + if ($background_color and $background_color !== 'transparent') { |
|
| 435 | 435 | list($balise_svg, $attributs) = $svg_infos; |
| 436 | 436 | |
| 437 | 437 | $background_color = svg_couleur_to_hexa($background_color); |
@@ -458,10 +458,10 @@ discard block |
||
| 458 | 458 | */ |
| 459 | 459 | function svg_transformer($img, $attributs) { |
| 460 | 460 | if ($svg = svg_charger($img) |
| 461 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 461 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 462 | 462 | |
| 463 | 463 | if ($attributs) { |
| 464 | - list($balise_svg, ) = $svg_infos; |
|
| 464 | + list($balise_svg,) = $svg_infos; |
|
| 465 | 465 | $g = "<g"; |
| 466 | 466 | foreach ($attributs as $k=>$v) { |
| 467 | 467 | if (strlen($v)) { |
@@ -488,14 +488,14 @@ discard block |
||
| 488 | 488 | */ |
| 489 | 489 | function svg_apply_filter($img, $filter_def) { |
| 490 | 490 | if ($svg = svg_charger($img) |
| 491 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 491 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 492 | 492 | |
| 493 | 493 | if ($filter_def) { |
| 494 | - list($balise_svg, ) = $svg_infos; |
|
| 495 | - $filter_id = "filter-". substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 494 | + list($balise_svg,) = $svg_infos; |
|
| 495 | + $filter_id = "filter-".substr(md5($filter_def.strlen($svg)), 0, 8); |
|
| 496 | 496 | $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
| 497 | 497 | $g = "<g filter=\"url(#$filter_id)\">"; |
| 498 | - $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 498 | + $svg = svg_insert_shapes($svg, $filter.$g); |
|
| 499 | 499 | $svg = svg_insert_shapes($svg, "</g>", false); |
| 500 | 500 | } |
| 501 | 501 | return $svg; |
@@ -546,7 +546,7 @@ discard block |
||
| 546 | 546 | */ |
| 547 | 547 | function svg_flip($img, $HorV) { |
| 548 | 548 | if ($svg = svg_force_viewBox_px($img) |
| 549 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 549 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 550 | 550 | |
| 551 | 551 | list($balise_svg, $atts) = $svg_infos; |
| 552 | 552 | $viewBox = explode(' ', $atts['viewBox']); |
@@ -554,14 +554,14 @@ discard block |
||
| 554 | 554 | if (!in_array($HorV, ['h', 'H'])) { |
| 555 | 555 | $transform = "scale(-1,1)"; |
| 556 | 556 | |
| 557 | - $x = intval($viewBox[0]) + intval($viewBox[2]/2); |
|
| 557 | + $x = intval($viewBox[0]) + intval($viewBox[2] / 2); |
|
| 558 | 558 | $mx = -$x; |
| 559 | 559 | $transform = "translate($x, 0) $transform translate($mx, 0)"; |
| 560 | 560 | } |
| 561 | 561 | else { |
| 562 | 562 | $transform = "scale(1,-1)"; |
| 563 | 563 | |
| 564 | - $y = intval($viewBox[1]) + intval($viewBox[3]/2); |
|
| 564 | + $y = intval($viewBox[1]) + intval($viewBox[3] / 2); |
|
| 565 | 565 | $my = -$y; |
| 566 | 566 | $transform = "translate(0, $y) $transform translate(0, $my)"; |
| 567 | 567 | } |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | */ |
| 584 | 584 | function svg_rotate($img, $angle, $center_x, $center_y) { |
| 585 | 585 | if ($svg = svg_force_viewBox_px($img) |
| 586 | - and $svg_infos = svg_lire_balise_svg($svg)){ |
|
| 586 | + and $svg_infos = svg_lire_balise_svg($svg)) { |
|
| 587 | 587 | |
| 588 | 588 | list($balise_svg, $atts) = $svg_infos; |
| 589 | 589 | $viewBox = explode(' ', $atts['viewBox']); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
@@ -30,16 +30,16 @@ discard block |
||
| 30 | 30 | * `on` ou `off` |
| 31 | 31 | */ |
| 32 | 32 | function logo_supprimer($objet, $id_objet, $etat) { |
| 33 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 34 | - $objet = objet_type($objet); |
|
| 35 | - $primary = id_table_objet($objet); |
|
| 36 | - include_spip('inc/chercher_logo'); |
|
| 37 | - |
|
| 38 | - // existe-t-il deja un logo ? |
|
| 39 | - $logo = $chercher_logo($id_objet, $primary, $etat); |
|
| 40 | - if ($logo) { |
|
| 41 | - spip_unlink($logo[0]); |
|
| 42 | - } |
|
| 33 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 34 | + $objet = objet_type($objet); |
|
| 35 | + $primary = id_table_objet($objet); |
|
| 36 | + include_spip('inc/chercher_logo'); |
|
| 37 | + |
|
| 38 | + // existe-t-il deja un logo ? |
|
| 39 | + $logo = $chercher_logo($id_objet, $primary, $etat); |
|
| 40 | + if ($logo) { |
|
| 41 | + spip_unlink($logo[0]); |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -56,143 +56,143 @@ discard block |
||
| 56 | 56 | * Erreur, sinon '' |
| 57 | 57 | */ |
| 58 | 58 | function logo_modifier($objet, $id_objet, $etat, $source) { |
| 59 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 60 | - $objet = objet_type($objet); |
|
| 61 | - $primary = id_table_objet($objet); |
|
| 62 | - include_spip('inc/chercher_logo'); |
|
| 63 | - $type = type_du_logo($primary); |
|
| 64 | - |
|
| 65 | - // nom du logo |
|
| 66 | - $nom = $type . $etat . $id_objet; |
|
| 67 | - |
|
| 68 | - // supprimer le logo eventueel existant |
|
| 69 | - logo_supprimer($objet, $id_objet, $etat); |
|
| 70 | - |
|
| 71 | - include_spip('inc/documents'); |
|
| 72 | - $erreur = ''; |
|
| 73 | - |
|
| 74 | - if (!$source) { |
|
| 75 | - spip_log('spip_image_ajouter : source inconnue'); |
|
| 76 | - $erreur = 'source inconnue'; |
|
| 77 | - |
|
| 78 | - return $erreur; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $file_tmp = _DIR_LOGOS . $nom . '.tmp'; |
|
| 82 | - |
|
| 83 | - $ok = false; |
|
| 84 | - // fichier dans upload/ |
|
| 85 | - if (is_string($source)) { |
|
| 86 | - if (file_exists($source)) { |
|
| 87 | - $ok = @copy($source, $file_tmp); |
|
| 88 | - } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 89 | - $ok = @copy($f, $file_tmp); |
|
| 90 | - } |
|
| 91 | - } elseif (!$erreur = check_upload_error($source['error'], '', true)) { |
|
| 92 | - // Intercepter une erreur a l'envoi |
|
| 93 | - // analyse le type de l'image (on ne fait pas confiance au nom de |
|
| 94 | - // fichier envoye par le browser : pour les Macs c'est plus sur) |
|
| 95 | - $ok = deplacer_fichier_upload($source['tmp_name'], $file_tmp); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ($erreur) { |
|
| 99 | - return $erreur; |
|
| 100 | - } |
|
| 101 | - if (!$ok or !file_exists($file_tmp)) { |
|
| 102 | - spip_log($erreur = "probleme de copie pour $file_tmp "); |
|
| 103 | - |
|
| 104 | - return $erreur; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($size = @spip_getimagesize($file_tmp) |
|
| 108 | - and $extension = logo_decoder_type_image($size[2]) |
|
| 109 | - and in_array($extension, $GLOBALS['formats_logos'])) { |
|
| 110 | - |
|
| 111 | - @rename($file_tmp, $file_tmp . ".$extension"); |
|
| 112 | - $file_tmp = $file_tmp . ".$extension"; |
|
| 113 | - |
|
| 114 | - // checker les metadata si plugin medias present |
|
| 115 | - // inclu une sanitization des SVG |
|
| 116 | - if ($metadata = charger_fonction($extension, 'metadata', true) |
|
| 117 | - or $metadata = charger_fonction('image', 'metadata', true )) { |
|
| 118 | - $infos = $metadata($file_tmp); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $poids = filesize($file_tmp); |
|
| 122 | - |
|
| 123 | - if (defined('_LOGO_MAX_WIDTH') or defined('_LOGO_MAX_HEIGHT')) { |
|
| 124 | - if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH) |
|
| 125 | - or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) |
|
| 126 | - ) { |
|
| 127 | - $max_width = (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH) ? _LOGO_MAX_WIDTH : '*'; |
|
| 128 | - $max_height = (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT) ? _LOGO_MAX_HEIGHT : '*'; |
|
| 129 | - |
|
| 130 | - // pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures |
|
| 131 | - // on met directement a la taille maxi a la volee |
|
| 132 | - if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') { |
|
| 133 | - include_spip('inc/filtres'); |
|
| 134 | - $img = filtrer('image_reduire', $file_tmp, $max_width, $max_height); |
|
| 135 | - $img = extraire_attribut($img, 'src'); |
|
| 136 | - $img = supprimer_timestamp($img); |
|
| 137 | - if (@file_exists($img) and $img !== $file_tmp) { |
|
| 138 | - spip_unlink($file_tmp); |
|
| 139 | - @rename($img, $file_tmp); |
|
| 140 | - $size = @spip_getimagesize($file_tmp); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - // verifier au cas ou image_reduire a echoue |
|
| 144 | - if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH) |
|
| 145 | - or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) |
|
| 146 | - ) { |
|
| 147 | - spip_unlink($file_tmp); |
|
| 148 | - $erreur = _T( |
|
| 149 | - 'info_logo_max_poids', |
|
| 150 | - array( |
|
| 151 | - 'maxi' => |
|
| 152 | - _T( |
|
| 153 | - 'info_largeur_vignette', |
|
| 154 | - array( |
|
| 155 | - 'largeur_vignette' => $max_width, |
|
| 156 | - 'hauteur_vignette' => $max_height |
|
| 157 | - ) |
|
| 158 | - ), |
|
| 159 | - 'actuel' => |
|
| 160 | - _T( |
|
| 161 | - 'info_largeur_vignette', |
|
| 162 | - array( |
|
| 163 | - 'largeur_vignette' => $size[0], |
|
| 164 | - 'hauteur_vignette' => $size[1] |
|
| 165 | - ) |
|
| 166 | - ) |
|
| 167 | - ) |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (!$erreur and defined('_LOGO_MAX_SIZE') and _LOGO_MAX_SIZE and $poids > _LOGO_MAX_SIZE * 1024) { |
|
| 174 | - spip_unlink($file_tmp); |
|
| 175 | - $erreur = _T( |
|
| 176 | - 'info_logo_max_poids', |
|
| 177 | - array( |
|
| 178 | - 'maxi' => taille_en_octets(_LOGO_MAX_SIZE * 1024), |
|
| 179 | - 'actuel' => taille_en_octets($poids) |
|
| 180 | - ) |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if (!$erreur) { |
|
| 185 | - @rename($file_tmp, _DIR_LOGOS . $nom . ".$extension"); |
|
| 186 | - } |
|
| 187 | - } else { |
|
| 188 | - spip_unlink($file_tmp); |
|
| 189 | - $erreur = _T( |
|
| 190 | - 'info_logo_format_interdit', |
|
| 191 | - array('formats' => join(', ', $GLOBALS['formats_logos'])) |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $erreur; |
|
| 59 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 60 | + $objet = objet_type($objet); |
|
| 61 | + $primary = id_table_objet($objet); |
|
| 62 | + include_spip('inc/chercher_logo'); |
|
| 63 | + $type = type_du_logo($primary); |
|
| 64 | + |
|
| 65 | + // nom du logo |
|
| 66 | + $nom = $type . $etat . $id_objet; |
|
| 67 | + |
|
| 68 | + // supprimer le logo eventueel existant |
|
| 69 | + logo_supprimer($objet, $id_objet, $etat); |
|
| 70 | + |
|
| 71 | + include_spip('inc/documents'); |
|
| 72 | + $erreur = ''; |
|
| 73 | + |
|
| 74 | + if (!$source) { |
|
| 75 | + spip_log('spip_image_ajouter : source inconnue'); |
|
| 76 | + $erreur = 'source inconnue'; |
|
| 77 | + |
|
| 78 | + return $erreur; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $file_tmp = _DIR_LOGOS . $nom . '.tmp'; |
|
| 82 | + |
|
| 83 | + $ok = false; |
|
| 84 | + // fichier dans upload/ |
|
| 85 | + if (is_string($source)) { |
|
| 86 | + if (file_exists($source)) { |
|
| 87 | + $ok = @copy($source, $file_tmp); |
|
| 88 | + } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 89 | + $ok = @copy($f, $file_tmp); |
|
| 90 | + } |
|
| 91 | + } elseif (!$erreur = check_upload_error($source['error'], '', true)) { |
|
| 92 | + // Intercepter une erreur a l'envoi |
|
| 93 | + // analyse le type de l'image (on ne fait pas confiance au nom de |
|
| 94 | + // fichier envoye par le browser : pour les Macs c'est plus sur) |
|
| 95 | + $ok = deplacer_fichier_upload($source['tmp_name'], $file_tmp); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ($erreur) { |
|
| 99 | + return $erreur; |
|
| 100 | + } |
|
| 101 | + if (!$ok or !file_exists($file_tmp)) { |
|
| 102 | + spip_log($erreur = "probleme de copie pour $file_tmp "); |
|
| 103 | + |
|
| 104 | + return $erreur; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($size = @spip_getimagesize($file_tmp) |
|
| 108 | + and $extension = logo_decoder_type_image($size[2]) |
|
| 109 | + and in_array($extension, $GLOBALS['formats_logos'])) { |
|
| 110 | + |
|
| 111 | + @rename($file_tmp, $file_tmp . ".$extension"); |
|
| 112 | + $file_tmp = $file_tmp . ".$extension"; |
|
| 113 | + |
|
| 114 | + // checker les metadata si plugin medias present |
|
| 115 | + // inclu une sanitization des SVG |
|
| 116 | + if ($metadata = charger_fonction($extension, 'metadata', true) |
|
| 117 | + or $metadata = charger_fonction('image', 'metadata', true )) { |
|
| 118 | + $infos = $metadata($file_tmp); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $poids = filesize($file_tmp); |
|
| 122 | + |
|
| 123 | + if (defined('_LOGO_MAX_WIDTH') or defined('_LOGO_MAX_HEIGHT')) { |
|
| 124 | + if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH) |
|
| 125 | + or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) |
|
| 126 | + ) { |
|
| 127 | + $max_width = (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH) ? _LOGO_MAX_WIDTH : '*'; |
|
| 128 | + $max_height = (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT) ? _LOGO_MAX_HEIGHT : '*'; |
|
| 129 | + |
|
| 130 | + // pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures |
|
| 131 | + // on met directement a la taille maxi a la volee |
|
| 132 | + if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') { |
|
| 133 | + include_spip('inc/filtres'); |
|
| 134 | + $img = filtrer('image_reduire', $file_tmp, $max_width, $max_height); |
|
| 135 | + $img = extraire_attribut($img, 'src'); |
|
| 136 | + $img = supprimer_timestamp($img); |
|
| 137 | + if (@file_exists($img) and $img !== $file_tmp) { |
|
| 138 | + spip_unlink($file_tmp); |
|
| 139 | + @rename($img, $file_tmp); |
|
| 140 | + $size = @spip_getimagesize($file_tmp); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + // verifier au cas ou image_reduire a echoue |
|
| 144 | + if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH) |
|
| 145 | + or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) |
|
| 146 | + ) { |
|
| 147 | + spip_unlink($file_tmp); |
|
| 148 | + $erreur = _T( |
|
| 149 | + 'info_logo_max_poids', |
|
| 150 | + array( |
|
| 151 | + 'maxi' => |
|
| 152 | + _T( |
|
| 153 | + 'info_largeur_vignette', |
|
| 154 | + array( |
|
| 155 | + 'largeur_vignette' => $max_width, |
|
| 156 | + 'hauteur_vignette' => $max_height |
|
| 157 | + ) |
|
| 158 | + ), |
|
| 159 | + 'actuel' => |
|
| 160 | + _T( |
|
| 161 | + 'info_largeur_vignette', |
|
| 162 | + array( |
|
| 163 | + 'largeur_vignette' => $size[0], |
|
| 164 | + 'hauteur_vignette' => $size[1] |
|
| 165 | + ) |
|
| 166 | + ) |
|
| 167 | + ) |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (!$erreur and defined('_LOGO_MAX_SIZE') and _LOGO_MAX_SIZE and $poids > _LOGO_MAX_SIZE * 1024) { |
|
| 174 | + spip_unlink($file_tmp); |
|
| 175 | + $erreur = _T( |
|
| 176 | + 'info_logo_max_poids', |
|
| 177 | + array( |
|
| 178 | + 'maxi' => taille_en_octets(_LOGO_MAX_SIZE * 1024), |
|
| 179 | + 'actuel' => taille_en_octets($poids) |
|
| 180 | + ) |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if (!$erreur) { |
|
| 185 | + @rename($file_tmp, _DIR_LOGOS . $nom . ".$extension"); |
|
| 186 | + } |
|
| 187 | + } else { |
|
| 188 | + spip_unlink($file_tmp); |
|
| 189 | + $erreur = _T( |
|
| 190 | + 'info_logo_format_interdit', |
|
| 191 | + array('formats' => join(', ', $GLOBALS['formats_logos'])) |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $erreur; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | |
@@ -207,27 +207,27 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | // https://code.spip.net/@decoder_type_image |
| 209 | 209 | function logo_decoder_type_image($type, $strict = false) { |
| 210 | - switch ($type) { |
|
| 211 | - case IMAGETYPE_GIF: |
|
| 212 | - return 'gif'; |
|
| 213 | - case IMAGETYPE_JPEG: |
|
| 214 | - return 'jpg'; |
|
| 215 | - case IMAGETYPE_PNG: |
|
| 216 | - return 'png'; |
|
| 217 | - case IMAGETYPE_SWF: |
|
| 218 | - return $strict ? '' : 'swf'; |
|
| 219 | - case IMAGETYPE_PSD: |
|
| 220 | - return 'psd'; |
|
| 221 | - case IMAGETYPE_BMP: |
|
| 222 | - return 'bmp'; |
|
| 223 | - case IMAGETYPE_TIFF_II: |
|
| 224 | - case IMAGETYPE_TIFF_MM: |
|
| 225 | - return 'tif'; |
|
| 226 | - case IMAGETYPE_WEBP: |
|
| 227 | - return 'webp'; |
|
| 228 | - case IMAGETYPE_SVG: |
|
| 229 | - return $strict ? '' : 'svg'; |
|
| 230 | - default: |
|
| 231 | - return ''; |
|
| 232 | - } |
|
| 210 | + switch ($type) { |
|
| 211 | + case IMAGETYPE_GIF: |
|
| 212 | + return 'gif'; |
|
| 213 | + case IMAGETYPE_JPEG: |
|
| 214 | + return 'jpg'; |
|
| 215 | + case IMAGETYPE_PNG: |
|
| 216 | + return 'png'; |
|
| 217 | + case IMAGETYPE_SWF: |
|
| 218 | + return $strict ? '' : 'swf'; |
|
| 219 | + case IMAGETYPE_PSD: |
|
| 220 | + return 'psd'; |
|
| 221 | + case IMAGETYPE_BMP: |
|
| 222 | + return 'bmp'; |
|
| 223 | + case IMAGETYPE_TIFF_II: |
|
| 224 | + case IMAGETYPE_TIFF_MM: |
|
| 225 | + return 'tif'; |
|
| 226 | + case IMAGETYPE_WEBP: |
|
| 227 | + return 'webp'; |
|
| 228 | + case IMAGETYPE_SVG: |
|
| 229 | + return $strict ? '' : 'svg'; |
|
| 230 | + default: |
|
| 231 | + return ''; |
|
| 232 | + } |
|
| 233 | 233 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | $type = type_du_logo($primary); |
| 64 | 64 | |
| 65 | 65 | // nom du logo |
| 66 | - $nom = $type . $etat . $id_objet; |
|
| 66 | + $nom = $type.$etat.$id_objet; |
|
| 67 | 67 | |
| 68 | 68 | // supprimer le logo eventueel existant |
| 69 | 69 | logo_supprimer($objet, $id_objet, $etat); |
@@ -78,14 +78,14 @@ discard block |
||
| 78 | 78 | return $erreur; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - $file_tmp = _DIR_LOGOS . $nom . '.tmp'; |
|
| 81 | + $file_tmp = _DIR_LOGOS.$nom.'.tmp'; |
|
| 82 | 82 | |
| 83 | 83 | $ok = false; |
| 84 | 84 | // fichier dans upload/ |
| 85 | 85 | if (is_string($source)) { |
| 86 | 86 | if (file_exists($source)) { |
| 87 | 87 | $ok = @copy($source, $file_tmp); |
| 88 | - } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 88 | + } elseif (file_exists($f = determine_upload().$source)) { |
|
| 89 | 89 | $ok = @copy($f, $file_tmp); |
| 90 | 90 | } |
| 91 | 91 | } elseif (!$erreur = check_upload_error($source['error'], '', true)) { |
@@ -108,13 +108,13 @@ discard block |
||
| 108 | 108 | and $extension = logo_decoder_type_image($size[2]) |
| 109 | 109 | and in_array($extension, $GLOBALS['formats_logos'])) { |
| 110 | 110 | |
| 111 | - @rename($file_tmp, $file_tmp . ".$extension"); |
|
| 112 | - $file_tmp = $file_tmp . ".$extension"; |
|
| 111 | + @rename($file_tmp, $file_tmp.".$extension"); |
|
| 112 | + $file_tmp = $file_tmp.".$extension"; |
|
| 113 | 113 | |
| 114 | 114 | // checker les metadata si plugin medias present |
| 115 | 115 | // inclu une sanitization des SVG |
| 116 | 116 | if ($metadata = charger_fonction($extension, 'metadata', true) |
| 117 | - or $metadata = charger_fonction('image', 'metadata', true )) { |
|
| 117 | + or $metadata = charger_fonction('image', 'metadata', true)) { |
|
| 118 | 118 | $infos = $metadata($file_tmp); |
| 119 | 119 | } |
| 120 | 120 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | if (!$erreur) { |
| 185 | - @rename($file_tmp, _DIR_LOGOS . $nom . ".$extension"); |
|
| 185 | + @rename($file_tmp, _DIR_LOGOS.$nom.".$extension"); |
|
| 186 | 186 | } |
| 187 | 187 | } else { |
| 188 | 188 | spip_unlink($file_tmp); |