TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   A form of a image class.. (http://www.talkphp.com/advanced-php-programming/2045-form-image-class.html)

Tanax 01-22-2008 07:45 AM

A form of a image class..
 
Hey!

I'm having a problem..
I wanna code a class, that would let me do something like this:

php Code:
$image = new TibiaChar('Mortis Dominus');
if($image) {
      echo $image->getImageCode();
}
else {
      echo 'Character does not exist!';
}

And that would search Tibia - Free Multiplayer Online Role Playing Game - News character page for Mortis Dominus, and if it exists, it would do some fetching of the data on that character page, and create an image for it.
Character page is: Tibia - Free Multiplayer Online Role Playing Game - Community

Then it would echo out a image code, such as "www.domain.com/images/mortis_dominus.png" so I can use it as a dynamic image.

How would I achieve this?
I know I have to use file_get_contents, and also use the PHP GD library..

I have an example here:
Tibia - Character Image

sketchMedia 01-22-2008 09:46 AM

do we have a form of api for this? ie how do you get the data for the character?

Tanax 01-22-2008 11:16 AM

with file_get_contents and using preg_match on the resulted page IF the character existed.

Tibia - Free Multiplayer Online Role Playing Game - Community

As you see on the link, if you click it, you come to the page with the data of my char.. if you write a char that doesn't exist instead of "Mortis%20Dominus", you won't get that page..

Wildhoney 01-22-2008 01:24 PM

1 Attachment(s)
Wrote you a class for this because I'm feeling kind today ;-) ! Don't forget to mention TalkPHP wherever!

php Code:
<?php

    class TalkPHP_TibiaChar
    {
        private $m_aData;
        private $m_bFound;
       
        const TIBIA_ADDRESS = 'http://www.tibia.com/community/?subtopic=characters&name=%s';
       
        public function find($szCharacter)
        {
            $szCharacter = urlencode($szCharacter);
            $szContent = file_get_contents(sprintf(self::TIBIA_ADDRESS, $szCharacter));
           
            if(empty($szContent))
            {
                throw new Exception('Tibia content is not available');
            }
           
            preg_match_all('~<td[\swidth=20%]*>([^<]+):</td><td>([^<]+)</td>~i', $szContent, $aMatches);
           
            for($iIndex = 0; $iIndex < count($aMatches[1]); $iIndex++)
            {
                $szKey = preg_replace('~[^a-z]+~i', '', ucwords($aMatches[1][$iIndex]));
                $szValue = ucwords(html_entity_decode($aMatches[2][$iIndex]));
               
                $this->m_aData[$szKey] = $szValue;
            }
        }
       
        public function __call($szCall, $aArgs)
        {
            if(preg_match('~get(?P<key>.+)~i', $szCall, $aMatches))
            {
                if(!array_key_exists($aMatches['key'], $this->m_aData))
                {
                    return false;
                }
               
                return $this->m_aData[$aMatches['key']];
            }
        }
       
        public function isCharacter()
        {
            if(count($this->m_aData) > 0)
            {
                return true;
            }
           
            return false;
        }
    }
   
    $pTibia = new TalkPHP_TibiaChar();
    $pTibia->find('Mortis Dominus');
   
    if($pTibia->isCharacter())
    {
        echo 'Name: ' . $pTibia->getName();
        echo '<br />';
        echo 'Sex: ' . $pTibia->getSex();
        echo '<br />';
        echo 'Profession: ' . $pTibia->getProfession();
        echo '<br />';
        echo 'Level: ' . $pTibia->getLevel();
        echo '<br />';
        echo 'World: ' . $pTibia->getWorld();
        echo '<br />';
        echo 'Residence: ' . $pTibia->getResidence();
        echo '<br />';
        echo 'Last Login: ' . $pTibia->getLastLogin();
        echo '<br />';
        echo 'Account Status: ' . $pTibia->getAccountStatus();
    }
    else
    {
        die('Character does not exist.');
    }

?>

sketchMedia 01-22-2008 02:58 PM

PHP Code:

for($iIndex 0$iIndex count($aMatches[1]); $iIndex++) 

NOOOOOOOOOOOOOOOOOoooooooooooooooooOOOOOOOOOOOOOoo ooo

PHP Code:

$iNumMatches count($aMatches[1]);
for(
$iIndex 0$iIndex $iNumMatches$iIndex++) 

being picky abit there, im just allergic to count() being used there.

sjaq 01-22-2008 03:12 PM

lol @ sketchmedia
You can even do it in one line:
PHP Code:

for($iIndex 0$iNumMatches count($aMatches[1]); $iIndex $iNumMatches$iIndex++) 

;-)

For another speed boost you should use curl instead of file_get_contents

Village Idiot 01-22-2008 03:13 PM

Quote:

Originally Posted by sketchMedia (Post 9260)
PHP Code:

for($iIndex 0$iIndex count($aMatches[1]); $iIndex++) 

NOOOOOOOOOOOOOOOOOoooooooooooooooooOOOOOOOOOOOOOoo ooo

PHP Code:

$iNumMatches count($aMatches[1]);
for(
$iIndex 0$iIndex $iNumMatches$iIndex++) 

being picky abit there, im just allergic to count() being used there.

You should be because putting it in the for loop executed count each time.

sketchMedia 01-22-2008 03:26 PM

which was precisely why i objected to it in the first place

Tanax 01-22-2008 06:12 PM

Thanks!!!

I got a question though about your code there..
I suck at those.. preg_replace and that..

And I know I should read about it in some tutorial.. but I just don't.. understand it :(

So could someone please explain this to me:
PHP Code:

preg_match_all('~<td[\swidth=20%]*>([^<]+):</td><td>([^<]+)</td>~i'$szContent$aMatches); 

Also, how would I make an image out of it?
If you go to this site:
Tibia - Character Image

And enter "Mortis Dominus" as character.. you'll get an image.
Also, that image is updated every 10 minutes, how would I achieve that?

Thanks alot!

Tanax 01-22-2008 06:39 PM

Oh, and also, how would I get it to include the comments of a player?

Tibia - Free Multiplayer Online Role Playing Game - Community

That character has a comment, so how would I include it in the preg_match_all statement??

Tanax 01-23-2008 07:21 PM

Anyone? xD


All times are GMT. The time now is 08:21 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0