The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WWW::Imgur - upload images to imgur.com

SYNOPSIS

    my $imgur = WWW::Imgur->new ();
    $imgur->key ('YoUrApIkEy');
    # Put an image on to the web site
    my $json = $imgur->upload ('fabulous.png')
        or die "Upload failed";
    # Delete an image
    $imgur->delete ('DelETEhasH');
        or die "Delete failed";

WWW::Imgur provides an interface to the image uploading and image deletion APIs of the http://imgur.com/ image sharing website.

METHODS

new

    my $imgur = WWW::Imgur->new ({key => 'YoUrApIkEy',
                                  verbose => 1});

Create a new object.

key

    $imgur->key ('MyApiKEy');

Set the API key. You can get an API key at http://imgur.com/register/api_anon for an anonymous application, or http://imgur.com/register/api_oauth for a registered application.

verbosity

    # Turn on messages
    $imgur->verbosity (1);
    # Turn off messages
    $imgur->verbosity ();

Give a true value to get messages from the object telling you what it is doing. Give a false or empty value to stop the messages.

upload

    $json = $imgur->upload ('fabulous.png');
    $json = $imgur->upload ('http://www.example.com/fabulous.png');

Upload an image to imgur.com. If it succeeds, it returns the JSON message from imgur.com as plain text (it does not parse this message into a Perl object). If it fails, it prints an error message on the standard error and returns an undefined value.

If you want to view the contents of $json, try, for example

    use JSON;
    use Data::Dumper;
    my $json = $imgur->upload ('nuts.png');
    print Dumper (json_decode ($json));

See examples/upload.pl for a full example.

There is another argument to upload where you can add more options. Currently there are two options, "caption" and "title".

    my $json = $imgur->upload ('sharon-stone.jpeg',
                           {
                               caption => 'Sharon Stone stoned again',
                               title => 'Sharon Stone gathers no moss',
                           });

However, although the values you send are sent back in the JSON response you get from the site, these don't appear to do anything.

delete

     $imgur->delete ('ImageDeleteHASH');

Delete an image from imgur.com. You need a key called the "deletehash", which is one of the parts of the JSON response from the "upload" method.

If you try to delete an image which has already been deleted, it seems to respond with a "400 Bad Request" error.

agent

     $imgur->agent ('MyScript.pl');

Set the user agent string of the agent which makes the request. The default value of the user agent string is "WWW::Imgur".

SEE ALSO

Image::Imgur is an alternative upload module for imgur.com.

AUTHOR

Ben Bullock, <bkb@cpan.org>

LICENCE

You can copy, modify and redistribute WWW::Imgur under the Perl Artistic Licence or the GNU General Public Licence.