Merge pull request #456 from Struart88/patch-2
[FlightAirMap.git] / getImages.php
blob13b2e1eedc0a76ba85f4da0c1614763b82670858
1 <?php
3 function hexToRGB($hex) {
4 $hex = str_replace("#", "", $hex);
5 $color = array();
6 if (strlen($hex) == 3) {
7 $color['r'] = hexdec(substr($hex, 0, 1) . substr($hex,0,1));
8 $color['g'] = hexdec(substr($hex, 1, 1) . substr($hex,1,1));
9 $color['b'] = hexdec(substr($hex, 2, 1) . substr($hex,2,1));
10 } else if (strlen($hex) == 6) {
11 $color['r'] = hexdec(substr($hex, 0, 2));
12 $color['g'] = hexdec(substr($hex, 2, 2));
13 $color['b'] = hexdec(substr($hex, 4, 2));
15 return $color;
19 if (!isset($_GET['color']) || $_GET['color'] == '' || !preg_match('/^([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b/',$_GET['color'])) {
20 exit(0);
22 $color = $_GET['color'];
23 if (!isset($_GET['filename']) || !preg_match('/^[a-z0-9-_]+\.png$/', strtolower($_GET['filename']))) {
24 echo "Incorrect filename";
25 exit(0);
27 $filename = $_GET['filename'];
28 if (file_exists(dirname(__FILE__).'/cache/'.$color.'-'.$filename) && is_readable(dirname(__FILE__).'/cache/'.$color.'-'.$filename)) {
29 header('Content-type: image/png');
30 readfile(dirname(__FILE__).'/cache/'.$color.'-'.$filename);
31 exit(0);
33 if (isset($_GET['tracker'])) {
34 $original = dirname(__FILE__).'/images/vehicules/color/'.$filename;
35 if (!file_exists($original)) {
36 $original = dirname(__FILE__).'/images/vehicules/'.$filename;
38 } elseif (isset($_GET['marine'])) {
39 //$original = dirname(__FILE__).DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'vehicules'.DIRECTORY_SEPARATOR.$filename;
40 //$original = dirname(__FILE__).'/images/vehicules/color/'.$filename;
41 $original = dirname(__FILE__).'/images/marine/'.$filename;
42 } elseif (isset($_GET['satellite'])) {
43 //$original = dirname(__FILE__).DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'vehicules'.DIRECTORY_SEPARATOR.$filename;
44 $original = dirname(__FILE__).'/images/satellites/'.$filename;
45 } else {
46 $original = dirname(__FILE__).'/images/aircrafts/new/'.$filename;
48 if (!file_exists($original)) {
49 echo "File not found";
52 if (extension_loaded('gd') && function_exists('gd_info')) {
53 $image = imagecreatefrompng($original);
54 $index = imagecolorexact($image,26,49,81);
55 if ($index < 0) {
56 $index = imagecolorexact($image,25,49,79);
58 if ($index < 0) {
59 $index = imagecolorexact($image,0,0,0);
61 $c = hexToRGB($color);
62 imagecolorset($image,$index,$c['r'],$c['g'],$c['b']);
64 $ig = imagecolorat($image, 0, 0);
65 imagecolortransparent($image, $ig);
68 header('Content-type: image/png');
69 if (isset($_GET['resize']) && function_exists('imagecopyresampled')) {
70 $resize = filter_input(INPUT_GET,'resize',FILTER_SANITIZE_NUMBER_INT);
71 $newimg = imagecreatetruecolor($resize,$resize);
72 imagealphablending($newimg, false);
73 imagesavealpha($newimg, true);
74 imagecopyresampled($newimg,$image,0,0,0,0,15,15,imagesx($image),imagesy($image));
75 if (isset($_GET['heading'])) {
76 $heading = filter_input(INPUT_GET,'heading',FILTER_SANITIZE_NUMBER_INT);
77 $rotation = imagerotate($newimg,$heading,imageColorAllocateAlpha($newimg,0,0,0,127));
78 imagealphablending($rotation, false);
79 imagesavealpha($rotation, true);
80 imagepng($rotation);
81 imagedestroy($newimg);
82 imagedestroy($image);
83 imagedestroy($rotation);
85 } else {
86 imagepng($newimg);
87 imagedestroy($newimg);
88 imagedestroy($image);
90 } else {
91 imagealphablending($image, false);
92 imagesavealpha($image, true);
93 imagepng($image);
94 imagepng($image);
95 if (is_writable(dirname(__FILE__).'/cache')) {
96 imagepng($image,dirname(__FILE__).'/cache/'.$color.'-'.$filename);
98 imagedestroy($image);
100 } else {
101 header('Content-type: image/png');
102 if (isset($_GET['tracker'])) {
103 readfile(dirname(__FILE__).'/images/vehicules/'.$filename);
104 } elseif (isset($_GET['marine'])) {
105 readfile(dirname(__FILE__).'/images/vehicules/'.$filename);
106 } elseif (isset($_GET['satellite'])) {
107 readfile(dirname(__FILE__).'/images/satellites/'.$filename);
108 } else {
109 if ($color == 'FF0000') readfile(dirname(__FILE__).'/images/aircrafts/selected/'.$filename);
110 else readfile(dirname(__FILE__).'/images/aircrafts/'.$filename);