Gossamer Forum
Home : General : Perl Programming :

HTTP::Size

Quote Reply
HTTP::Size
Hi all!

I got a little problem getting HTTP::Size to work:

Code:

#!/usr/bin/perl
use HTTP::Size;
my $url = "http://www.trempi.com";
my $size = HTTP::Size::get_size( $url );

print "Content-type: text/html\n";
if( defined $size )
{
print "$url size was $size";
}
elsif( HTTP::Size::ERROR == $HTTP::Size::INVALID_URL )
{
print "$url is not a valid absolute URL";
}
elsif( HTTP::Size::ERROR == $HTTP::Size::COULD_NOT_FETCH )
{
print "Could not fetch $url\nHTTP status is $HTTP::Size::HTTP_STATUS";
}
elsif( HTTP::Size::ERROR == $HTTP::Size::BAD_CONTENT_LENGTH )
{
print "Could not determine content length of $url";
}

...gives me the popular 500 error. can anyone explain me what i do wrong here?

thank you in advance.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Hi pgolovko,

There are a couple of things to check. First, you need two new lines when printing the header. Second, it looks like the pod for HTTP::Size has a typo. You need to prefix all the HTTP::Size::ERRORs with a $. useing strict; and warnings; would have caught that. If that doesn't fix it, make sure you have the HTTP::Size module installed.

This code works for me:

Code:
#!/usr/bin/perl
use strict;
use warnings;


use HTTP::Size;
my $url = "http://www.trempi.com";
my $size = HTTP::Size::get_size( $url );

print "Content-type: text/html\n\n";

if( defined $size ) {
print "$url size was $size";
} elsif($HTTP::Size::ERROR == $HTTP::Size::INVALID_URL ) {
print "$url is not a valid absolute URL";
} elsif($HTTP::Size::ERROR == $HTTP::Size::COULD_NOT_FETCH ) {
print "Could not fetch $url\nHTTP status is $HTTP::Size::HTTP_STATUS";
} elsif($HTTP::Size::ERROR == $HTTP::Size::BAD_CONTENT_LENGTH ) {
print "Could not determine content length of $url";
}

~Charlie
Quote Reply
Re: [Chaz] HTTP::Size In reply to
Chaz, you're right.
I forgot to check for installed modules. HTTP::Size isnt installed. But Image::Size is installed:


Code:

#!/usr/bin/perl
use strict;
use Image::Size;
my $img = "http://movies.easy-dater.com/...ries/01/375/play.jpg";
($x, $y) = imgsize(\$img);
print "Content-type: text/html\n\n";
print "[size:$x/$y] $img";


I picked up some information from:
http://search.cpan.org/...e-Size-2.992/Size.pm
which didnt explain much to me. and i still get 500 error on above code.

I am trying to get x/y size of an image on another server.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Code:

#!/usr/bin/perl
use strict;
use Image::Size 'html_imgsize';
my $size = html_imgsize("http://www.youngandfresh.org/139sp/images/tn01.jpg");
print "Content-type: text/html\n\n";
print "$size";


no errors, but no image size either. returns a plain bank page.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Code:

#!/usr/bin/perl
use Image::Size 'html_imgsize';
$url = "http://www.youngandfresh.org/139sp/images/tn01.jpg";
$size = html_imgsize ($url);
print "Content-type: text/html\n\n";
if ($size) { print "$size"; }
else { print "couldnt get size"; }

still returns 'couldnt get size'
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
examples from http://netfactory.dk/...ology/perl/graphics/ didnt help much either:
Code:

#!/usr/bin/perl
use strict;
use Image::Size;
my $size = Image::Size::html_imgsize("http://www.youngandfresh.org/139sp/images/tn01.jpg");
print "Content-type: text/html\n\n";
print "Here is my image $size\n";

no errors, but no results either.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Image::Size won't take a url as an arg. You need to use something else, like LWP, to get the image data and pass it in as a ref to Image::Size.

Try this:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Image::Size;

my $url = "http://www.youngandfresh.org/139sp/images/tn01.jpg";
my $img = get($url) or die "Failed to fetch image!";
my $size = Image::Size::html_imgsize(\$img);

print "Content-type: text/html\n\n";
print "Here is my image $size\n";

~Charlie
Quote Reply
Re: [Chaz] HTTP::Size In reply to
Thank you Chaz explaining me this. Here's the exact thing that I needed, and it does the job:
Code:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Image::Size;
my $url = "http://www.youngandfresh.org/139sp/images/tn01.jpg";
my $img = get($url) or die "Failed to fetch image!";
my $img_x;
my $img_y;
my $max = 50;
($img_x, $img_y) = Image::Size::imgsize(\$img);

print "Content-type: text/html\n\n";
if($img_x >= $max) {
print "image width is ($img_x), greater than $max";
}
else {
print "image width is ($img_x), less than $max";
}


Thanks again.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
I'm must be blind, because I cand find what gives me Error 500 here :(

Code:

use strict;
use warnings;
use LWP::Simple;
use Image::Size;
my $url;
my $img_x;
my $img_y;
my $max_x = 200;
my $max_y = 200;

open(OLDDATABASE, "<photo1.txt");
@lines = <OLDDATABASE>;
close OLDDATABASE;
foreach $url (@lines) {
my $img = get($url) or die "Failed to fetch image!";
($img_x, $img_y) = Image::Size::imgsize(\$img);
if($img_x >= $max_x) {
print "image width is ($img_x), greater than $max_x";
}
else {
print "image width is ($img_x), less than $max_x";
}
}


photo1.txt is a flat ASCII database of image URLs:
http://adv.alsscan.com/...er/zowl/agnes174.jpg
http://adv.alsscan.com/...r/zowl/agnes174s.jpg
http://adv.alsscan.com/...er/zowl/agnes176.jpg
http://adv.alsscan.com/...r/zowl/agnes176s.jpg

Please correct me here. Thank you.
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
All it takes is few cans of beer and a good sleep. All works now:

Code:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Image::Size;
print "Content-type: text/html\n\n";
open(DATABASE, "<photo1.txt");
my @entries = <DATABASE>;
close DATABASE;
my $url;
my $number = 0;
foreach $url (@entries) {
$number++;
if($number <= 100) {
my $img = get($url) or die "Failed to fetch image!";
my $img_x;
my $img_y;
my $max = 300;
($img_x, $img_y) = Image::Size::imgsize(\$img);
if($img_x >= $max) {
print "$number - $url width is ($img_x), greater than $max<BR>\n";
}
else {
print "$url width is ($img_x), less than $max<BR>\n";
}
}
else { exit; }
}
exit;


Tongue
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
hmmm. works kinda slow with my database of 500,000+ entries, even if i do them by hundereds :(
might it be time to think about mysql, or are there any other solutions?
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Hi pgolovko,

Just a note; be careful with those links. While they are not offensive to me :) they are guaranteed to be offensive to others on the board.

Judging by the images I think I have a good idea of what you're doing. I would probably use a DB to store the link location and meta data, *not* the image itself, or use a template solution like Template::Toolkit or HTML::Template to generate static pages as needed. If the images and page content don't change very often I'd go for #2. If they do change often then #1 is the way to go. Use Perl or PHP to dynamically generate the pages and use a DB to grab the image location and any meta data that you need. You can modify your script to read in directories of images and add the info to the DB.

~Charlie
Quote Reply
Re: [Chaz] HTTP::Size In reply to
Thanks. I'm gonna think about your suggestions.
Oh, and I'm sorry for the censored links, I shouldnt have used examples from my database here :(
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
To get all CGI execution load from the server I downloaded Activestate Perl and installed it on my PC. Testing my scripts, so far they run good. If all goes well, then I will only need to upload a ready to use database to my web site, while performing cron jobs to sort databse on my PC doring the night.

The major problem right now is that my database grows big very fast. On average it takes about 1 second to validate 1 image URL with above code. My database ... I'm not talking about 50,000 of entries. Its almost 1,000,000 of entries and few megabytes of a single flat ASCII database. I dont know if its possible to do with Perl... Is it possible to check URLs not one by one, but say 20 or 100 URLs at the same time? Multitasking? Or what is it called? The problem is not within my machine (P4 3Ghz, 512RAM, WinXP) or my DSL connection, its either within the code or Perl it self. 1 second per URL is far too slow for such database :(
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Hey Chaz, is there a way to calculate elapsed time since perl script started? As I said above, it takes few hours to sort my database, and I leave it running at night. I just thought it might be useful to know how many hours/minutes/seconds it actually takes to complete the job. I rewrote the script, it now checks about 3-4 entries per second. I was thinking of grabbing current system time at the beginning of the script and then get time again. Based on these two I'd get the time it took for script to run. What do you think?
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
Hmm. What would this error be?
Quote:

Prototype mismatch: sub main::head ($) vs none at C:/Perl/lib/CGI.pm line 299.

It occurs right when I start the script, but doesnt affect the script.

Here's the cut from CGI.pm. Line 299 is in red.
Code:

# to import symbols into caller
sub import {
my $self = shift;
# This causes modules to clash.
undef %EXPORT_OK;
undef %EXPORT;
$self->_setup_symbols(@_);
my ($callpack, $callfile, $callline) = caller;
# To allow overriding, search through the packages
# Till we find one in which the correct subroutine is defined.
my @packages = ($self,@{"$self\:\:ISA"});
foreach $sym (keys %EXPORT) {
my $pck;
my $def = ${"$self\:\:AutoloadClass"} || $DefaultClass;
foreach $pck (@packages) {
if (defined(&{"$pck\:\:$sym"})) {
$def = $pck;
last;
}
}
*{"${callpack}::$sym"} = \&{"$def\:\:$sym"};
}
}


Odd error that doesnt seen to do anything bad? My script continues to run as it should with no further errors. I couldnt find any useful information about it on CPAN or perl.org Crazy
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
is there a way to calculate elapsed time since perl script started?
Code:
$begin = times;
your page comes here and all your page code that takes the process!
$end = times;
printf "This page was process in %.2f second(s).\n", $end - $begin;


You mean something like that ? hope i helped :)

Last edited by:

NamedRisk: Jul 16, 2005, 5:59 AM
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
I see Time::HiRes[1] mentioned for this quite often but if it takes that long I don't see being that precise will do you much good :)

~Charlie

[1] http://search.cpan.org/...-HiRes-1.72/HiRes.pm
Quote Reply
Re: [pgolovko] HTTP::Size In reply to
What are you trying to import from CGI.pm? I thought you were using the OO form so your use line should be use CGI qw(); as to not import anything by default.

~Charlie
Post deleted by courierb In reply to