Vacation seating
Online version: http://ernie.art.yale.edu/~emilylarned/weathersliced.html
Sample images:
Source code:
<?php
//----PHP should tell the browser it is getting a png image, not html as is the
default
header("Content-type: image/png");
//--constants
$canvasWidth =
87;
$canvasHeight =
87;
// Based on the request to this script, figure out what location to get the
weather
for
//$location = $_REQUEST["location"];
$location =
"06511";
//-------------------------------
// GET THE WEATHER INTO VARIABLES
// First just load the file as straight data. We need to alter it before we can
begin.
$xmlContents =
file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=$location");
$xmlContents =
str_replace("yweather:",
"",
$xmlContents);
$xmlContents =
str_replace("geo:",
"",
$xmlContents);
// Now load the string into SimpleXML
$xml = simplexml_load_string
($xmlContents);
// ----- FOR CONVENIENCE, LOAD DATA WE CARE ABOUT INTO VARIABLES
// <channel> node contains some of the info
$channel =
$xml->
channel;
// Location name: only the attributes are meaningful
$location =
$channel->
location;
$city =
$location["city"];
// e.g. "New Haven"
$region =
$location["region"];
// e.g. "CT"
$country =
$location["country"];
// e.g. "US"
// Units info: only the attributes are meaningful
$units =
$channel->
units;
$tempUnits =
$units["temperature"];
// e.g. "F"
$distanceUnits =
$units["distance"];
// e.g. "mi"
$pressureUnits =
$units["pressure"];
// e.g. "in"
$speedUnits =
$units["speed"];
// e.g. "mph"
// Wind data: only the attributes are meaningful
$wind =
$channel->
wind;
$windChill =
$wind["chill"];
// e.g. "57"
$windDir =
$wind["direction"];
// e.g. "350"
$windSpeed =
$wind["speed"];
// e.g. "7"
// Atmosphere data: only the attributes are meaningful
$atmosphere =
$channel->
atmosphere;
$humidity =
$atmosphere["humidity"];
// e.g. "93";
$visibility =
$atmosphere["visibility"];
// e.g. "1609";
$pressure =
$atmosphere["pressure"];
// e.g. "30.12";
$rising =
$atmosphere["rising"];
// e.g. "0";
// Astronomy data: only the attributes are meaningful
$astronomy =
$channel->
astronomy;
$sunrise =
$astronomy["sunrise"];
// e.g. "7:02 am";
$sunset =
$astronomy["sunset"];
// e.g. "4:51 pm";
// <item> node is within the <channel> node, contains additional info
$item =
$channel->
item;
$lat =
$item->
lat;
// Latitude, e.g. "37.39"
$long =
$item->
long;
// Longitude, e.g. "122.03"
// Condition data: only the attributes are meaningful
$condition =
$item->
condition;
$conditionText =
$condition["text"];
// e.g. "Mostly Cloudy" (corresponds to
code below
)
$conditionCode =
$condition["code"];
// e.g. "26" (47 possibilities)
$temp =
$condition["temp"];
// e.g. 57
$date =
$condition["date"];
// e.g. Tue, 29 Nov 2005 3:56 pm PST
// THE WEATHER IS NOW IN VARIABLES
//---------------------------------
//$conditionCode=46;
//---create a new canvas
$canvas = imagecreatetruecolor
($canvasWidth,
$canvasHeight);
//-- define a few colors with rgb values
$white = imagecolorallocate
($canvas,
255,
255,
255);
$black = imagecolorallocate
($canvas,
0,
0,
0);
$blue = imagecolorallocate
($canvas,
0,
110,
185);
$lightblue = imagecolorallocate
($canvas,
0,
172,
233);
$darkblue = imagecolorallocate
($canvas,
0,
73,
160);
$red = imagecolorallocate
($canvas,
255,
0,
0);
$orange = imagecolorallocate
($canvas,
242,
76,
24);
$gray = imagecolorallocate
($canvas,
127,
127,
127);
$green = imagecolorallocate
($canvas,
41,
153,
71);
$yellow = imagecolorallocate
($canvas,
255,
204,
1);
//--fill the background
//--define white as a transparent color
imagecolortransparent
($canvas,
$white);
imagefill
($canvas,
0,
0,
$white);
//--sprinke few pixles if it is raining lighty
if ($conditionCode==
"11" ||
$conditionCode==
"12" ||
$conditionCode==
"40" ||
$conditionCode==
"45"
||
$conditionCode==
"9") {
for ($count =
1;
$count <=
150;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$blue);
}
}
//--sprinke more pixles if it is raining even harder
if ($conditionCode==
"39" ||
$conditionCode==
"38" ||
$conditionCode==
"4" ||
$conditionCode==
"47"
||
$conditionCode==
"37") {
for ($count =
1;
$count <=
1000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$blue);
}
}
//--sprinke more even darker pixles if it is raining severely
if ($conditionCode==
"17" ||
$conditionCode==
"18" ||
$conditionCode==
"3") {
for ($count =
1;
$count <=
3000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$blue);
}
}
//+ yellow shape for thunder/lightening
// set up array of points for polygon
if ($conditionCode==
"3" ||
$conditionCode==
"4" ||
$conditionCode==
"37" ||
$conditionCode==
"38"
||
$conditionCode==
"39" ||
$conditionCode==
"45" ||
$conditionCode==
"47"){
$values =
array(
5,
60,
// Point 1 (x, y)
10,
60,
// Point 2 (x, y)
30,
40,
// Point 3 (x, y)
10,
10,
// Point 4 (x, y)
20,
10,
// Point 5 (x, y)
30,
40 // Point 6 (x, y)
);
imagefilledpolygon
($canvas,
$values,
6,
$yellow);
}
//--sprinke green pixles if it is a tropical storm
if ($conditionCode==
"1") {
for ($count =
1;
$count <=
2000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$green);
}
}
//blue and light blue pixles if snow-rain mix
if ($conditionCode==
"5" ||
$conditionCode==
"6" ||
$conditionCode==
"7" ||
$conditionCode==
"8" ||
$conditionCode==
"10" ||
$conditionCode==
"35"){
for ($count =
1;
$count <=
20000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$blue);
}
}
if ($conditionCode==
"5" ||
$conditionCode==
"6" ||
$conditionCode==
"7" ||
$conditionCode==
"8" ||
$conditionCode==
"10" ||
$conditionCode==
"35"){
for ($count =
1;
$count <=
20000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$lightblue);
}
}
//lightblue if snowing
if ($conditionCode==
"13" ||
$conditionCode==
"14" ||
$conditionCode==
"15" ||
$conditionCode==
"42" ||
$conditionCode==
"46"){
for ($count =
1;
$count <=
25000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$lightblue);
}
}
//more white if severe snow
if ($conditionCode==
"16" ||
$conditionCode==
"41" ||
$conditionCode==
"43"){
for ($count =
1;
$count <=
9000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$lightblue);
}
}
//dark blue if cold
if ($conditionCode==
"25"){
for ($count =
1;
$count <=
65000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$blue);
}
}
//black if night
if ($conditionCode ==
"27" ||
$conditionCode==
"29" ||
$conditionCode==
"31" ||
$conditionCode==
"33"){
for ($count =
1;
$count <=
65000;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$black);
}
}
//red pixels if dusty
if ($conditionCode==
"19") {
for ($count =
1;
$count <=
800;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$red);
}
}
//black pixels if smokey
if ($conditionCode==
"22") {
for ($count =
1;
$count <=
800;
$count++
) {
//pick a random x y coordinate
$x=
rand (0,
$canvasWidth -
1);
$y=
rand (0,
$canvasHeight -
1);
//draw the pixel
imagesetpixel
($canvas,
$x,
$y,
$black);
}
}
//grey shape if hurriance
// set up array of points for polygon
if ($conditionCode==
"2"){
$values =
array(
10,
20,
// Point 1 (x, y)
10,
60,
// Point 2 (x, y)
40,
80,
// Point 3 (x, y)
60,
10,
// Point 4 (x, y)
20,
10,
// Point 5 (x, y)
80,
40 // Point 6 (x, y)
);
imagefilledpolygon
($canvas,
$values,
6,
$gray);
}
//black shapee if tornado
// set up array of points for polygon
if ($conditionCode==
"0"){
$values =
array(
10,
20,
// Point 1 (x, y)
10,
60,
// Point 2 (x, y)
40,
80,
// Point 3 (x, y)
60,
10,
// Point 4 (x, y)
20,
10,
// Point 5 (x, y)
80,
40 // Point 6 (x, y)
);
imagefilledpolygon
($canvas,
$values,
6,
$black);
}
// blue shapee if windy
// set up array of points for polygon
if ($conditionCode==
"23" ||
$conditionCode==
"24" ||
$conditionCode==
"15" ||
$conditionCode==
"1"){
$values =
array(
5,
60,
// Point 1 (x, y)
10,
60,
// Point 2 (x, y)
30,
40,
// Point 3 (x, y)
10,
10,
// Point 4 (x, y)
20,
10,
// Point 5 (x, y)
80,
40 // Point 6 (x, y)
);
imagefilledpolygon
($canvas,
$values,
6,
$blue);
}
//--draw a red circle for the sun if sunny / fair / partly daytime
if ($conditionCode==
"32" ||
$conditionCode==
"34"||
$conditionCode==
"30"){
$x =
rand(0,
$canvasWidth -
20);
$y =
rand(0,
$canvasHeight -
20);
$radius =
50;
imagefilledellipse
($canvas,
$x,
$y,
$radius,
$radius,
$red);
}
//--draw a lightblue circle for the moon if clear / partly at night
if ($conditionCode==
"31" ||
$conditionCode==
"33" ||
$conditionCode==
"29"){
$x =
rand(0,
$canvasWidth -
20);
$y =
rand(0,
$canvasHeight -
20);
$radius =
40;
imagefilledellipse
($canvas,
$x,
$y,
$radius,
$radius,
$lightblue);
}
//--draw a huge red circle if hot
if ($conditionCode==
"36") {
$x =
rand(0,
$canvasWidth -
20);
$y =
rand(0,
$canvasHeight -
20);
$radius =
80;
imagefilledellipse
($canvas,
$x,
$y,
$radius,
$radius,
$red);
}
//--draw a gray circle if it is cloudy
if ($conditionCode==
"26" ||
$conditionCode==
"27" ||
$conditionCode==
"28"
||
$conditionCode==
"29" ||
$conditionCode==
"30" ||
$conditionCode==
"44"){
$x =
rand(0,
$canvasWidth -
20);
$y =
rand(0,
$canvasHeight -
20);
$radius =
70;
imagefilledellipse
($canvas,
$x,
$y,
$radius,
$radius,
$gray);
}
//--duisplay temperature as a truetype font
$x =
rand(15,
$canvasWidth /
2);
$y =
rand(50,
$canvasHeight);
imagettftext
($canvas,
18,
35,
$x,
$y,
$orange,
"5d-whitney.ttf",
"$temp");
//output the image to the browser as a png
imagepng
($canvas);
?>