Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry maximum width and height in CGI

    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

  • #2
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

    Code:
    #!/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";
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you wanted to get the size of a JPEG file you could use this subroutine:

    Code:
    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 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!

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    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..

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:

    Code:
    #!/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.

  • #7
    New Coder
    Join Date
    Aug 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks...

    This was a great tutorial of this function... never used it yet but it's in my next project.

  • #8
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I have a problem

    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

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    Code:
    #!/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.

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    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.

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by FishMonger View Post
    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

  • #12
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Quote Originally Posted by FishMonger View Post
    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.

  • #13
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    Quote Originally Posted by KevinADC View Post
    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.

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by KevinADC View Post
    Fish, look at the date of the original thread.
    , never mind, maybe somebody need this stuff.
    Is bad that people don't search, just post again.

    best regards

  • #15
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    - 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).


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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