...

maximum width and height in CGI

sanuk
08-07-2002, 04:43 PM
Hello friends,
I am having the following problem:
In my CGI script, charity clubs can create a reference page.
All their date is submited thru a form, including a URL to a logo picture
The picture can only be MAXIMUM 300px width and 80px heigh.
If bigger than 300 on 80, then the table is completely pulled out of proportion.

example of submitted data and the print statement:
$title = "This is Club Bla Bla Bla";
$subtitle = "about the club bla bla bla";
$logoURL = "http://thisclub.com/images/ourlogo.gif";

==== The CGI print statement ========================
print "<table border=0 cellpadding=0 cellspacing=0 width='600'>\n";
print "<tr><td align='left' valign='top' width='300'>$title <br> $subtitle</td>\n"
print "<td align=center valign=middle width=300>\n"
print "<img src='$logoURL'></td></tr></table>\n"
=========================================

What I am looking for is a script that would reduce the width to 300 if larger than 300 and reduce the height to 80 if larger than 80.

Problems are that height and width are not submitted, only the URL to the picture.
Also I do not want that the Logo-picture is uploaded to my website.

Thanks in advance
Regards,
Sanuk

Feyd
08-07-2002, 05:58 PM
You could use the extension module Image::Size. You could also GD, GIMP or ImageMagick.

PERL, in its current incarnation, wasn't really meant to do this kind of stuff...though the future plans put it up to near the levels of PHP in dynamics and ease, which is what I would recommend for this...but I'll see what I can do for you with just PERL...

Now this routine would work for gifs...it reads the first 10 bytes of the gif file, which stores dimensions, and gives them to you to work with... (chances are, however, that with the dimensions you are talking about that these people will be using jpgs, or at least they should be if they know what they are doing)


#!/usr/bin/perl -w

use strict;

# ($heightref, $widthref) = gifdim($filename);

sub gifdim ($) {
my $filename = $_[0];

open(GIF, $filename) || return (undef, undef);
my $buf = '';
my $n = read GIF, $buf, 10;
close GIF;

return (undef, undef) if $n < 10;
my ($head, $width, $height) = unpack("A6vv", $buf);
return (undef, undef) unless $head =~ /^GIF8[79]a/;
return \($width, $height);
}

#example:
# print "<IMG SRC=\"$filename\" WIDTH=\"" . $$widthref . "\" HEIGHT=\"" . $$heightref . "\">\n";

Mouldy_Goat
08-07-2002, 11:02 PM
If you wanted to get the size of a JPEG file you could use this subroutine:

sub jpegsize {
my($JPEG) = @_;
my($done)=0;
my $size, $height, $width;

read($JPEG, $c1, 1); read($JPEG, $c2, 1);
if( !((ord($c1) == 0xFF) && (ord($c2) == 0xD8))){
print "This is not a JPEG!";
$done=1;
}
while (ord($ch) != 0xDA && !$done) {
# Find next marker (JPEG markers begin with 0xFF)
# This can hang the program!!
while (ord($ch) != 0xFF) { read($JPEG, $ch, 1); }
# JPEG markers can be padded with unlimited 0xFF's
while (ord($ch) == 0xFF) { read($JPEG, $ch, 1); }
# Now, $ch contains the value of the marker.
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
read ($JPEG, $junk, 3); read($JPEG, $s, 4);
($a,$b,$c,$d)=unpack("C"x4,$s);
$height = $a<<8|$b;
$width = $c<<8|$d;
$done=1;
} else {
# We **MUST** skip variables, since FF's within variable names are
# NOT valid JPEG markers
read ($JPEG, $s, 2);
($c1, $c2) = unpack("C"x2,$s);
$length = $c1<<8|$c2;
if( ($length < 2) ){
print "Erroneous JPEG marker length";
$done=1;
} else {
read($JPEG, $junk, $length-2);
}
}
}
return ($height, $width);
}

This is a slightly hacked around version of a subroutine I found in a script (this (http://www.ocean.ic.net/ftp/systems/linux/www/wwwis) script if you're interested).

I was going to look up the JPEG specifications and write my own... but found this first.

Haven't tested it yet so it might not work!

Mouldy_Goat
08-07-2002, 11:20 PM
Ok I've just tested it and it works fine.
Remember though that you need to pass it a filehandle reference for it to work - not a scalar with the data in!

i.e. this was my test code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
open (STREAM, "image.jpg") || die "Couldn't open image.jpg for reading: $!";
binmode STREAM; # could be on a win machine for all we know..
($height, $width) = jpegsize(\*STREAM);
close STREAM;

printf ("Image is %d pixel%s high and %d pixel%s wide\n", $height, ($height == 1)?'':'s', $width, ($width == 1)?'':'s');

With the subroutine pasted on the end of the file obviously..

sanuk
08-08-2002, 05:35 AM
Hi friends,
Thanks for your replys

I am trying to make the snippets that you gave work,
but without succes.
Seems I do not know very well where to enter the url of the outsite website where the logo picture is.

I have tried the following 2 code snippets you gave me,
using a pictures from www.travel.com as a example


============================

#!/usr/bin/perl -w
use CGI::Carp "fatalsToBrowser";
$wherepic = "http://www.travel.com/site/ship.jpg";
print "Content-type: text/html\n\n";
open (STREAM,$wherepic) || die "Couldn't open image.jpg for reading: $!";
binmode STREAM; # could be on a win machine for all we know..
($height, $width) = jpegsize(\*STREAM);
close STREAM;
printf ("Image is %d pixel%s high and %d pixel%s wide\n", $height, ($height == 1)?'':'s', $width, ($width == 1)?'':'s');

=======================================

#!/usr/bin/perl -w
use CGI::Carp "fatalsToBrowser";
use strict;
$filename = "http://www.travel.com/images/hrn.gif";
# ($heightref, $widthref) = gifdim($filename);
sub gifdim ($) {
my $filename = $_[0];
open(GIF, $filename) || return (undef, undef);
my $buf = '';
my $n = read GIF, $buf, 10;
close GIF;
return (undef, undef) if $n < 10;
my ($head, $width, $height) = unpack("A6vv", $buf);
return (undef, undef) unless $head =~ /^GIF8[79]a/;
return \($width, $height);
}
#example:
print "<IMG SRC=\"$filename\" WIDTH=\"" . $$widthref . "\" HEIGHT=\"" . $$heightref . "\">\n";

=============================

But No success
Regards,
Sanuk

Mouldy_Goat
08-09-2002, 12:32 AM
Ahh.... Perl doesn't do implicit remote opens for you, which is a Good Thing really...

If that's what you want to do you can use something like:

sub remote_get {
die "Not enough args to remote_exists" unless (@_ > 2);
my ($host, $EOL, $BLANK, $document, $remote, $saveto, $binary);
($host, $document, $saveto, $binary) = @_;
$EOL = "\015\012";
$BLANK = $EOL x 2;

$remote = new IO::Socket::INET( Proto => "tcp",
PeerAddr => $host,
PeerPort => "http(80)",
);
unless ($remote) { die "cannot connect to http daemon on $host" }
$remote->autoflush(1);
print $remote "GET $document HTTP/1.0" . $EOL . "Host: $host" . $BLANK;
while ( <$remote> ) { $data .= $_ }
close $remote;

$data =~ s/^.*?\n\n//s;
open (SAVE, ">$saveto") || die "Couldn't open $saveto for writing: $!";
binmode SAVE if $binary;
print SAVE $data;
close SAVE;
}

You'd use that like this:

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

remote_get("http://www.travel.com", "/site/ship.jpg", "ship.jpg", 1);
$wherepic = "ship.jpg";
print "Content-type: text/html\n\n";
open (STREAM,$wherepic) || die "Couldn't open image.jpg for reading: $!";
binmode STREAM; # could be on a win machine for all we know..
($height, $width) = jpegsize(\*STREAM);
close STREAM;
printf ("Image is %d pixel%s high and %d pixel%s wide\n", $height, ($height == 1)?'':'s', $width, ($width == 1)?'':'s');
unlink $wherepic;

Hope that helps a bit.

pager
08-09-2002, 12:53 AM
This was a great tutorial of this function... never used it yet but it's in my next project. :thumbsup:

Macsimum
12-13-2007, 11:43 AM
Thanks for this, I was wondering how to get the size of images.

From comments that have been made, if you know the size of the image, specify them on the <img src=...> and it will load quicker and better. I thought I would add it to my thumbnail page which has a variety of thumbnails at different sizes. However once I call the jpegsize routine with the filehandle reference, it doesnt seem to change the filehandle reference on the second call. So the size of the first thumbnail get tagged on all the other thumbnails.

I close the file each time with no error, below is the code,

sub display_thumbnail {
my $photo = $_[0];
my ($width, $height);
if ($photo =~m/.jpg$/) {
$filename = $fullpath.$_;
if ($i %5 == 0) {
if ($i) { print("</tr>\n"); }
print "<tr>\n";
}
$thumbnail = $fullpath."thumbnails/".$_;

if (-e $thumbnail) {
$filename = $thumbnail;
} else {
$filename .= '" width="120';
}
$caption =$_;
$thumbnail =~ s/.jpg$/.txt/;
if (-e $thumbnail) {
open(TITLE, $thumbnail);
$caption = <TITLE>;
close(TITLE);
}
if (length($caption) >130) { $caption = substr($caption, 0, 130)."..."; }
open (IMAGE, $filename) || die "Couldn't open $filename for reading: $!";
binmode IMAGE; # could be on a win machine for all we know..
($width, $height) = &jpegsize(*IMAGE);
close IMAGE || die "Couldn't close $filename: $!";;
$caption = qq(alt="$caption");
print qq(<td align="center">);
if ($location) {
print qq(<a href="photo.cgi?gallery=$location&image=$_">);
} else {
print qq(<a href="photo.cgi?image=$_">);
}
print qq(<img src="$filename" $caption width="$width" height="$height"></a></td>\n);
$i = $i + 1;
}
}

Can any one tell me what I am doing wrong?

Thanks

FishMonger
12-13-2007, 05:23 PM
#!/usr/bin/perl

use warnings;
use strict;
use LWP::Simple;
use Image::Size 'html_imgsize';

getstore('http://www.codingforums.com/img/logo.gif', 'CFlogo.gif');
my $size = html_imgsize("CFlogo.gif");

print $size;

outputs:
width="315" height="64"

Once you know the size, you can adjust it accordingly, then delete the local file and use the url in the img tag.

FishMonger
12-13-2007, 06:04 PM
What I am looking for is a script that would reduce the width to 300 if larger than 300 and reduce the height to 80 if larger than 80.Does that mean that if an image is 300x300 you'll reduce it to 300x80? You should maintain the proportions and reduce accordingly.

oesxyl
12-13-2007, 09:21 PM
Does that mean that if an image is 300x300 you'll reduce it to 300x80? You should maintain the proportions and reduce accordingly.

- only few people see the difference if the proportion is modified up to 20%
- images not stored on the server increase the loading page time and that afect the number of visitors.

best regards

KevinADC
12-13-2007, 09:27 PM
Does that mean that if an image is 300x300 you'll reduce it to 300x80? You should maintain the proportions and reduce accordingly.

Fish, look at the date of the original thread.

FishMonger
12-13-2007, 09:47 PM
Fish, look at the date of the original thread.
LOL, Boy did miss that!

Posting to this old of a thread is a waste of time/effort.

oesxyl
12-13-2007, 09:49 PM
Fish, look at the date of the original thread.

:D, never mind, maybe somebody need this stuff.
Is bad that people don't search, just post again.

best regards

FishMonger
12-13-2007, 09:59 PM
- only few people see the difference if the proportion is modified up to 20%How do you know if it's over/under the 20% without doing the calculation? If you're going to do the calculation, you might as well do it right and keep the proportioning.

- images not stored on the server increase the loading page time and that afect the number of visitors.That is correct. Unfortunately it's what the OP wanted.

Both of our comments are mote since this is such an old thread; the OP and each of the others are long gone (they haven't posted since 2003/2004).

oesxyl
12-13-2007, 10:31 PM
How do you know if it's over/under the 20% without doing the calculation? If you're going to do the calculation, you might as well do it right and keep the proportioning.

I don't say nothing about skiping the calculation. You say to keep the proportion, I'm agree with you. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum