Image-Proxy (with Cache) for Achievements, Spells, Items, Events by Id

An issue I regularly come across in web applications for Trinity and WoW-ish stuff in general is how to get an image

for something, when you only have some id at hand. Usually icons in WoW are named and they can be reused, but you

always have to know the name, which isn’t really ideal.

I therefore wrote a php wrapper to download icons from wowhead or openwow and cache them locally to not steal their

traffic more then necessary.

As an example the following images:

First is, second In case nothing is found it returns the proper red questionmark on black ground.

(Unfortunately the forum doesn’t allow that kind of links for images, but that can easyil be mitigated by rewrite rules, which would circumvent

the call to the php interpreter at all for already cached images)

Event=181 (OpenWow doesn’t seem to have Event images)




As they use kinda the same scripts to render their page you only have to exchange the base link to their page:

$link = ““.$type.”=”.$item_id;

$link = "".$type."=".$item_id;
to translate from Id->IconName. And then put in the according content distribution servers. You can probably mix those two without any further problems.

$img = file_get_contents("".strtolower($match['1']).".jpg");

$img = file_get_contents("".strtolower($match['1']).".jpg");
So here without further ado, the whole small script. Please remember creating the appropriate directory structure and to apply proper chmod for it to be writable by you webserver user.


<?php error_reporting(E_ALL); header("Content-Type: image/jpeg"); if (isset($_REQUEST['spell'])) $type = "spell"; elseif (isset($_REQUEST['item'])) $type = "item"; elseif (isset($_REQUEST['achievement'])) $type = "achievement"; elseif (isset($_REQUEST['event'])) $type = "event"; else print file_get_contents("cache/unknown.jpg"); $id = intval($_REQUEST[$type]); // prevent injection function get_image($type, $item_id) { // check cache for image if (file_exists("cache/".$type."/".$item_id.".jpg")) return "cache/".$type."/".$item_id.".jpg"; // set locale $link = "".$type."=".$item_id; // download ini_set('user_agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'); $data = @file($link); if (!empty($data)) { $match = false; foreach($data as $line) { if (preg_match("/Icon.create(["'](.+)["'].*/", $line, $match)) $img = file_get_contents("".strtolower($match['1']).".jpg"); } } // no image found, return question mark if (!isset($img)) return "cache/unknown.jpg"; file_put_contents("cache/".$type."/".$item_id.".jpg", $img); chmod("cache/".$type."/".$item_id.".jpg", 0755); return "cache/".$type."/".$item_id.".jpg"; } print file_get_contents(get_image($type, $id)); ?>

I’ll be around for issues and questions. I remember posting this script some time ago, but this post is probably alot cleaner; short and sweet and to the point.