Simple ImageShack upload using C#

Filed Under (c#) by The Chef on 08-06-2009

Tagged Under : , , ,

First of all this class is based on the http helper class. The rest is trivial. Here is a piece of code

    public static class ImageShack
    {
        private static http http = new http();

        public static String UploadImage(String filename)
        {
            http.PostMode = PostMode.Multipart;
            http.AddPostKey("xml", "yes");
            http.AddPostFile("fileupload", filename);
            return http.GetUrl("http://www.imageshack.us/index.php");
        }
    }

The result is an xml. You can deserialize it using the XML serializer/deserializer from the previous post.

    [Serializable]
    public class links
    {
        public String image_link;
        public String thumb_link;
        public String ad_link;
        public String thumb_exists;
        public int total_raters;
        public float ave_rating;
        public String server;
        public String image_name;
        public String done_page;
        public String resolution;
        public int filesize;
        public String image_class;
    }

.
.
.
imageshark isr=new imageshark();
links lnk = new links();
lnk = XMLData.ObjectXMLSerializer<links>.LoadString(ImageShack.UploadImage("picture.png"));
Console.WriteLine(lnk.image_link);

Hope it helps. Enjoy

PHP: Simple upload image url to ImageShack

Filed Under (php) by The Chef on 26-04-2009

Tagged Under : , , ,

Here is a function you can use to directly upload url images to ImageShack. The function returns an associative array with 2 elements representing the ImageShack url of the image and of the thumbnail


function uploadURLToImageshack($url)
{
$ch = curl_init ( "http://www.imageshack.us/transload.php" );

$post ['xml'] = 'yes';
$post ['url'] = $url;

curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Expect: ' ) );

$result = curl_exec ( $ch );
curl_close ( $ch );

if (strpos ( $result, '<' . '?xml version="1.0" encoding="iso-8859-1"?>' ) === false)
{
return NULL;
} else
{
$xml = new SimpleXMLElement ( $result );
return array (
"image" => $xml->image_link,
"thumb" => $xml->thumb_link );
}
}