|
1 | 1 | <?php
|
2 |
| - |
3 | 2 | header('Content-Type: application/json');
|
4 |
| - header('Access-Control-Allow-Origin: *'); |
5 |
| - |
| 3 | + header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); |
| 4 | + header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); |
| 5 | + header('Access-Control-Max-Age: 1000'); |
| 6 | + header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); |
| 7 | + |
6 | 8 | $inputimages = ($_REQUEST["svgsources"]);
|
7 | 9 | $dumpcache = ($_REQUEST["dumpcache"]);
|
8 |
| - $urlprefix = ($_REQUEST["secure"] == 'false') ? 'http://' : 'https://'; |
9 |
| - $result = array( |
10 |
| - 'error' => '1', |
11 |
| - 'errormsg' => 'No images given' |
12 |
| - ); |
13 |
| - |
14 |
| - if (isset($inputimages) && is_array($inputimages)) { |
15 |
| - |
| 10 | + $urlprefix = ($_REQUEST["secure"] == 'false') ? 'http://' : 'https://'; |
| 11 | + $result = array('error' => '1', 'errormsg' => 'No images given'); |
| 12 | + |
| 13 | + if(isset($inputimages) && is_array($inputimages)){ |
| 14 | + |
16 | 15 | // Create empty array with images to return
|
17 | 16 | $imageurls = array();
|
18 |
| - |
| 17 | + |
19 | 18 | // Loop through given image urls
|
20 |
| - foreach ($inputimages as $input) { |
21 |
| - |
| 19 | + foreach($inputimages as $input){ |
| 20 | + |
22 | 21 | // Get the file name
|
23 |
| - $filename = explode('/', $input); |
| 22 | + $filename = explode('/',$input); |
24 | 23 | $filename = array_pop($filename);
|
25 |
| - $filename = str_replace('.svg', '', $filename); |
26 |
| - |
| 24 | + $filename = str_replace('.svg' ,'' , $filename); |
| 25 | + |
27 | 26 | // Check the domain and create a folder if needed
|
28 |
| - $url = parse_url($input); |
| 27 | + $url = parse_url($input); |
29 | 28 | $domain = $url['host'];
|
30 |
| - |
31 |
| - if (!file_exists("images/results/" . $domain)) { |
32 |
| - mkdir("images/results/" . $domain, 0777); |
| 29 | + if(!file_exists("images/results/" . $domain)){ |
| 30 | + mkdir("images/results/" . $domain, 0777); |
33 | 31 | }
|
34 |
| - |
35 | 32 | $imagefolder = "images/results/" . $domain;
|
36 |
| - |
| 33 | + |
37 | 34 | // Check if we need to dump the cache
|
38 | 35 | if($dumpcache == "true"){
|
39 | 36 | if(file_exists($imagefolder . '/'.$filename.'.png')){
|
|
42 | 39 | }
|
43 | 40 |
|
44 | 41 | // Check if the file already exists
|
45 |
| - if (!file_exists($imagefolder . '/' . $filename . '.png')) { |
| 42 | + if(!file_exists($imagefolder . '/'.$filename.'.png')){ |
46 | 43 | // Get the image
|
47 | 44 | $inputimage = file_get_contents($input);
|
48 |
| - |
| 45 | + |
49 | 46 | // Convert the given image to PNG and store it in the custom directory
|
50 | 47 | $image = new Imagick();
|
51 | 48 | $image->setBackgroundColor(new ImagickPixel('transparent'));
|
52 | 49 | $image->readImageBlob(($inputimage));
|
53 | 50 | $image->setImageFormat("png32");
|
54 | 51 | $image->setImageCompressionQuality(100);
|
55 |
| - $image->writeImage($imagefolder . '/' . $filename . '.png'); |
| 52 | + $image->writeImage($imagefolder . '/'.$filename.'.png'); |
56 | 53 | }
|
57 |
| - |
58 |
| - $imageurls[] = array( |
59 |
| - 'url' => $urlprefix . "bitlabs.nl/svgmagic/" . $imagefolder . '/' . $filename . '.png' |
60 |
| - ); |
| 54 | + |
| 55 | + $imageurls[] = array('url' => $urlprefix."bitlabs.nl/svgmagic/" . $imagefolder . '/'.$filename.'.png'); |
61 | 56 | }
|
62 |
| - |
63 |
| - $result = array( |
64 |
| - 'results' => $imageurls, |
65 |
| - 'error' => '0' |
66 |
| - ); |
| 57 | + |
| 58 | + $result = array('results' => $imageurls, 'error' => '0'); |
67 | 59 | }
|
68 |
| - echo $_GET['callback'] . '(' . json_encode($result) . ')'; |
| 60 | + |
| 61 | + // Return the images, since version 2.3 we use cross-domain requests via post, so the callback has been removed |
| 62 | + if(isset($_GET['callback'])){ |
| 63 | + echo $_GET['callback'] . '('. json_encode($result) . ')'; |
| 64 | + } |
| 65 | + else{ |
| 66 | + echo json_encode($result); |
| 67 | + } |
| 68 | + |
69 | 69 | ?>
|
0 commit comments