Astronomy Picture of the Day Widget for Wordpress

A friend of mine wanted to start his own blog, and as a major astronomy buff, he picked out the Red Planet theme. I thought a nice touch would be to add the Astronomy Picture of the Day widget to his sidebar.

However, nothing is ever as easy as it seems. The plugin failed, because url file access was disabled in the php.ini configuration on the webhost. I attempted to change this using the ini_set() function, but was unsuccessful.

Not willing to give up, I rewrote a large portion of the widget that retrieves the APOD content to use cURL instead. In a nutshell, the widget uses cURL to read the APOD page for the current day. There are two images in the page - a small version and a large version. The source of both images is parsed from the captured html, and cURL is used again to fetch the image data for the small image.

Because of the security settings, I couldn't do a getimagesize() call against the image URL directly, so I had to write the image data to a temp file first, and then I was able to get the dimensions of the image to resize it to fit within the sidebar.

Here is the plugin, also available here:

<?php

/*
Plugin Name: APOD Widget
Description: Adds a sidebar widget to display the Astronomy Picture of the Day (APOD)
Author: Paul Lamb
Version: 1.3
Author URI: <a href="http://www.digitalmeandering.com/<br />
Notes:" title="http://www.digitalmeandering.com/<br />
Notes:">http://www.digitalmeandering.com/<br />
Notes:</a>  The framework for this widget was politely borrowed, with no intent to give back, from the Google Search widget

Version 1.3 - Modified by Erich Beyrent [http://www.beyrent.net]
uses a CURL method for retrieving the APOD images, when URL file access is disabled
*/
function widget_apodwidget_init() {

// Check for the required plugin functions. This will prevent fatal
// errors occurring when you deactivate the dynamic-sidebar plugin.
if ( !function_exists('register_sidebar_widget') )
return;

function 
widget_apodwidget_returnimageandlink()
{
$apodAvailable true;
// Grab our date-time stamps
$TodayDt date(ymd);
$MonthDay date(ym);
// Set the image width to 150 pixels
$ImageWidthConst 150;
// Set the base URL for the APOD site, thanks NASA!
$URL 'http://antwrp.gsfc.nasa.gov/apod/';
// Create our HTML file name, following this format:
// 'ap060925.html' where today's date is 09/25/2006
$Filename 'ap'.$TodayDt.'.html';
// Append the file to the URL to create the full URL to today's APOD
$FullURL $URL.$Filename;

// Create the regular expressions for both the regular and large image, even though at the moment I'm not even using the large image
$RegExStringSmall 'src="image/'.$MonthDay.'/(.*)" mce_src="image/'.$MonthDay.'/(.*)"      ';
$RegExStringBig 'href="image/'.$MonthDay.'/(.*)" mce_href="image/'.$MonthDay.'/(.*)"      ';

// Regular expression to validate that we have a good image to present
$RegExImageExtension'.*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg])';

$ch curl_init();
curl_setopt($chCURLOPT_URL$FullURL);
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
$html curl_exec ($ch);
curl_close ($ch);

if(
$html == '')
{
$apodAvailable false;
}

if (
eregi ($RegExStringBig$html$big))
{
$ImageSrcBig $big[1];
}

// Check for the regular image
if (eregi ($RegExStringSmall$html$small))
{
$ImageSrcSmall $small[1];
}

// If the APOD is available, use CURL to fetch the image and write it to a temp file
if ($apodAvailable)
{
// Create the full image URL based on what the regular expression found
$ImageUrlSmall $URL.'image/'.$MonthDay.'/'.$ImageSrcSmall;
$ImageUrlSmall preg_replace("/\">/"'',$ImageUrlSmall);

// Create the full big image URL based on what the regular expression found
$ImageUrlBig $URL.'image/'.$MonthDay.'/'.$ImageSrcBig;
$ImageUrlBig preg_replace("/\">/"'',$ImageUrlBig);

preg_match("/.*\.jpg/"$ImageUrlSmall$matches);
$ImageUrlSmall $matches[0];

preg_match("/.*\.jpg/"$ImageUrlBig$matches);
$ImageUrlBig $matches[0];

// Check to make sure the image is a compatible format
if(eregi($RegExImageExtension$ImageUrlSmall))
{
$ImageDimensionsBig = array();

// Fetch the image
$ch curl_init();
curl_setopt($chCURLOPT_URL$ImageUrlSmall);
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
$image curl_exec ($ch);
curl_close ($ch);

// We need to get around the security restrictions, so we create a temp file to write the image to
$filename "temp_".mt_rand().sha1(time());
$handle = <a href="http://twitter.com/fopen">@fopen</a>('/tmp/'.$filename,"w+");
if (
$handle)
{
flock($handle,LOCK_EX);
if (
fwrite($handle,$image))
{
fclose($handle);

// Get the dimensions of the image for resizing
$ImageDimensionsBig getimagesize('/tmp/'.$filename);

//Delete the file
unlink('/tmp/'.$filename);
}
}

// We want a proportional image, so create our resize percentage based on the width
$ImageResizePercentage = ($ImageDimensionsBig[0] / $ImageWidthConst);
// Set the image width to our constant
$ImageWidthSmall = ($ImageWidthConst);
// Set the image height using the resize percentage, again porpotions are the key
$ImageHeightSmall = <a href="http://twitter.com/">@</a>($ImageDimensionsBig[1] / $ImageResizePercentage);

// Create the hyperlink to the APOD site, wrapped around the image itself, setting the target to a new window and passing in the image height and width
$ImageAndLink '<a href="'.$FullURL.'" target="_blank"><img src="'.$ImageUrlSmall.'" width="'.$ImageWidthSmall.'" height="'.$ImageHeightSmall.'"/></a>';

return 
$ImageAndLink;
}
else {
$apodAvailable false;
}
}
// The APOD wasn't available, return a message indicating as such
if (!$apodAvailable) {
return 
'APOD not available

'
;
}
}

function 
widget_apodwidget($args) {
extract($args);

// Each widget can store its own options. We keep strings here.
$options get_option('widget_apodwidget');
$title $options['title'];

echo 
$before_widget;
echo 
$before_title $title $after_title;
$ApodImageAndLink widget_apodwidget_returnimageandlink();
echo 
'
<div style="margin-top: 5px; text-align: left">'
.$ApodImageAndLink.'</div>
'
;
echo 
$after_widget;
}

// This function creates the widget control, using the built in widget abilities for controling widgets (they only get the change the title, it's no big deal)
function widget_apodwidget_control() {

// Get our options and see if we're handling a form submission.
$options get_option('widget_apodwidget');
if ( !
is_array($options) )
$options = array('title'=>__('APOD''widgets'));
if ( 
$_POST['apodwidget-submit'] ) {

// Remember to sanitize and format use input appropriately.
$options['title'] = strip_tags(stripslashes($_POST['apodwidget-title']));
update_option('widget_apodwidget'$options);
}

// Be sure you format your options to be valid HTML attributes.
$title htmlspecialchars($options['title'], ENT_QUOTES);

// Here is our little form segment. Notice that we don't need a
// complete form. This will be embedded into the existing form.
echo '
<p style="text-align: right"><label for="apodwidget-title">' 
__('Title:') . ' <input type="text" value="'.$title.'" name="apodwidget-title" id="apodwidget-title" style="width: 200px" /></label></p>
'
;
echo 
'<input type="hidden" value="1" name="apodwidget-submit" id="apodwidget-submit" />';
}

// This registers our optional widget control form. Because of this
// our widget will have a button that reveals a 200x100 pixel form.
register_widget_control(array('APOD''widgets'), 'widget_apodwidget_control'200100);

// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget(array('APOD''widgets'), 'widget_apodwidget');
}

// Run our code later in case this loads prior to any required plugins.
add_action('widgets_init''widget_apodwidget_init');
?>

[tags][/tags]

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thx a lot

It works great, the only thing I did was to change the width adapting it to my sidebar... you can check it on www.astronomos.org

it has been very useful,

it has been very useful, helpful and valuable tutorial i love the way of presentation these informative scripts.
free wordpress themes

fyi the actual url I uploaded

fyi the actual url I uploaded to is http://arcturus.tv/php/apod_widget.php

thanks for that & reply :)

thanks for that & reply :)
but maybe I explained it bad,

because I have no clue on PHP, I've only worked with ones with stepby step installation instructions.

I uploaded the file to my server without any tinkering, and when I open the file through a web browser: http://xxx.com/apod_widget.php I get the error:

function add_action() in /home/users/2/namaste.jp/web/php/apod_widget.php on line 167

I'm thinking is there a "function" or something I need to input on the php file to get it to work?

my hosting company says server is ok for PHP5 but not for PHP4...

I'm just having difficulty understanding why the /apod_widget.php wont execute as it is through a web brouser...

Hi Gus, I fixed the link -

Hi Gus,

I fixed the link - please let me know if you have any further problems!

http://www.beyrent.net/downloads/apod_widget.tar.gz

hi there, love the APOD

hi there, love the APOD widget,
I was wanting to get it installed on my website as well but get the error similar to this (below) when I copy and upload /apod_widget.php

Fatal error: Call to undefined function add_action() in /home/ebeyrent/public_html/downloads/apod_widget.php on line 182

any ideas? it seems to be working for you on this page but get the error when you open this page:

http://www.beyrent.net/downloads/apod_widget.php

Thank You

Thank You

Outstanding! I ran into the

Outstanding! I ran into the same problem with my hosting provider, but didn't get too far with my limited PHP skills. I'll have to go back and look at using this. Thanks!

[...] Christine «

[...] Christine « Astronomy Picture of the Day Widget for Wordpress 13 02 [...]

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011