Habbo avatar caching [ANY CMS]

Status
Not open for further replies.
Newbie Spellweaver
Joined
Oct 4, 2009
Messages
15
Reaction score
10
Hello, i recently tested my site speed and saw the habbo images were slowing down my site! So i coded a caching script, and going to release it. This works for any cms, only needs a few manual changes to it. Any code improvements are welcome(I'm new to PHP)!

1.Make a folder called cache.
2.Create a file cache.php with content:
PHP:
<?php
  $figure = $_GET['figure'];
	$action = $_GET['action'];
	$direction = $_GET['direction'];
	$head_direction = $_GET['head_direction'];
	$gesture = $_GET['gesture'];
	$size = $_GET['size'];
    $imgFile = "$figure$action$direction$head_direction$gesture$size";
    $imagesPath =  $imgFile;

    if(!file_exists(($imagesPath))) {
       
        $otherSiteUrl  = "http://habbo.com/habbo-imaging/avatarimage?figure=$figure&action=$action&direction=$direction&head_direction=$head_direction&gesture=$gesture&size=$size";
        file_put_contents($imagesPath, file_get_contents($otherSiteUrl));
        }
    
 header("Content-Type: image/gif");
        readfile($imagesPath);
        
	?>
3.Change to in all cms files (expect the caching script)
And you're done!

Demo:

EDIT:Removed unused parts
 
Last edited:
You have too much going on.

This is my code (works with uber!)

PHP:
function RetrieveFigure($id){ 
            $result = mysql_query("SELECT * FROM users WHERE id = '".$id."'"); 
             
            while ($row = mysql_fetch_array($result)){ 
                if (!file_exists('./cache/figures/'.$row['look'])){                    
                    file_put_contents('./cache/figures/'.$row['look'].'.png', file_get_contents('http://www.habbo.com/habbo-imaging/avatarimage?figure='.$row['look'].'.gif')); 
                } 
                echo '<img src="./cache/figures/'.$row['look'].'.png" />'; 
            } 
        }

Alot of hotels don't have figures because they use direction, and actions and size and such; so I would keep it basic at what I have.
 
You have too much going on.

This is my code (works with uber!)

PHP:
function RetrieveFigure($id){ 
            $result = mysql_query("SELECT * FROM users WHERE id = '".$id."'"); 
             
            while ($row = mysql_fetch_array($result)){ 
                if (!file_exists('./cache/figures/'.$row['look'])){                    
                    file_put_contents('./cache/figures/'.$row['look'].'.png', file_get_contents('http://www.habbo.com/habbo-imaging/avatarimage?figure='.$row['look'].'.gif')); 
                } 
                echo '<img src="./cache/figures/'.$row['look'].'.png" />'; 
            } 
        }

Alot of hotels don't have figures because they use direction, and actions and size and such; so I would keep it basic at what I have.

I hate how 1. You're doing a query to get it, instead of caching it on log in and 2. You only need to retrieve the look yet you're retrieving all of the user's info.

Mine's like...

PHP:
    public function avatarURL($figure)
    {   
        $hash = md5($figure);
                
        if($SETTINGS['cache_figures'] == 1)
        {
            if(file_exists("themes/cache/avatars/".$figure.",".$hash.".gif"))
            {
                $URL = $HOTEL['url'] . "/themes/cache/avatar/".$figure.",".$hash.".gif";
            }
            else
            {
                $URL = "http://www.habbo.com/habbo-imaging/avatarimage?figure=".$figure."&action=wav&direction=2&head_direction=3&gesture=srp&size=l";
                $i = file_get_contents($URL);
                $f = fopen("themes/cache/avatars/".$figure.",".$hash.".gif","w+");
                fwrite($f,$i);
                fclose($f);
                $URL = $HOTEL['url'] . "/themes/cache/avatar/".$figure.",".$hash.".gif";
            }
        }
        else
        {
            $URL = "http://www.habbo.com/habbo-imaging/avatarimage?figure=$figure&action=wav&direction=2&head_direction=3&gesture=srp&size=l";
        }
                
        return $URL;                
    }
 
I hate how 1. You're doing a query to get it, instead of caching it on log in and 2. You only need to retrieve the look yet you're retrieving all of the user's info.

Mine's like...

PHP:
    public function avatarURL($figure)
    {   
        $hash = md5($figure);
                
        if($SETTINGS['cache_figures'] == 1)
        {
            if(file_exists("themes/cache/avatars/".$figure.",".$hash.".gif"))
            {
                $URL = $HOTEL['url'] . "/themes/cache/avatar/".$figure.",".$hash.".gif";
            }
            else
            {
                $URL = "http://www.habbo.com/habbo-imaging/avatarimage?figure=".$figure."&action=wav&direction=2&head_direction=3&gesture=srp&size=l";
                $i = file_get_contents($URL);
                $f = fopen("themes/cache/avatars/".$figure.",".$hash.".gif","w+");
                fwrite($f,$i);
                fclose($f);
                $URL = $HOTEL['url'] . "/themes/cache/avatar/".$figure.",".$hash.".gif";
            }
        }
        else
        {
            $URL = "http://www.habbo.com/habbo-imaging/avatarimage?figure=$figure&action=wav&direction=2&head_direction=3&gesture=srp&size=l";
        }
                
        return $URL;                
    }

I actually grabbed that from the post where I had showed everyone it. Now it just uses $_SESSION['figure'] and such. And why would you have an option on if they should cache them or not, the avatars turn out broken if you add the action/direction/gesture etc. Just automatically cache the figures without the user's consent.
 
Status
Not open for further replies.
Back