Reputation: 23
Ok, here is what I'm trying to do.
On my site, I have a set of images I've uploaded to use as my avatar. Those sites usually asks for different sizes and are very picky to what kind of URLs they accept.
What I'm trying to do is to get a Gravatar image, from the Gravatar server, and serve it using a customized URL, on the fly or with cache.
My Gravatar URL for an image 100x100px:
http://gravatar.com/avatar/b6e428090e62182266a4e9dd87297ee1.png?s=100
What I want to offer to websites, more or less:
http://alenonimo.com.br/avatar/avatar100.png
Now let me explain the issue I'm trying to solve here, because it's interesting.
Some sites accepts third party URLs for avatar images, like phpBB and the like. They also have different sizes they prefer, like 100x100 pixels, 120x120 pixels, 200x200 pixels, etc. Yet, they refuse to accept the Gravatar URL if it comes with the "s" variable on the URL, which defines the size of the images served from Gravatar. The default size is always 80x80 pixels without the option.
So I want to serve those sites the Gravatar images and defining the size on the URL, in a way it doesn't notice that's coming from Gravatar's image generator. I want those sites to think the image actually comes from my website. It would allow me to set the size for those sites that doesn't accept Gravatar and luckily allow me to change all the avatar images on all those sites at once by just going to Gravatar.
I'm pretty sure it could be done using Apache's RewriteEngine, yet I lack the code-fu to do it. All the examples that I've found on the net regards redirecting content from the same site to somewhere else on the same site, which is not the case. I also wanted to make sure the size attribute would be a more or less valid number, instead of accepting anything. So a file called "100.png" would be okay but "banana.png" would not.
This for itself would be interesting but it occured me another thing. What if the site doesn't accept PNG files but accept JPG or GIF files? Probably wouldn't happen on sites which just show images but it would be interesting for me if I also could offer images to sites which uploads from URLs too. Also, maybe making sure something shows up even if Gravatar goes offline would be interesting too, like a cache system. That could be achievable with PHP but I'm not sure how complicated it would be.
Could someone help me with the RewriteEngine rules? Or even giving me some pointer on how to make this PHP script? Would it be interesting to someone else in here?
Upvotes: 1
Views: 598
Reputation: 3967
Here you go, you need to make your server listen to .php file with .png extension which can be done quite easily using .htacces
RewriteEngine on
RewriteRule ^avatar/([a-z,A-Z,0-9,-_]+)$ index.php?avatar_param=$1
Php Script
<?php
if(isset($_GET['avatar_param']))
{
$avatar_size = filter_var($_GET['avatar_param'],FILTER_SANITIZE_NUMBER_INT);
$user_email_hash = 'b6e428090e62182266a4e9dd87297ee1';
$get_gravar_image = 'http://gravatar.com/avatar/'.$user_email_hash.'.png?s='.$avatar_size;
echo '<img src="'.$get_gravar_image.'" />';
}
?>
By looking the your provided url structure i have used the numbers as the size of the image, just use the above code it will work for you.
Just try to play with the size in the url you will find it dynamic and working the nice way
Demo
http://thetutlage.com/demo/avatarProblem/avatar/avatar100.png
Upvotes: 2