getting remote image size with Image::Size & LWP

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default getting remote image size with Image::Size & LWP

    
    Thank you for your patience and help bringing me up to speed on how to
    be a better user of this list. My previous post was my first.

    I am trying to write a script that will use Image::Size module in
    conjunction with the LWP module to retrieve the height and width
    attribute of a gif or jpg on a remote server. Below the first script
    uses the Image::Size module and successfully returns the height and
    width of an image on the same volume. The second script uses the LWP
    module and depending on which version of $the_url is used, successfully
    returns the contents of an html file or an image file. When the image
    file is read it prints the "raw data" of the image file. Where I am at
    a loss is how to combine these so that I can return the height & width
    attributes of a remote file. Any suggestions on where I might start or
    what I might try? Please bare with me while I learn how to use the list
    properly. TIA

    ################################################## #####

    #!/usr/bin/perl -w
    use strict;
    use Image::Size;

    my $image = '/Users/mgalaher/Pictures/malden_house.jpg';

    my ($globe_x, $globe_y) = imgsize($image);

    print $globe_x . " " . $globe_y;

    # This successfull prints the image dimensions as: 700 578

    ################################################## ####

    #!/usr/bin/perl -w
    use strict;
    # to read the files via http that each url points to.
    my $file = "";
    my $the_url = 'http://homepage.mac.com/galaher/images/maul.jpg';

    # Since printing an image just shows a huge string, test with the
    following as well
    # which shows an html page:
    #my $the_url = 'http://homepage.mac.com/galaher/index.html';

    use LWP::UserAgent; # This will cover all of them!
    use URI::URL;
    use HTTP::Request;

    my $hdrs = new HTTP::Headers(Accept => 'text/plain', UserAgent =>
    'MegaBrowser/1.0');

    my $url = new URI::URL($the_url);
    my $req = new HTTP::Request('GET', $url, $hdrs);
    my $ua = new LWP::UserAgent;
    my $resp = $ua->request($req);

    if ($resp->is_success) {

    # If connection is successful the contents of the file
    # read will now go into the variable $file
    $file = $resp->content;

    }
    else {

    # If connection is not successful then make note of this
    print $resp->message;
    #$file = "socket_failure";
    }

    print $file;

    ################################################## ####

    Matthew Galaher Guest

  2. Similar Questions and Discussions

    1. How to find size (in bytes) of an image using the GD image resource?
      I would like to be able to determine the size (in bytes) of a file and re-adjust before output. Is this possible? Norm -- Avatar hosting at...
    2. getting remote image sizes with image::size lwp
      I am trying to get the width and height attributes of jpg's and gif's on a remote server. I have found Image::Size and LWP but am unable to put...
    3. Dragging Image onto canvas changes image size
      when I create a new canvas say 8.5x11, then try to drag a 4x6 size image onto it, it is not 4x6 on the canvas. it is smaller, is there a way to make...
    4. New Image Size?
      I think it's algorithm is: if there is an image on the clipboard image size = big enough for it else image size is the last value you set it to...
  3. #2

    Default Re: getting remote image size with Image::Size & LWP

    I got this working. I found what I was looking for at cpan's site.

    [url]http://search.cpan.org/dist/Image-Size/[/url]

    works:

    #!/usr/bin/perl -w
    use strict;
    # to read the files via http that each url points to.
    my $img = "";
    my $the_url = 'http://homepage.mac.com/galaher/images/maul.jpg';

    # Since printing an image just shows a huge string, test with the
    following as well
    # which shows an html page:
    #my $the_url = 'http://homepage.mac.com/galaher/index.html';

    use LWP::UserAgent; # This will cover all of them!
    use URI::URL;
    use HTTP::Request;

    my $hdrs = new HTTP::Headers(Accept => 'text/plain', UserAgent =>
    'MegaBrowser/1.0');

    my $url = new URI::URL($the_url);
    my $req = new HTTP::Request('GET', $url, $hdrs);
    my $ua = new LWP::UserAgent;
    my $resp = $ua->request($req);

    if ($resp->is_success) {

    # If connection is successful the contents of the file
    # read will now go into the variable $img
    $img = $resp->content;

    }
    else {

    # If connection is not successful then make note of this
    print $resp->message;
    #$img = "socket_failure";
    }


    use Image::Size;
    # Assume that &read_data gets data somewhere (WWW, etc.)
    my ($height, $width, $id) = imgsize(\$img);
    print $height . " " . $width . " " . $id;


    Matthew Galaher Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139