Share/Bookmark

Featured Posts

SharePoint Ticker Web Part from Existing Data Ever wanted to create a ticker in SharePoint without having to buy the $99 add-on from Microsoft, well here's the code: Simply create a new Web Part using the 'Content Editor' and open the 'Source Editor...'...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Whats next for Nintendo? Today I noticed that Nintendo have had falling profits for 2010 and predict even less sales for next year. The Wii 2 hasn`t been officially announced and from what I can see is not really on the radar....

Read more

7 Great Free Android Apps The Android mobile operating system is both fast, reliable and can do everything that the Apple OS can and a little more (all for normally a fraction of the price.) plus it supports Flash and soon Flash...

Read more

Grab Wallpaper (or any image) from Google on-the-fly

Posted by matthew | Posted in Code Snippets | Posted on 02-12-2009

Tags: ,

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!

Comments (1)

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

Write a comment