Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Apod Widget
- Version: 1.0
- Plugin URI: http://www.vdfn.altervista.org/
- Description: The APOD widget, like the wordpress one.
- Author: Piero VDFN
- Author URI: http://www.vdfn.altervista.org/
- */
- /**
- * This is the file of the image URL cache.
- */
- define('APOD_CACHE', CACHE_DIR.'apod.tmp');
- /**
- * This is the thumb file.
- */
- define('APOD_THUMB', IMAGES_DIR.'apod_thumb.jpg');
- /**
- * This is the width of the thumbnail in pixels.
- */
- define('APOD_THUMB_WIDTH', 150);
- /**
- * This is the target attribute of the link of the widget.
- * It it breaks your validation, leave it void.
- */
- define('APOD_TARGET', '');
- /**
- * This is the an advanced function: if filemtime does
- * not work, set it to true.
- */
- define('APOD_CLEARSTAT', false);
- /**
- * This function returns the URL of the Apod Image.
- *
- * @return string: The URL
- */
- function apod_geturl() {
- global $apod_image;
- if(!empty($apod_image)) {
- return $apod_image;
- }
- if(APOD_CLEARSTAT) {
- clearstatcache();
- }
- if(@filemtime(APOD_CACHE)>strtotime('today')) {
- $apod_image=io_load_file(APOD_CACHE);
- return $apod_image;
- }
- $baseurl='http://apod.nasa.gov/apod/';
- $pageurl=$baseurl.'ap'.date('ymd').'.html';
- // Get the page with cURL
- $ch=curl_init();
- curl_setopt($ch, CURLOPT_URL, $pageurl);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $pagehtml=curl_exec($ch);
- curl_close($ch);
- // If we had an error with cURL...
- if($pagehtml===false) {
- return false;
- }
- // Get the image URL
- $searchfor='<IMG SRC="';
- $src_beg=strpos($pagehtml, $searchfor);
- if($src_beg===FALSE) {
- return false;
- }
- $src_beg+=strlen($searchfor);
- $src_end=strpos($pagehtml, '"', $src_beg);
- $url=$baseurl.substr($pagehtml, $src_beg, $src_end-$src_beg);
- $apod_image=$url;
- io_write_file(APOD_CACHE, $url);
- return $url;
- }
- /**
- * This function creates the thumbnail of the Apod Image.
- *
- * @return boolean: Is the thumb ok?
- */
- function apod_thumb() {
- if(APOD_CLEARSTAT) {
- clearstatcache();
- }
- if(@filemtime(APOD_THUMB)>strtotime('today')) {
- return true;
- }
- if(file_exists(APOD_THUMB)) {
- @unlink(APOD_THUMB);
- }
- // Get the image URL
- @$image_url=apod_geturl();
- if($image_url===false) {
- return false;
- }
- // Download the image with cURL
- $ch=curl_init();
- curl_setopt($ch, CURLOPT_URL, $image_url);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $image=curl_exec($ch);
- curl_close($ch);
- if($image===false) {
- return false;
- }
- // Save a temporary copy of the image to get its width
- $tmpfile=CACHE_DIR.'apod-tmp-'.mt_rand();
- if(!io_write_file($tmpfile, $image)) {
- $image='';
- unset($image);
- return false;
- }
- list($width, $height)=getimagesize($tmpfile);
- unlink($tmpfile);
- unset($tmpfile);
- // Save the new dimensions...
- $newwidth=APOD_THUMB_WIDTH;
- $newheight=$newwidth*$height/$width;
- // Make the thumb
- // @link http://it.php.net/manual/en/function.imagecopyresized.php
- $thumb=imagecreatetruecolor($newwidth, $newheight);
- $source=imagecreatefromstring($image);
- unset($image); // Free memory
- if($source===false) {
- return false;
- }
- imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- $result=imagejpeg($thumb, APOD_THUMB);
- imagedestroy($thumb);
- imagedestroy($source);
- return $result;
- }
- /**
- * This function is the handler for the APOD Widget.
- *
- * @return array: The title and the content of the widget
- */
- function apod_widget() {
- $fullurl=apod_geturl();
- $page='http://apod.nasa.gov/apod/ap'.date('ymd').'.html';
- if(apod_thumb()) {
- $image=BLOG_BASEURL.APOD_THUMB;
- } else {
- $image=$fullurl.'" style="width: 150px;" ';
- }
- if(APOD_TARGET!='') {
- $target=' target="'.APOD_TARGET.'"';
- }
- $content="<p>\n<a href=\"{$page}\"{$target}>\n"."<img src=\"{$image}\" ".
- "alt=\"Astronomy Picture of the Day\" title=\"Astronomy Picture of the Day\" />\n</a>\n</p>";
- return array(
- 'subject'=>'APOD',
- 'content'=>$content
- );
- }
- register_widget('apod', 'APOD', 'apod_widget');
Advertisement
Add Comment
Please, Sign In to add comment