<?php
## Name: Solar Desktop 2.0
## Last Updated: 15 Apr, 2009
## Author: Jason Priem
## Website: jasonpriem.com

/* Description:
Scrapes solar data and images from NASA and the NOAA and combines them into a display you can set
as your desktop.  You can customize the size by putting "width" and "height" pixel values in the URL, 
like so:
http://jasonpriem.com/solar-desktop?width=1024&height=768
*/


// @param    img        the image to be resized; has to be square
// @param    new_w    the width of the new image
// @param    new_h     the height of the new image
// @return    an image resource that has been shrunk and/or padded to be the new dimensions.
function sized_img($orig_img$new_w$new_h) {
    
// clean up inputs
    
if (!$new_w 0$new_w 1024;
    if (!
$new_h 0$new_h 768;
    if (
$new_w 4000$new_w 4000;
    if (
$new_h 2000$new_h 2000;
    
$new_img imagecreatetruecolor($new_w$new_h);
    
$orig_side_len imagesx($orig_img);
    
    if (
$new_w >= $new_h) {  // the new image will be landscape-layout
        // if the orig is too short, the resampled height will be the desktop height; otherwise, it stays the same.
        
$resampled_h = ($new_h $orig_side_len) ? $new_h $orig_side_len;
        
$new_x_pos $new_w $resampled_h// resampled width is the same as resampled height
        
        
imagecopyresampled($new_img$orig_img$new_x_pos000$resampled_h$resampled_h$orig_side_len$orig_side_len);
        
    }
    else {
// the new image will be portrait layout; not too likely, but just in case.
        
$resampled_w = ($new_w $orig_side_len) ? $new_w $orig_side_len;
        
imagecopyresampled($new_img$orig_img0000$resampled_w$resampled_w$orig_side_len$orig_side_len);
        }
        
    return 
$new_img;    
}
    
    

//////////////// initial working image size
$total_width 1130;
$total_height 1130;

//////////////// load the images //////////////////////////////

//load the desktop images
$desktop imagecreatetruecolor($total_width,$total_height);
$black imagecolorallocate($desktop000);

// get images from remote servers
$url[] = 'http://sohowww.nascom.nasa.gov/data/realtime/eit_171/1024/latest.jpg'//main image; blue sun
$url[] = 'http://sohowww.nascom.nasa.gov/data/realtime/eit_304/512/latest.jpg'//orange sun
$url[] = 'http://sohowww.nascom.nasa.gov/data/realtime/eit_195/512/latest.jpg'//green sun
$url[] = 'http://sohowww.nascom.nasa.gov/data/realtime/eit_284/512/latest.jpg'//yeller sun
$url[] = 'http://www.swpc.noaa.gov/rt_plots/XrayBL.gif'//xray graph
$url[] = 'http://www.swpc.noaa.gov/rt_plots/SatEnvBL.gif'//GOES satellite environment graph

// create each image for gd to work with.
foreach ($url as $k => $v) { 
    
$img[$k] = imagecreatefromstring(file_get_contents($v));
}
///////////// format images for pasting ///////////////////////

//build the top right sub-image
$xray_graph_w 307;
$xray_graph_h 315;
$xray_graph imagecreatetruecolor($xray_graph_w$xray_graph_h);
imagecopyresampled($xray_graph$img[4], 0153033$xray_graph_w$xray_graph_h457450);
$white imagecolorallocate($xray_graph255255255);
imageRectangle($xray_graph00$xray_graph_w 2$xray_graph_h -2$white);

//build the bottom-right sub-image
$multigraph_w 400;
$multigraph_h 400;
$multigraph imagecreatetruecolor($multigraph_w$multigraph_h);
imagecopyresampled($multigraph$img[5], 55010$multigraph_w$multigraph_h550385);
$white imagecolorallocate($multigraph255255255);
imageRectangle($multigraph00$multigraph_w 2$multigraph_h 2$white);


//// Paste the images/////////////////////////////////////////

// get the old image size:
$new_width 800;
$new_height 768//the timestamp on the bottom of the image is getting chopped off
//paste the resized image.  
## It's currently 1700px from the left
imagecopymerge($desktop$img[0], 0, -50001024984100);  

//move the main-image timestamp
imagecopyresampled($desktop$img[0], 90065009902003030040);

//Paste the smaller pics:
imagecopyresampled($desktop$img[1], 5090000192192512512);
imagecopyresampled($desktop$img[2], 24290000192192512512);
imagecopyresampled($desktop$img[3], 43490000192192512512);

imagecopymerge($desktop$xray_graph7503000$xray_graph_w$xray_graph_h60);
imagecopymerge($desktop$multigraph65069300$multigraph_w$multigraph_h65);



////////// draw the text //////////////////////////////////////
// Create some colors
$white imagecolorallocate($desktop255255255);
$grey imagecolorallocate($desktop100100100);
$black imagecolorallocate($desktop000);

// Get the text to draw
$raw_text file_get_contents('http://www.swpc.noaa.gov/forecast.html');
$regex '/<pre[^>]*>(.*?)<\/pre>/s';
preg_match($regex$raw_text$forecast);


// Add the text
$font './VeraMono.ttf';
imagettftext($desktop905050$white$font$forecast[1]);


// size the image
$sized_desktop sized_img($desktop$_GET['width'], $_GET['height']);


//send the image to the browser
header ('content-type: image/jpg');
imagejpeg($sized_desktopnull90);

//clean up
imagedestroy($sized_desktop);
imagedestroy($xray_graph);
imagedestroy($multigraph);
imagedestroy($desktop);
foreach(
$img as $k) {
    
imagedestroy($img[$k]);
}

?>