-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
149 lines (130 loc) · 4.48 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
MODIFY FOLLOWING LINES TO CONFIGURE SOURCES:
*/
function data_weather_provider($source){
if($source==1){
$data=array('urlimage' => 'https://dsx.weather.com/util/image/map/DCT_SPECIAL11_1280x720.jpg',
'pathimages' => '/home/youracccount/public_html/weather/images1/',
'outputgif' => 'animation1.gif',
'width' => '1280',
'height' => '720');
} else if($source==2){
$data=array('urlimage' => 'http://rammb.cira.colostate.edu/ramsdis/online/images/latest/tropical/tropical_ge_4km_ir4_floater_2.gif',
'pathimages' => '/home/youracccount/public_html/weather/images2/',
'outputgif' =>'animation2.gif',
'width' => '640',
'height' => '480');
} else if($source==3){
$data=array('urlimage' => 'https://weather.msfc.nasa.gov/cgi-bin/get-abi?satellite=GOESEastfullDiskband13&palette=ir2.pal&lat=16&lon=-62&type=Image&width=640&height=480&zoom=1&quality=50&map=standard',
'pathimages' => '/home/youracccount/public_html/weather/images3/',
'outputgif' =>'animation3.gif',
'width' => '640',
'height' => '480');
}
return $data;
}
/********************* DO NOT MODIFY UNDER THIS LINE ************************/
function download_curl($url,$output_filename){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.12011-10-16 20:23:00");
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$fp = fopen($output_filename, 'wb');
fwrite($fp, $result);
fclose($fp);
}
function count_files_in_dir($dir){
$total=0;
$dir=$dir.'*';
$dir=glob($dir);
foreach($dir as $file) {
if(is_file($file)) {
$total++;
}
}
return $total;
}
function remove_old_files($dir){
$totalfiles=count_files_in_dir($dir);
if ($totalfiles > 9 ) {
$files = glob( $dir.'*.*' );
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
unlink($files[0]); // remove oldest file
}
}
function create_animation($data){
$i=0;
$delay=array(30,30,30,30,30,30,30,30,30,300);
$dir=$data[pathimages].'*';
$dir=glob($dir);
$animation = new Imagick();
$animation->setFormat("GIF");
foreach($dir as $file) {
if(is_file($file)) {
$frame = new Imagick($file);
$frame->thumbnailImage($data[width], $data[height]);
$animation->addImage($frame);
$animation->setImageDelay($delay[$i]);
$animation->nextImage();
$i++;
}
}
$animation->writeImages('./'.$data[outputgif], true);
}
function convert_gif_jpg_and_move($data){
download_curl($data[urlimage],'temporary.gif');
$i = new IMagick('temporary.gif');
$i->setImageBackgroundColor(new ImagickPixel('white'));
$i = $i->flattenImages();
$i->setImageFormat('jpg');
$i->writeImage('temporary.jpg');
rename('temporary.jpg',$data[pathimages].date("YmdHis").'.jpg');
unlink('temporary.gif');
unlink('temporary.jpg');
}
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
/******************************* MAIN PROGRAM **********************************/
$op=explode('TIPO',$_GET['op']);
if(($op[0]=='cronjob')&&(isset($op[1]))){
$data=data_weather_provider($op[1]);
remove_old_files($data[pathimages]);
if($op[1]==2){ // origin is a gif image, case 2:
download_curl($data[urlimage],'temporary.gif');
convert_gif_jpg_and_move($data);
}else if($op[1]==3){ // origin is a jpg displayed by cgi script, case 3:
$html=file_get_contents($data[urlimage]);
$data[urlimage]="https://weather.msfc.nasa.gov/goes/abi/dynamic/".get_string_between($html, '<IMG SRC="/goes/abi/dynamic/', '" WIDTH="640" HEIGHT="480">');
download_curl($data[urlimage],$data[pathimages].date("YmdHis").'.jpg');
}else{
download_curl($data[urlimage],$data[pathimages].date("YmdHis").'.jpg');
}
create_animation($data);
echo 'New animation created';
}
else if(isset($_GET["tipo"])){
$data=data_weather_provider($_GET["tipo"]);
header("Content-type: image/gif");
echo file_get_contents($data[outputgif]);
}
?>