red = 255; $textColor->green = 255; $textColor->blue = 255; $backgroundColor->red = 0; $backgroundColor->green = 0; $backgroundColor->blue = 0; /*-------------------------------------------------------- | | You should not need to change anything after this | \--------------------------------------------------------*/ // Connects to a mysql database function connect($username, $password, $database) { mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); } // Takes a search string and returns a random image url function get_image_url($q){ $q = urlencode($q); // The page of google images to look at. Use 0 for the first page $start = "0"; $htmlString = file_get_contents("http://images.google.com/images?safe=off&q=$q&start=$start"); $htmlArray = preg_match_all("-http[^\"]*jpg-", $htmlString, $imageUrls); return $imageUrls[0][array_rand($imageUrls[0])]; } //Get the client's ip $ip = $_SERVER['REMOTE_ADDR']; // Get the client's browser $browser = $_SERVER['HTTP_USER_AGENT']; if(USE_DATABASE){ // Connect to the sql server connect(MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE); // Record this hit to the database mysql_query("INSERT INTO hits (ip, browser) VALUES (INET_ATON('$ip'), '$browser');"); // Get the total number of hits out of the database $hits = number_format(mysql_num_rows(mysql_query("SELECT hit_id FROM hits;"))); $bottomString = "Avatar Hits: $hits"; }else{ $bottomString = $noDatabaseString; } $topString = "Your IP: $ip"; // Send the image header header ("Content-type: image/jpg"); // Get the height of this font size $fontHeight = imagefontheight($fontSize); // Get the image url from google $source = get_image_url($search); // Find the dimensions of the source image list($width_orig, $height_orig) = getimagesize($source); // width/hight ratio of original image $ratio_orig = $width_orig/$height_orig; // Calculate the image height $imageHeight = $width/$ratio_orig; // Calculate the avatar height $height = $imageHeight + (2 * $fontHeight) + (4 * $margin); // Create the image $im = imagecreatetruecolor($width, $height); // Set a color for the text $textColor = imagecolorallocate($im, $textColor->red, $textColor->green, $textColor->blue); // Set a color for the background $background = imagecolorallocate($im, $backgroundColor->red, $backgroundColor->green, $backgroundColor->blue); // Fill the image with the background color imagefill($im, 0, 0, $background); // Get the source image $srcImage = imagecreatefromjpeg($source); // Insert the source image imagecopyresampled($im, $srcImage, 0, ($margin + $margin + $fontHeight), 0, 0, ($width), ($imageHeight), $width_orig, $height_orig); // Print the strings to the image imagestring ($im, $fontSize, $margin, $margin, $topString, $textColor); imagestring ($im, $fontSize, $margin, ($height - ($fontHeight + $margin)), $bottomString, $textColor); imagejpeg($im); imagedestroy($im); ?>