Recently over on Blogshares we’ve been having a bit of a brainstorm about how to make things better. One of the suggestions to which I quickly replied, “I could do that easy, tonight!” was to alter the image people use to claim their blogs in the game so that it would show the current valuation of the blog on the claimed page.
My thinking at the time was, easy just use PHP to add the info pulled from the DB onto the image and pass that back to the browser. So that’s what I did and it was easy (here’s the code – excluding the DB calls.)
However this meant that the image was now blogshares.php and not blogshares.jpg. Well with thousands of claim codes out there already in place I couldn’t make everyone change their code to the new one could I?
This meant I needed to either have PHP handle .jpg requests via .htaccess or some other method or make the jpg auto-forward to the PHP script somehow. Neither was looking attractive as I don’t have access to the Blogshares administration tools. While researching the .htaccess method I found people were having irregular results. It’s a bit hit and miss. I was also told it coulsnt’ be done on the BS server anyhow so method 2. To make the .jpg point to another place you need to create a symlink. However the image itself is actually already a symlink to another image! I know, my head would explode if I thought about that for too long.
So in came hidden mystery option 3, which on the face of it is so simple it’s unimaginable how I didn’t think of it first. Just use the web server! The script was renamed index.php and placed in a folder called blogshares.jpg. And what happens when you hit a folder URL? Yep, it looks for the index page. So now the image is served nicely onto the pages of the 12,474 (at time of writing) existing claimants out there without them having to do a thing!
This is a great thing to remember for when you want to host images on a rotation or whatever in say a forum signature or some other place where you can’t use the .php extension for an image. Stick the php script you want to return in a folder named whatever.jpg !!
With relation to the BS script one thing I would recommend if you do have an existing code is remove the width and height attributes to make it look less squished. The result is something like:
If you have a blog and want to know how much it’s worth add this code to your blog’s main page(if it comes back as B$0 you’re not listed, so go and add your blog!):
<img src="http://blogshares.com/images/blogshares.jpg" alt="Listed On Blogshares" />
*Edit – 26/Feb/09* An example of how I’ve utilised this technique(ok it’s .png not .jpg but meh, pics is pics!) is at http://billy.the-kid.org/cotd.png this is a folder that uses index.php to provide the image randomly using the following code
Toggle Code:
<?php
$code = imagecreatetruecolor(250,250); // Sets blank image placeholder (black)
$back = imagecolorallocate($code, 255, 255, 255); // White (R G B)
if (!isset($_GET['text'])) {
$textfile = "random.txt";
$items = file("$textfile");
$item = rand(0, sizeof($items)-1);
$cotd = urlencode($items[$item]);
}
else $cotd = urlencode($_GET['text']);
$SrcImage = imagecreatefrompng("http://chart.apis.google.com/chart?cht=qr&chs=250x250&chl=$cotd"); // Set image to be pulled in
imagefilledrectangle($code, 1, 1, 248, 248, $back); // Fill blank image with background colour
imagecopy ($code, $SrcImage, 1, 1, 1, 1, 248, 248); // Pull it in!
header("Content-type: image/png"); // Assign MIME for browser
imagepng($code); // Output image to browser
imagedestroy($code); // Release memory
?>