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 wowhead.com, second wotlk.openwow.com. 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)

http://www.linuxlounge.net/~martin/wowheadimages/?event=341

http://www.linuxlounge.net/~martin/wowimages/?event=181

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

http://www.linuxlounge.net/~martin/wowheadimages/?spell=70541

http://www.linuxlounge.net/~martin/openwowimages/?spell=70541

Spell=70541

http://www.linuxlounge.net/~martin/wowheadimages/?item=51939

http://www.linuxlounge.net/~martin/openwowimages/?item=51939

Item=50225

http://www.linuxlounge.net/~martin/wowheadimages/?achievement=4619

http://www.linuxlounge.net/~martin/openwowimages/?achievement=4619

Achievement=4619

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

$link = “http://www.wowhead.com/“.$type.”=”.$item_id;
or

$link = "http://wotlk.openwow.com/".$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("http://wow.zamimg.com/images/wow/icons/large/".strtolower($match['1']).".jpg");
or

$img = file_get_contents("http://cdn.openwow.com/images/icons/large/".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.

[ul][li]index.php[/li][li]cache/achievement/[/li][li]event/[/li][li]item/[/li][li]spell/[/li][li]unknown.jpg[/li][/ul]

<?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 = "http://wotlk.openwow.com/".$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("http://cdn.openwow.com/images/icons/large/".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.

hexa-

niceeee