Elliott C. Back: Internet & Technology

Using the ImageShack XML API

Posted in Code, Graphics by Elliott Back on June 1st, 2008.

If you wanted to start a free photo uploading site, but didn’t want to pay the fixed storage and bandwidth costs of Amazon’s S3 or another CDN service, you might be interested in the free ImageShack API. Currently, it allows you to upload a photo to the ImageShack service, and get back the image’s size, a photo URL, and a thumbnail URL. You can access their API by using a simple ImageShack php class:

<?php
class ImageShack
{
    var $is_url = "http://www.imageshack.us/index.php";
    var $is_result = false;
    var $is_result_parsed = false;

    public function upload( $file )
    {
        // send the image to ImageShack
        $ch = curl_init($this->is_url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 240);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array( ‘xml’=>‘yes’, ‘fileupload’=>‘@’.$file ));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( ‘Expect: ’ ));
        $this->is_result = curl_exec($ch);
        curl_close($ch);

        // Parse the result
        $this->parse();
    }

    public function get_image_url()
    {
        return $this->get( "image_link" );
    }

    public function get_thumb_url()
    {
        return $this->get( "thumb_link" );
    }

    public function get_done_page()
    {
        return $this->get( "done_page" );
    }

    public function get_resolution()
    {
        return $this->get( "resolution" );
    }

    public function get_size()
    {
        return $this->get( "filesize" );
    }

    private function get( $key )
    {
        if( !$this->is_result_parsed )
            return false;

        return( $this->is_result_parsed[ $key ] );
    }

    private function parse()
    {
        if (strpos($this->is_result, '<'.'?xml version=”1.0″ encoding="iso-8859-1″?>') === false)
            $this->is_result_parsed = false;

        $xmlData = explode("\n",$this->is_result);
        $xmlr = array();

        foreach($xmlData as $xmlDatum){
            $xmlDatum = trim($xmlDatum);

            if($xmlDatum != "" && !eregi("links",$xmlDatum) && !eregi("xml",$xmlDatum)){
                $xmlDatum = str_replace(">","<",$xmlDatum);
                list($xmlNull,$xmlName,$xmlValue) = explode("<",$xmlDatum);
                $xmlr[$xmlName] = $xmlValue;
            }
        }

        $this->is_result_parsed = $xmlr;
    }
}
?>

Accepting file uploads in PHP is almost trivial. You just need a form on a web page somewhere with multipart encoding, such as:

<form enctype="multipart/form-data" id="uploadform" method="post" action="http://example.com/path/to/upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input id="anchor_file" type="file" name="files[]" />
<input type="submit" value="" id="submitimages" name="submitimages" />
</form>

On the server side, you need to iterate through the special $_FILES array in PHP:

<?php
for($i = 1; $i < count($_FILES['files']['name']); $i++){
    $error = $_FILES['files']['error'][$i];
    if($error !== 0) break;

    $data = array(
        'name' => $_FILES['files']['name'][$i],
        'temp' => $_FILES['files']['tmp_name'][$i],
        'size' => $_FILES['files']['size'][$i]
    );

    // do something with $data now, like upload it to ImageShack
}
?>

With these bits, it’s easy to write a wraparound interface to ImageShack. In the future, I hope their API also includes being able to query for photos, comments, and ratings after uploading; at the moment I see now way to do that.

This entry was posted on Sunday, June 1st, 2008 at 7:54 pm and is tagged with . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

9 Responses to “Using the ImageShack XML API”

  1. Mikael says:

    Class can be used without a key?
    What about the photos then? How long will they exist? can you control them in any way?

  2. aldo says:

    can you give me example how to use your code? thanks

  3. Colin says:

    Same here. I keep going thought it and can not figure it out.

    Does the file need to be uploaded to my server first?

    I’ve tried the method on the ImageShack API documentation and it works. However, it does not return any information.

  4. sinx says:

    Nice, maybe when i will have some time i will rewrte this to Perl, btw: there is an OpenSource ImageShack app to upload pictures :) .

  5. Hi,
    I have been experimenting with the imageshack redirect API. Its a little easier to get to grips with than this example, check it out:

    http://www.blocsoft.com/blog/imageshack.asp

  6. mark says:

    Hi elliott,
    i was just wondering if you have any more detailed example code or anywhere i can get a bit more help on this. as i tried to do it but it just keeps failing and the image is never uploaded any help is apprechiated.

    if it is easier to email me rather than respond on here my email is
    mark [at] playgen [dot] com

  7. Bryce Thomas says:

    Hi Elliot, nice post. I recently took a dig into the ImageShack API myself, but using C# instead of php given that I’ve got very little experience with the latter. I’ve written a blog post about it at http://www.codeemporium.com/2009/06/14/dot-net-c-sharp-wrapper-for-the-imageshack-xml-api/ where I’ve explained some of the quirks of the API as well which are useful to know about from a user’s perspective. There’s also working source code and examples to download. I’ve only just started the blog (it’s my first post) – let me know what you think.

    Cheers, Bryce.

  8. Unreal Media says:

    Great script there. But it does seem like alot just to upload an image. APIs never seem to be simple :(

  9. Sid says:

    I would love to do it – but I can’t figure out how to make it work – Someone help!

    My email is – sid1 [at] gmx [dot] com

Leave a Reply

Powered by WP Hashcash