Grab Wallpaper (or any image) from Google on-the-fly
Posted by matthew | Posted in Code Snippets | Posted on 02-12-2009
Tags: Google, PHP
1
Sometimes quite handy, this code snippet allows you to display an image from a Google image search on-the-fly. This means the image will always be up-to-date and often what you were expecting
<?php
$term = "metal gear solid 4: guns of the patriots";
$term = preg_replace('/ /', '%20', $term);
$url = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".$term."%20wallpaper%20&imgsz=huge&as_filetype=jpg&key=MYKEY&rsz=large";
$subject = $str;
$subject = preg_replace('/.*?"unescapedUrl":"/', '', $str);
$subject = preg_replace("/.jpg.*/", '' , $subject);
echo "<img src="".$subject.".jpg" alt="" />";
?>
This code will display the image in HTML from the term you provide. You must obtain a Google API key first and put it in place of ‘MYKEY’, but feel free to change the search term to whatever you like. You can also change the size of the image to search for to ’small’ , ‘medium’ or ‘large’ from where it currently says ‘huge’. The ‘rsz’ value is best kept set to ‘large’ as this is the size of the pool of results it will choose the image from.
This code could be adapted to display multiple images, use the image as a background image or whatever you require to be honest, have fun!




lovely, i just added up another emo backgrounds in my blog
http://tinyurl.com/y9rfwk2