Skip to content

Instantly share code, notes, and snippets.

@emilkje
Created September 11, 2011 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilkje/1209743 to your computer and use it in GitHub Desktop.
Save emilkje/1209743 to your computer and use it in GitHub Desktop.
Pulls current weather icon from Yahoo and displays it with alpha transparency
<?php
$url="http://weather.yahoo.com/norway/oppland-fylke/gjovik-858964/"; //Yahoo weather URL for you location
$file_contents = file_get_contents($url); //Fetching the contents from the url
$divStart = '<div class="forecast-icon" style="background:url(' . "'"; //HTML before weather image
$strEnd = "'); _background-image/* */: none;"; //HTML after weather image
//Preparing for image url extraction via substr()
$start = strpos($file_contents, $divStart) + 50;
$end = strpos($file_contents, $strEnd);
$length = $end-$start;
$imagepath=substr($file_contents, $start , $length); //Pulls path to image
$image=imagecreatefrompng($imagepath); //Pulls image source into PHP
imagealphablending($image, true); //Enable alpha blending (important)
imagesavealpha($image, true); //Applies alpha to image.
header('Content-Type: image/png'); //Identifies itself as a PNG image
imagepng($image); //Outputs image contents
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment