|
<?php
/*
Title: Thumbnail Keyword
Version: 1.01
Author: Jon Berg <jon.berg|a|gmail.com> 2005
Description: This code will display a image for any keyword given in parameter k. The image displayed is the
thumbnail of the first image returned by Google's image search.
This can be useful in any web page where you need a thumbnail related to some topic
and do not have the time or energy to make one for your self, for example in blogs
or frequently updated news pages.
Usage: in any html code <img src="/image.php?k=yourkeyword">
Copyright: This code is copyrighted Jon Berg. Permission is given to use this code as long as you provide a link to
http://turtlemeat.com/keyword-thumb/ from a reachable page on the site where you use it on. Linkware.
You are given permission to modify this code as long as you include this header, and state in the
new code that it is derived from Keyword Thumb by Jon Berg, (and provide a link).
Disclaim: You use this software on your own risk, there is no guarantees for this software.
*/
//use strict SafeSearch 0 or 1 , no offensive language or images
$use_safe_search = 0;
//fill in your mysql information
$mysql_user = ""; //the username for the mysql server
$mysql_password = ""; //the password for the mysql server
$mysql_server = ""; //hostname or ipaddress for the mysqlserver
$mysql_database = ""; //database to use on the mysqlserver
//no need to edit anything futher down
function get_image_from_mysql($keyword)
{
global $mysql_user, $mysql_password, $mysql_server, $mysql_database;
$link = mysql_connect($mysql_server, $mysql_user, $mysql_password)
or die("Could not connect : " . mysql_error());
mysql_select_db($mysql_database) or die("Could not select database");
mysql_query("CREATE TABLE IF NOT EXISTS KeywordImages(keyword TEXT, INDEX (keyword(10)), image MEDIUMBLOB)")
or die("Query failed : " . mysql_error());
$q = 'SELECT image FROM KeywordImages WHERE keyword = "' . mysql_escape_string($keyword) . '"';
$result = mysql_query($q) or die("Query failed : " . mysql_error());
$tmp = mysql_fetch_row($result);
if ($tmp === FALSE) return NULL;
if ($tmp )
{
return base64_decode($tmp[0]);
}
}
function store_image_mysql($keyword, $image_data)
{
global $mysql_user, $mysql_password, $mysql_server, $mysql_database;
$link = mysql_connect($mysql_server, $mysql_user, $mysql_password)
or die("Could not connect : " . mysql_error());
mysql_select_db($mysql_database) or die("Could not select database");
mysql_query("CREATE TABLE IF NOT EXISTS KeywordImages(keyword TEXT, INDEX (keyword(10)), image MEDIUMBLOB)")
or die("Query failed : " . mysql_error());
$q = 'INSERT INTO KeywordImages SET keyword = "' . mysql_escape_string($keyword) . '",
image = "' . base64_encode($image_data) . '"';
mysql_query($q) or die("Query failed : " . mysql_error());
}
$image_data = get_image_from_mysql($_GET['k']);
if ($image_date != NULL)
{
header("Content-type: image/jpg");
print $image_data;
exit (6);
}
$google_url = 'http://images.google.com/images?q=' . urlencode($_GET['k']) .'&hl=en&btnG=Google+Search' ;
if ($use_safe_search == 1)
{
$google_url = $google_url . '&safe=active';
}
$search_result = @file_get_contents ( $google_url);
$position = strpos ( $search_result, '<img src=/images?q=' );
if ($position === false)
{
exit(4); //not found, to spesific query
}
$position = $position + 9;
$position_end = strpos( $search_result, ' ', $position);
$thumb_url = substr($search_result, $position, $position_end - $position);
$thumb_url = 'http://images.google.com' . $thumb_url ;
//you can not just redirect because, Google's datacenters differs and some
user may not be able to get the redirected url at their datacenter
header("Content-type: image/jpg");
$imaged = @file_get_contents ( $thumb_url);
store_image_mysql($_GET['k'], $imaged);
print $imaged;
exit (3);
?>
|
shows the code for version 1.01.