Dream.In.Code Dynamic Signature! (Using New XML API)

A dynamic signature for DiC!

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 20849 Views - Last Post: 04 July 2010 - 04:23 PM

#1 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Dream.In.Code Dynamic Signature! (Using New XML API)

Post icon  Posted 28 May 2010 - 10:52 AM

*
POPULAR

Hello everybody!

skyhawk suggested this dynamic signature using some XML thing he just added to the forums that allows almost every page to be turned into XML, including the profile pages...

Anyway, I created this...I know it's incredibly dry, but I can't help it...I'm not exactly a graphics artist :P So I'm looking for ideas on improvements! Tell me what you think!

Here's an example:
Posted Image
http://tibialottery.com/dic/217966.png is the image URL, the number is dynamic.

So incase you don't understand, here's Skyhawk's:
Posted Image
http://tibialottery.com/dic/34.png

(If either of them say there was an error it just means it didn't parse the XML correctly and you should probably reload the page.)

Anyway, tell me what you think!

OH! RIGHT! And you find the profile ID in the URL when you click on your name in the forums...

<Open-source-style>
<?php
date_default_timezone_set('America/Chicago');
class dicImager{
    private $userID;
    private $link = "http://www.dreamincode.net/forums/xml.php?showuser=%s";
    private $userInformation = array();
    private $ranks = array("Webmaster" => array("r" => 150, "g" => 20, "b" => 48), "Alumni" => array("r" => 239, "g" => 122, "b" => 200), "Mentor" => array("r" => 0, "g" => 183, "b" => 172), "Expert" => array("r" => 127, "g" => 73, "b" => 125), "Author" => array("r" => 164, "g" => 156, "b" => 110), "Contributor" => array("r" => 165, "g" => 108, "b" => 89), "Default" => array("r" => 0, "g" => 0, "b" => 0), "Admin" => array("r" => 39, "g" => 123, "b" => 89), "Moderator" => array("r" => 0, "g" => 0, "b" => 250));
    private $height;
    private $width;
    
    public function __construct($id, $height = 100, $width = 330){
        $this->userID = $id;
        $this->setHeight($height);
        $this->setWidth($width);
    }
    
    public function setHeight($height){
        if(is_integer($height) && $height > 0){
            $this->height = $height;
        }else{
            $this->height = 100;
        }
    }
    
    public function setWidth($width){
        if(is_integer($width) && $width > 0){
            $this->width = $width;
        }else{
            $this->width = 330;
        }
    }
    
    public function generateImage(){
        Header('Content-type: image/png');
        $image = imagecreatetruecolor($this->width, $this->height);
        $black = imagecolorallocate($image, 0, 0, 0);
        $white = imagecolorallocate($image, 255, 255, 255);
        imagefilledrectangle($image, 1, 1, ($this->width-2), ($this->height-2), $white);
        if(!$this->parseXML()){
            imagestring($image, 5, 0, 50, "There was an error, sorry.", $black);
            imagepng($image, null, 9);
        }else{
            $rank = $this->safelyGetRankColors();
            $rank = imagecolorallocate($image, $rank['r'], $rank['g'], $rank['b']);
            $dist_size = $this->getSafeSizes();
            imagecopyresized($image, $this->getImageResource(), 1, 1, 0, 0, 100, 98, $dist_size['width'], $dist_size['height']);
            imagestring($image, 3, 110, 5, "Name:", $black);
            imagestring($image, 3, 148, 5, $this->userInformation['name'], $rank);
            imagestring($image, 3, 110, 20, "Posts: ".$this->userInformation['posts'], $black);
            imagestring($image, 3, 110, 35, "Reputation: ".$this->userInformation['rep'], $black);
            imagestring($image, 3, 110, 50, "Gender: ".$this->userInformation['gender'], $black);
            imagestring($image, 3, 110, 65, "Date joined: ".$this->userInformation['joined'], $black);
            imagestring($image, 3, 110, 80, "Group: ", $black);
            imagestring($image, 3, 155, 80, $this->userInformation['group'], $rank);
            imagepng($image, NULL, 9);
            imagedestroy($image);
        }
    }
    
    private function getSafeSizes(){
        $sizes = getimagesize($this->userInformation['image']);
        $sizes = array("width" => $sizes[0], "height" => $sizes[1]);
        return $sizes;
    }
    
    private function getImageResource(){
        $userImage = $this->userInformation['image'];
        $format = explode(".", $userImage);
        $format = $format[count($format)-1];
        if($format == "jpg" || $format == "jpeg"){
            return imagecreatefromjpeg($userImage);
        }else if($format == "png"){
            return imagecreatefrompng($userImage);
        }else if($format == "bmp"){
            return imagecreatefromwbmp($userImage);
        }else{
            die("Error. Unknown file type. FORMAT: ".$format);
        }
    }
    
    private function safelyGetRankColors(){        
        if(stristr($this->userInformation['group'], " w/DIC++") !== false){
            $group = explode(" w/DIC++", $this->userInformation['group']);
            $group = $group[0];
        }else{
            $group = $this->userInformation['group'];
        }
        if(strtolower(substr($group, -1)) == "s"){
            $group = substr($group, 0, -1);
        }

        if(isset($this->ranks[$group])){
            return $this->ranks[$group];
        }else{
            $this->ranks['Default'];
        }
    }
    
    private function parseXML(){
        libxml_use_internal_errors(true);
        $filename = sprintf($this->link, $this->userID);
        if(!$xml = simplexml_load_file($filename)){
            for($i = 0; $i < 5 && $xml == false; $i++){
                $xml = simplexml_load_file($filename); //Retry 5 tmes before giving up
            }
            if(!$xml){
                return false;
            }
        }
        libxml_use_internal_errors(false);
        $info = $xml->xpath('profile');
        $this->userInformation['name'] = (string) $info[0]->name;
        $this->userInformation['gender'] = (string) $info[0]->gender->gender->value;
        $this->userInformation['rep'] = (string) $info[0]->reputation;
        $this->userInformation['posts'] = (string) $info[0]->posts;
        $this->userInformation['joined'] = date('F jS, Y', strtotime((string) $info[0]->joined));
        $this->userInformation['group'] = (string) $info[0]->group->span;
        $this->userInformation['image'] = (string) $info[0]->photo;
        return true;
    }
}

function fixArrayReturnInteger($array, $key){
    if(isset($array[$key])){
        return (int) $array[$key];
    }else{
        return 0;
    }
}

if(isset($_GET['id'])){
    $width = fixArrayReturnInteger($_GET, "width");
    $height = fixArrayReturnInteger($_GET, "height");
    $dic = new dicImager($_GET['id'], $height, $width);
    $dic->generateImage();
}else{
    print "You need to add an ID to it! Like so:<br />\n<a href=\"http://tibialottery.com/dic/34.png\">http://tibialottery.com/dic/34.png</a><br />\nThe ID goes where the 34 is!";
}

?>

Last updated: May 29th, 2010 (10:41 PM Central US/Canada Time)
-- Made it retry 5 times before giving up...
-- Added Moderators, made it remove "s" from end of groups because of inconsistency
-- Made mentors "more blue" :P
-- Let images be resized via built-in parameters, like so:
Posted Image
http://tibialottery..../34h110w500.png
The above sets the image height to 110 and width to 500. (I became too addicted to the GD library, please stop me...)

Have suggestions/ideas for the code? Tell me about them too!

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2010 - 09:02 AM


Is This A Good Question/Topic? 17
  • +

Replies To: Dream.In.Code Dynamic Signature! (Using New XML API)

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:02 AM

Nice work Shane!

For those that are interested, the XML for user profiles is located here:

http://www.dreaminco...php?showuser=34

We'll have some upcoming contests/challenges that involve using the XML from DIC to create things like this, so start getting ideas together.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:15 AM

Awesome


time to make an app
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:25 AM

The source was added to the first post :)

Yours,
Shane~

This post has been edited by ShaneK: 28 May 2010 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 X@MPP  Icon User is offline

  • 僕わ馬鹿ですね?
  • member icon

Reputation: 36
  • View blog
  • Posts: 1,014
  • Joined: 20-February 09

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:39 AM

Nice work! kinda reminds me of what i wanted to do :/ but anyway keep up the good work ^^
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:54 AM

View PostX@MPP, on 28 May 2010 - 10:39 AM, said:

Nice work! kinda reminds me of what i wanted to do :/ but anyway keep up the good work ^^


Plenty of room for improvement. I just picked someone to get it started, you're welcome to tweak his code and add functionality.

Or use the API to create a "friends" matrix that shows all your friends, or something else like that!
Was This Post Helpful? 0
  • +
  • -

#7 X@MPP  Icon User is offline

  • 僕わ馬鹿ですね?
  • member icon

Reputation: 36
  • View blog
  • Posts: 1,014
  • Joined: 20-February 09

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 11:56 AM

Thanks! ill try to whip something up soon and/or tweak the existing code a bit ^^
Was This Post Helpful? 0
  • +
  • -

#8 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 01:15 PM

Is it possible to reduce the width? I'd like to put this on my blog, and the width is just too big to be placed in the sidebar.
Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 28 May 2010 - 04:12 PM

View PostCore, on 28 May 2010 - 01:15 PM, said:

Is it possible to reduce the width? I'd like to put this on my blog, and the width is just too big to be placed in the sidebar.


Done! Sorry it took so long, was at work :P
Also, the only reason it was ever so big was because I didn't know how much room I'd need :P

Yours,
Shane~

This post has been edited by ShaneK: 28 May 2010 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4306
  • View blog
  • Posts: 12,079
  • Joined: 18-April 07

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 10:13 AM

Nice work for sure!

Hopefully Chris will extend this into a full API where application writers can virtually get all sorts of information about DIC. From thread/snippet/tutorial counts to actual threads that list posts. From search capabilities to site wide statistics of all kinds. I smell web services here.

:)
Was This Post Helpful? 2
  • +
  • -

#11 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 10:22 AM

Awesome work! Can it be modified to get the actual avatar instead of the profile photo? Also, what about badges? :)
Was This Post Helpful? 0
  • +
  • -

#12 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 12:19 PM

What a great idea. This could really be useful and fun to work with!

@Core, ya you can just take a look at any showuser=## it will have everything you want about that user

This post has been edited by SwiftStriker00: 29 May 2010 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 12:24 PM

It doesn't colour my group blue. See Posted Image
Was This Post Helpful? 0
  • +
  • -

#14 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 12:39 PM

cause yours says "Moderators" when shouldn't it be "Moderator"?
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Re: Dream.In.Code Dynamic Signature! (Using New XML API)

Posted 29 May 2010 - 12:55 PM

It is the group, a group generally implies more than one. So I'm part of the Moderators group
See for Core his is Admins, cos he an AssLicker :winkiss: sorry an Admin
Posted Image
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2