share
Meta Stack OverflowFor those that demand those elusive badges
[+133] [64] Ross
[2008-08-24 19:52:09]
[ discussion badges fun ]
[ http://meta.stackoverflow.com/questions/10299] [DELETED]

Updated

Back online, with most of the bugs fixed and an about and stats page [1].

  • File formats work properly
  • Better quality images
  • Cached images (for the hell of it)
  • Fixed width calculation

Well I've spent the previous half hour creating the next best thing.

May I present to you...

The Stack Overflow Badge Creator CS3

It's built in PHP, using the GD image library, and is accessible via a simple API [2].

Examples:

http://uvshock.co.uk/badges/badge.php?label=PHP%20Doctor

PHP Doctor

http://uvshock.co.uk/badges/badge.php?label=Hacker&medal=silver

The Hacker Badge Returns!

http://uvshock.co.uk/badges/badge.php?label=Send%20me%20the%20codez&medal=bronze

Send me the codez

If you're up for some light reading the source is dynamically available [3]. Please ignore my horrendously bad variable-width calculator.

Enjoy :)

(1) They all look gold! I don't know PHP but I think isset($_GET['medal']) should be !isset($_GET['medal']) - tpower
(4) The images are broken: meta.stackoverflow.com/questions/19072/elusive-badges-site-gone - Brad Gilbert
Haha, I just won a "Nice Answer" badge from this thread...2 years later... Oh, and I added round borders to the badge image script. - Kevin
[+57] [2008-09-04 15:02:37] community_owned

Image at uvshock.co.uk.

Image at uvshock.co.uk.


Hey fella! you nicked my badge! - Mitch Wheat
(28) Shouldn't it be "All your badge are belong to us"? - Graeme Perrow
(2) Take off every badge. For Great Justice. - jweede
1
[+43] [2008-08-24 22:59:21] Jon Galloway

I Disapprove Of Custom Badges


I Lolled at this. - community_owned
2
[+42] [2008-09-18 13:59:55] community_owned

Image at uvshock.co.uk.


(8) PC LOAD LETTER? What the f@#$ does that mean?! - R. Bemrose
It has a wikipedia article, so it really exists: en.wikipedia.org/wiki/PC_Load_Letter - community_owned
(1) TonJ, the reference was to this: en.wikipedia.org/wiki/Office_Space - community_owned
(1) I recognized the reference as soon as I saw it (the error, not the movie reference), thought it was the best one so far. - community_owned
(4) @TimB — Not necessarily? That error was around for a good 15-20 years before the movie was made. - Ben Blank
3
[+35] [2008-09-15 18:35:28] Jeff Davis

Badger, badger, badger, badger, badger, badger, badger, badger


(1) Now I have that song in my head again :( - tpower
(8) Mushroom, mushroom. - _Lasar
....badger,badger,badger,badger,badger,badger... - community_owned
(3) snaaaaaaaake! snaaaaaake! - mmc
lol never heard the song but i've heard about it alot... time to go find it... - community_owned
Badgers? We don't need not stinking badgers! - YWE
4
[+34] [2008-09-18 14:29:12] Juan Manuel

Answer to Life, the Universe, and Everything

Answer to Life, the Universe, and Everything [1]

[1] http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything

(4) Why is this silver? Shouldn't it be gold? - epochwolf
(8) @epochwolf - Maybe we were asking the wrong question? - community_owned
5
[+34] [2008-09-15 18:14:23] community_owned

http://uvshock.co.uk/badge.php?label=SARAH+PALIN+REWINDS+HER+DVDS


6
[+26] [2008-09-30 19:49:43] Ferruccio

Image at uvshock.co.uk.


7
[+21] [2008-09-18 13:52:38] community_owned

Doesn't Need Any Stinking Badges - image at uvshock.co.uk.


"don't need no"? - Michael
8
[+18] [2008-09-30 19:53:26] Firas Assaad

Image at uvshock.co.uk.


(1) Requirement: Downvote a new answer or question within 1 minute of it being posted. - Mark C
Related badge: Overkill: Flagged a post within 1 minute of it being posted. - Mark C
Scratch that, I finally saw the word I couldn't think of: Alarmist. - Mark C
9
[+18] [2008-08-24 22:46:45] Jeff Atwood

I can't find where it was anymore, but I remember Jeff not liking custom badges.

I didn't like a user editing other people's posts to add funny badges, but if you want to add them yourself, that's fine.


Nothing against Jeff (in fact, I admire him quite a bit), but with all due respect: Who cares what Jeff thinks about badges? Is this <i>our</i> website when it matches what Jeff thinks? - Esteban Araya
(22) Who's this Jeff Atwood Character, and why should we listen to him? ;) - Brad Gilbert
Indeed. Jeff frequently makes comments in the podcast about what S/O is "supposed" to be for. Thing is, the Vandals that show up really don't care. If we want to ask gratuitous sys admin questions, we're gonna! And badges for all :-) - community_owned
10
[+15] [2008-08-25 06:07:29] Kevin

alt text


Thanks for saving the script :) - Jeff Davis
11
[+12] [2008-10-04 06:51:39] Mark Cidade

Badge Whore x37

Badge Whore


12
[+11] [2008-08-24 19:54:40] Sören Kuklau

I Am Awesome

I approve.


13
[+11] [2009-04-22 13:09:44] Ólafur Waage

Now that the badges have rounded corners, this should be updated :)


(2) Badges still aren't rounded on IE7, but I guess that's a browser limitation. - mmyers
14
[+7] [2008-10-04 06:43:55] scronide

Doing It Wrong


I was just about to post this EXACT same badge - community_owned
15
[+7] [2009-04-22 13:08:39] macke

Seriously.


16
[+7] [2008-09-04 14:44:05] Stu Thompson

Brilliant!!! I've added this to my profile :D


A...how I miss those times :) I have the three hem.passagen.se/tiletech - community_owned
17
[+6] [2008-08-24 20:01:35] cmcculloh

Grumpy Beta Tester Badge

lol, I bet Jeff would love to give certain people this badge (probably me).

btw... cool script.


18
[+6] [2008-08-24 22:50:07] Kevin

I did this like the first week of SO.

alt text


19
[+6] [2008-09-30 20:19:34] community_owned

Image at uvshock.co.uk.


20
[+5] [2008-09-07 01:36:44] spoulson

I love this!

FAIL badge


21
[+5] [2008-09-18 13:56:12] Alison

http://uvshock.co.uk/badge.php?label=This%20is%20not%20a%20badge&medal=bronze


22
[+4] [2008-10-09 08:04:51] stukelly

Great script thanks.

Image at uvshock.co.uk.

Image at uvshock.co.uk.

Image at uvshock.co.uk.


23
[+4] [2008-10-12 11:52:48] Mitch Wheat

alt text


24
[+4] [2008-08-25 01:12:18] Chris Jester-Young

Re calculating string widths, there is a way to do it via the GD library. Have a play with my render gimmick [1], and play with the "Draw frame" and "Padding" settings. The font names are as provided in the corefonts [2] package, without the .ttf extension (e.g., georgiab).

Once you like what you see, have a look at the source code [3] to see how it's done. :-)

P.S. GD draws bullets just fine for me! Try it out on the render gimmick too. :-)

[1] http://gimmicks.hedgee.com/gimmick/render
[2] http://corefonts.sourceforge.net/
[3] http://gimmicks.hedgee.com/gimmick?pretty=render

25
[+4] [2009-04-22 12:47:34] yx

doom

sc


(1) +1 doom code... - hyperslug
26
[+3] [2009-07-18 05:42:24] community_owned

Image at uvshock.co.uk.


27
[+3] [2008-08-31 20:30:52] community_owned

Hi Ross :)

Regarding the font width calculations I think you should create a text bounding box and use the coordinates from that to determine how wide your image should be (imagettfbbox).

Imagettfbbox() doesn't require an initialized image resource before using it so it's perfectly for use.

Regarding the issue with bullets, you should convert special characters to unicode, GD supports unicode characters :)

Else it looks great!


28
[+2] [2008-09-09 11:48:09] spoulson

It looks like the filetype field isn't working. When I set png or gif, it still returns a jpeg type complete with the lossy artifacts, according to Firefox 3.

Compare: JPEG vs. PNG vs. GIF


It seems to show the correct thing to me. - community_owned
(2) Well, it HAS been over 7 months. He's since fixed it. - spoulson
29
[+2] [2009-04-22 12:57:37] community_owned

Image at uvshock.co.uk.


30
[+1] [2008-10-02 09:55:57] community_owned

I like the idea. The anti-aliasing is ugly, though.


31
[+1] [2009-06-17 16:04:48] Robert Cartaino

You know you're a StackOverflow addict when...

this is on the top of your resume...

Will Work for Badges


32
[+1] [2009-07-13 03:05:50] Oscar Reyes

alt text

Won't post a real answer but just google it for the questioner... and copy paste the first link.


You're missing an "M". ;) - gnostradamus
33
[+1] [2009-07-13 20:37:32] community_owned

alt text

I need this one a lot...


34
[+1] [2009-07-13 20:47:21] community_owned

alt text


35
[+1] [2009-07-16 00:57:51] Nick Presta

Awesome job!

I, too, wanted to play around with making badges so I whipped this up in a few minutes:

My badge

I made it with Python [1] and PIL [2].

Here is the source code (latest version here [3]):

#!/usr/bin/python           
# -*- coding: utf-8 -*-     

import Image
import ImageDraw
import ImageFont

FONTPATH = "/usr/share/fonts/truetype/msttcorefonts/arial.ttf"

class Badge:
    """ Models a stackoverflow reputation badge """

    # Class constants
    MEDALS = {"Gold": "gold",
              "Silver": "silver",
              "Bronze": "#CC9966"}
    FORMATS = {"JPG": ".jpg",
               "PNG": ".png"}
    MODE = "RGB"
    MEDAL_SIZE = 8
    X_PADDING = 18
    Y_PADDING = 7
    FONT_SIZE = 12

    def __init__(self, message=None, medal="Gold", format="PNG"):
        if message is None:
            self.message = "Uncaught StackOverflow Exception"
        else:
            self.message = message

        try:
            self.medal = self.MEDALS[medal]
        except KeyError:
            self.medal = self.MEDALS["Gold"]

        try:
            self.format = self.FORMATS[format]
        except KeyError:
            self.format = self.FORMATS["PNG"]

        self.font = ImageFont.truetype(FONTPATH, self.FONT_SIZE)
        self.textsize = self.font.getsize(self.message)

        total_width = self.textsize[0] + self.MEDAL_SIZE + (self.X_PADDING * 2)
        total_height = self.textsize[1] + (self.Y_PADDING * 2)
        self.image = Image.new(self.MODE, (total_width, total_height), 0x333333)
        self.draw = ImageDraw.Draw(self.image)

    def draw_medal(self):
        top_left_x = ((self.X_PADDING * 2) - self.MEDAL_SIZE) / 2
        top_left_y = ((self.Y_PADDING * 2 + self.textsize[1])- self.MEDAL_SIZE) / 2
        bot_right_x = top_left_x + self.MEDAL_SIZE
        bot_right_y = top_left_y + self.MEDAL_SIZE

        self.draw.ellipse((top_left_x,
                           top_left_y,
                           bot_right_x,
                           bot_right_y),
                           self.medal)

    def draw_message(self):
        left = self.MEDAL_SIZE + (self.X_PADDING * 1.5)
        top = self.Y_PADDING
        self.draw.text((left, top), self.message, font=self.font)

    def create(self):
        self.draw_medal()
        self.draw_message()
        self.save()

    def save(self):
        self.image.save("out" + self.format)

if __name__ == '__main__':
    badge = Badge()
    badge.create()
[1] http://python.org/
[2] http://www.pythonware.com/products/pail/
[3] http://git.nickpresta.ca/cgit.cgi/git/tree/python/badge.py

36
[+1] [2009-07-18 05:36:37] Mark

Not to mention the corners aren't rounded... (you've been using IE haven't you??! Shame on you!)


This was built a long long time before rounded corners :) Also you can't use styling on stackoverflow. I'll be upgrading the code soon as uvshock.co.uk is expiring. - Ross
Oh. Thought this was meant for use on your own site... not SO specifically. Okay then - Mark
37
[+1] [2009-07-28 13:59:01] Drake

alt text


38
[+1] [2008-08-25 11:32:18] Ross

Did anyone else notice that someone got the first gold badge?

Aye, I was quite impressed by that - congrats Justin [1].


@ Chris [2]

Re calculating string widths, there is a way to do it via the GD library. Have a play with my render gimmick, and play with the "Draw frame" and "Padding" settings. The font names are as provided in the corefonts package, without the .ttf extension (e.g., georgiab).

Once you like what you see, have a look at the source code to see how it's done. :-)

P.S. GD draws bullets just fine for me! Try it out on the render gimmick too. :-)

Bah, I completely forgot about FreeType - I'll make some additions now :)

[1] http://beta.stackoverflow.com/badges/22/great-question
[2] #25531

39
[+1] [2008-09-18 14:05:33] Juan Manuel

StackOverflow Addicted

More than 10.000 views per hour


you might get banned before you get the badge ;) - community_owned
40
[0] [2008-09-18 14:08:51] user144147

alt text


41
[0] [2008-09-18 14:11:37] Rob Z

Here's one that I'm sure we can all appreciate -

alt text

I'm not sure what this one means -

alt text

Finally, for when you need a little pick-me-up -

alt text


42
[0] [2008-10-13 22:23:24] community_owned

is there a bug? setting medal to silver doent work if the label string has a space in it.


43
[0] [2008-09-30 20:06:05] user151927


44
[0] [2008-09-22 17:12:05] community_owned

Nice work! This isnt so much a useful app in my opinion but I definitely think it is neat and a little pick-me-up for when I slow down in coding! Thanks!


45
[0] [2008-08-24 22:55:26] Ryan Fox

Do you strip enough out to prevent HTML badges?

Let's see.

● Yearling

Edit: I guess so...


46
[0] [2008-08-24 20:05:11] Ross

There's already been some trouble caused by someone else doing this exact same thing.

Trouble? How and where?


47
[0] [2009-07-28 14:40:34] gclaghorn

alt text


48
[0] [2009-07-13 20:39:12] Canavar

No, I will not do your homework


49
[0] [2009-07-10 20:06:01] Ben Griswold

alt text


50
[0] [2009-04-22 13:27:08] community_owned

alt text


51
[0] [2008-10-02 14:42:51] Humpton


52
[0] [2008-11-05 02:20:48] BoltBait

I'm not telling you how I earned these...

Problem Exists Between Chair And Keyboard x5

Idiot x2


53
[0] [2009-04-22 12:42:08] Jon Winstanley

alt text x5


54
[-1] [2009-07-13 02:52:02] community_owned

alt text


55
[-1] [2009-07-13 20:43:01] community_owned

alt text


56
[-1] [2009-07-28 11:26:08] scunliffe

Oh pretty please! ;-)

alt text x439


57
[-1] [2008-08-24 20:16:43] Ryan Fox

I can't find where it was anymore, but I remember Jeff not liking custom badges.


58
[-1] [2008-08-24 20:03:16] Ryan Fox

There's already been some trouble caused by someone else doing this exact same thing.

Also, since it's not HTML, it doesn't take any custom CSS changes into account.


59
[-1] [2008-08-25 02:38:44] community_owned

Did anyone else notice that someone [1] got the first gold badge [2]?

[1] http://beta.stackoverflow.com/users/92/justin-standard
[2] http://beta.stackoverflow.com/badges/22/great-question

60
[-1] [2008-09-04 14:30:24] community_owned

Cool nice work and thanks for sharing the source.


61
[-1] [2008-09-25 03:50:07] davenpcj

Hmmmmm... There's a CSS class to do that.

● CSS User

The codez:

<span class="badge"><span class="badge1">●</span>&nbsp;CSS User</div>

AWWW. Stripped the span tag! - davenpcj
62
[-1] [2008-11-05 02:14:31] DrStalker

alt text


63
[-1] [2008-09-18 14:39:08] community_owned

● How about HTML Badges?

Hmm, looks great in the preview, but doesn't seem to work...


Yeah, the spans/divs get stripped. - davenpcj
64