Dynadot

PageRank Image Generator Script (PHP)

Spaceship Spaceship
Watch
Impact
83
I got bored so I made this, basically it just outputs an image of the PageRank, click here for an example of an output.

Usage: Save the script as pr.php. Go to pr.php?url=google.com to get Google's PageRank. Just replace google.com to whatever site you wish to generate a PageRank image for.

How it works:
1) It takes a base image (stored on TinyPic's servers), and saves it to your server. After this, it uses the file stored on your server.

2) It goes to ttfonts.net, and saves the Arial Black font to the directory of the script. After this, it uses the file stored on your server.

3) It then contact's Google's toolbar queries server to retrieve the PageRank, and utilizes PHP's image functions to render the PageRank image.

Here is the script, just upload and run:
PHP:
<?php
header("Content-type: image/png");

class GooglePageRankChecker
{
    private static $instance;
    function getRank($page)
    {
        if (!isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance->check($page);
    }
    function stringToNumber($string, $check, $magic)
    {
        $int32  = 4294967296;
        $length = strlen($string);
        for ($i = 0; $i < $length; $i++) {
            $check *= $magic;
            if ($check >= $int32) {
                $check = ($check - $int32 * (int) ($check / $int32));
                $check = ($check < -($int32 / 2)) ? ($check + $int32) : $check;
            }
            $check += ord($string{$i});
        }
        return $check;
    }
    
    function createHash($string)
    {
        $check1 = $this->stringToNumber($string, 0x1505, 0x21);
        $check2 = $this->stringToNumber($string, 0, 0x1003F);
        
        $factor     = 4;
        $halfFactor = $factor / 2;
        
        $check1 >>= $halfFactor;
        $check1 = (($check1 >> $factor) & 0x3FFFFC0) | ($check1 & 0x3F);
        $check1 = (($check1 >> $factor) & 0x3FFC00) | ($check1 & 0x3FF);
        $check1 = (($check1 >> $factor) & 0x3C000) | ($check1 & 0x3FFF);
        
        $calc1 = (((($check1 & 0x3C0) << $factor) | ($check1 & 0x3C)) << $halfFactor) | ($check2 & 0xF0F);
        $calc2 = (((($check1 & 0xFFFFC000) << $factor) | ($check1 & 0x3C00)) << 0xA) | ($check2 & 0xF0F0000);
        
        return ($calc1 | $calc2);
    }
    function checkHash($hashNumber)
    {
        $check = 0;
        $flag  = 0;
        
        $hashString = sprintf('%u', $hashNumber);
        $length     = strlen($hashString);
        
        for ($i = $length - 1; $i >= 0; $i--) {
            $r = $hashString{$i};
            if (1 === ($flag % 2)) {
                $r += $r;
                $r = (int) ($r / 10) + ($r % 10);
            }
            $check += $r;
            $flag++;
        }
        
        $check %= 10;
        if (0 !== $check) {
            $check = 10 - $check;
            if (1 === ($flag % 2)) {
                if (1 === ($check % 2)) {
                    $check += 9;
                }
                $check >>= 1;
            }
        }
        
        return '7' . $check . $hashString;
    }
    
    function check($page)
    {
        
        $socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
        
        if ($socket) {
            $out = "GET /tbr?client=navclient-auto&ch=" . $this->checkHash($this->createHash($page)) . "&features=Rank&q=info:" . $page . "&num=100&filter=0 HTTP/1.1\r\n";
            $out .= "Host: toolbarqueries.google.com\r\n";
            $out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
            $out .= "Connection: Close\r\n\r\n";
            fwrite($socket, $out);
            $result = "";
            while (!feof($socket)) {
                $data = fgets($socket, 128);
                $pos  = strpos($data, "Rank_");
                if ($pos !== false) {
                    $pagerank = substr($data, $pos + 9);
                    $result += $pagerank;
                }
            }
            fclose($socket);
            return $result;
        }
    }
}

$pageRank=GooglePageRankChecker::getRank(@$_GET["url"]);

if(!is_numeric($pageRank)){
	$pageRank = 0;
}

$negStr="";

if(!file_exists("base_image")){
	$k = @file_get_contents("http://i45.tinypic.com/s0y0z9.jpg");
	$n = @fopen("base_image.png","w");
	@fwrite($n,$k);
	@fclose($n);
}

if(!file_exists("arial_black_font.ttf")){
	$k = @file_get_contents("http://ttfonts.net/sfonts/0/02574_arial_bb.ttf");
	$n = @fopen("arial_black_font.ttf","w");
	@fwrite($n,$k);
	@fclose($n);
}

$im     = imagecreatefrompng("base_image.png");
$green = imagecolorallocate($im, 52, 156, 61);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$red2 = imagecolorallocate($im, 220, 0, 0);
$font="arial_black_font.ttf";
	if(strstr($pageRank,"-")){
		$pageRank = str_replace("-","",$pageRank);
		$green = $red;
		$white = $red2;
		$string = "Google PR(-".$pageRank.")";
		$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
		imagestring($im, 3, 23, 55, $string, $green);
		$negStr = "-";
	}else{
		$string = "Google PR-".$pageRank;
		$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
		imagestring($im, 3, 36, 55, $string, $green);
	}


	if($pageRank == 1){
		$pager=$pageRank*15;
	}else{
		$pager=$pageRank*11.5;
			if($pager==0){
				$pager=5;
			}
	}
imagefilledrectangle($im, 5, 32, $pager, 49, $green);
	if($pageRank != 10){
		imagettftext($im, 30, 0, 150, 52, $black, $font, $negStr.$pageRank);
		imagettftext($im, 30, 0, 147, 50, $white, $font, $negStr.$pageRank);
	}else{
		imagettftext($im, 30, 0, 135, 54, $black, $font, $negStr.$pageRank);
		imagettftext($im, 30, 0, 132, 51, $white, $font, $negStr.$pageRank);
	}
imagepng($im);
imagedestroy($im);
?>

Notes: You must have file_get_contents, fsockopen, and fopen enabled on your server for this to work, as well as you have to allow the script to write to the directory for the initial run (CHMOD 777).

You should be able to run this on most servers right out of the box, if not, check the notes.

Edit: To see a live example, go to: http://meevy.com/pr.php?url=namepros.com
Please refrain from hotlinking to the image generated on Meevy, it's not guaranteed to stay up that long.
 
Last edited:
3
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Warning: imagettftext(): Could not find/open font in /var/www/vhosts/account/httpdocs/de/pages/pr/pr.php on line 160

Warning: imagettftext(): Could not find/open font in /var/www/vhosts/account/httpdocs/de/pages/pr/pr.php on line 161
‰PNG

All files have chmod 777

Do you know where is the Problem?
 
0
•••
Thanks...
Was just looking for something like this.
 
1
•••
Warning: imagettftext(): Could not find/open font in /var/www/vhosts/account/httpdocs/de/pages/pr/pr.php on line 160

Warning: imagettftext(): Could not find/open font in /var/www/vhosts/account/httpdocs/de/pages/pr/pr.php on line 161
‰PNG

All files have chmod 777

Do you know where is the Problem?

Manually download the font at: http://ttfonts.net/sfonts/0/02574_arial_bb.ttf

Then put it in the same directory as pr.php and rename the font to arial_black_font.ttf
 
0
•••
Manually download the font at:

Then put it in the same directory as pr.php and rename the font to arial_black_font.ttf

Nope, this couldn't solve the Problem. If i set Display Errors off it works, only the big number is missing.

I have no idea what's wrong.
The idea with the script is great but a little bit tricky^^

Deleting this code solves the Problem with displaying errors:

PHP:
    if($pageRank != 10){
        imagettftext($im, 30, 0, 150, 52, $black, $font, $negStr.$pageRank);
        imagettftext($im, 30, 0, 147, 50, $white, $font, $negStr.$pageRank);
    }else{
        imagettftext($im, 30, 0, 135, 54, $black, $font, $negStr.$pageRank);
        imagettftext($im, 30, 0, 132, 51, $white, $font, $negStr.$pageRank);
    }
 
0
•••
Back