Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Image Size 1

Status
Not open for further replies.

kevinpham

Programmer
Dec 21, 2001
32
US
Hi,
how do you detect the image size using plain CGI with and without any additionaly modules? It screw up my program when someone upload an image with big size, if I displaying with resizing effect , says width=200, then smaller images whose width is only , says 50, looks distorted

So it is great if we can use CGI and detect the size before we display them. So if images whose width are larger than 200 pixels then we resize it to 200, if images whose width are smaller then we just keep it?

Is there anyway we can do that? cheer
kevin
 
I found a subroutine somewhere called (I think) gifsize (and corresponding jpegsize) that will return the size of an image file. Try searching for those.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
thanks tracy,

It is great if you have it on hand... c'z it is like find a sand in a desert. Do you know if it require any special modules to run???

I will keep searching. Anyone who knows, please let me share... cheer
kevin
 
gifsize.pl
Code:
#!/usr/local/bin/perl 
# Program name: gifsize 
# Author: Rajiv Pant (Betul) 
# Version: 1.0. October 1994. 
foreach $gif (@ARGV) {
	($width, $height, $type) = &gifsize ($gif);
	($type =~ m/GIF/i) ? print "\n" : print "$gif is not a GIF.\n" ;
}

sub gifsize {
	local ($gif) = @_ ;
	local ($w, $w2, $h, $h2, $gifwidth, $gifsize, $type) = () ;
	open (GIF, $gif) ;
	read (GIF, $type, 3) ;
	seek (GIF, 6, 0) ;
	read (GIF, $w, 1) ;
	read (GIF, $w2, 1) ;
	$width = ord ($w) + ord ($w2) * 256 ;
	read (GIF, $h, 1) ;
	read (GIF, $h2, 1) ;
	$height = ord ($h) + ord ($h2) * 256 ;
	close (GIF) ;
	return ($width, $height, $type) ;
} # end of sub gifsize

jpgsize.pl
Code:
#!/usr/local/bin/perl
## Program name: jpegsize
## Author: Stuart Curtis Lathrop <sclathrop@niitech.com>
## Version: 1.0. July 2000.
## Copyright: This code is placed into the Public Domain under the
##            terms of the GNU Public License. No fee may be charged
##            for its use or distribution.
##
## Kudos to: Rajiv Pant (Betul) <betul@rajiv.com [URL unfurl="true"]http://rajiv.org>,[/URL]
##             author of gifsize.pl
##           Jef Poskanzer <jef@acme.com>, author of image_size.c
##           and the Independent JPEG Group for their direction!
##
## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
## SUCH DAMAGE.
##
## Notes: This script will _not_ handle JPEG redefinition of image size
##        via DNL marker tag.  Files so encoded will either return zero
##        width & height (typically) or will generate a tag error.
##


foreach $jpeg (@ARGV)
{
  $M_SOI  = &quot;\xd8&quot;;
  $M_BLK  = &quot;\xff&quot;;

  ($width, $height, $type) = jpegsize($jpeg);
  print &quot;$jpeg width = &quot;, $width, &quot;, height = &quot;, $height;
  ($type =~ $M_BLK.$M_SOI) ? print &quot;\n&quot; : print &quot;$jpeg is not a JPEG.\n&quot; ;
}

sub jpegsize
{
  local ($jpeg) = @_ ;
  local ($type, $tag, $marker, $buffer, $lhob, $llob, $blocklen) = ();
  local ($whob, $wlob, $hhob, $hlob, $width, $height) = ();

  ## required jpeg markers
  $M_SOF0 = &quot;\xc0&quot;;
  $M_SOF1 = &quot;\xc1&quot;;
  $M_SOF2 = &quot;\xc2&quot;;
  $M_SOF3 = &quot;\xc3&quot;;
  $M_SOI  = &quot;\xd8&quot;;
  $M_EOI  = &quot;\xd9&quot;;
  $M_SOS  = &quot;\xda&quot;;
  $M_BLK  = &quot;\xff&quot;;

  open (JPEG, &quot;<&quot;.$jpeg) || die &quot;Unable to open $jpeg, $!&quot;;
  binmode (JPEG) || die &quot;Cannot read $jpeg in binary mode, $!&quot;;
  read (JPEG, $type, 2) || die &quot;Error while reading $jpeg, $!&quot;;
  ## check for jpeg file type (start of image) marker
  if (!($type eq $M_BLK.$M_SOI))
  {
    close(JPEG) || die &quot;Error closing $jpeg, $!&quot;;
    return ( 0, 0, $type );
  }
  for (;;)
  {
    ## check for block tag
    read (JPEG, $tag, 1) || die &quot;Error while reading $jpeg, $!&quot;;
    if ($tag ne $M_BLK)
    {
      print &quot;Error: No start of block marker (0xff) found!\n&quot;;
      close(JPEG) || die &quot;Error closing $jpeg, $!&quot;;
      return ( 0, 0, $type );
    }
    ## get marker type & block length
    read (JPEG, $marker, 1) || die &quot;Error while reading $jpeg, $!&quot;;
    read (JPEG, $lhob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
    read (JPEG, $llob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
    $blocklen = (ord($lhob) * 256) + ord($llob) - 2;
    ## check for any start of field marker
    if ( $marker ge $M_SOF0 && $marker le $M_SOF3 )
    {
      ## ignore data precision
      read (JPEG, $buffer, 1) || die &quot;Error while reading $jpeg, $!&quot;;
      ## read the height and width.
      read (JPEG, $hhob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
      read (JPEG, $hlob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
      $height = (ord($hhob) * 256) + ord($hlob);
      read (JPEG, $whob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
      read (JPEG, $wlob, 1) || die &quot;Error while reading $jpeg, $!&quot;;
      $width = (ord($whob) * 256) + ord($wlob);
      # ignore components & rest of file...
      close(JPEG) || die &quot;Error closing $jpeg, $!&quot;;
      return ($width, $height, $type);
    }
    else
    {
      if ( $marker eq M_SOS || $marker eq M_EOI )
      {
        ## past header data; size indeterminable
        close(JPEG) || die &quot;Error closing $jpeg, $!&quot;;
        return (0, 0, $type);
      }
      ## skip to next marker
      read (JPEG, $buffer, $blocklen);
    }
  }
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
thanks,
I just found it and was about to post it here and see you post. Wonderful... I dind't know much about images. if i knew they have image tags somewhere in that binary chunks, i could write one myself.

But thanks to the one who wrote that. Now, i won;t let users submit images that are bigger than my intention...

thanks Tracy once again cheer
kevin
 
You're welcome. If anyone needs a version of these subroutines that works with an image file loaded into a variable (like you would have if you fetched it from another size with LWP GET), I modified these two routines to do that too. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Is there a way you can resize them without using Image:Magick if the width is bigger than what you want? cheer
kevin
 
I don't know, I don't have code for that. All I was doing was checking banner image sizes for a link sharing page and rejecting them if they were the wrong size. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
i used to seek for graphics interchange format.. hang on..

here what i found about gif format:
this one is great:
this is a bit more textfull :) :
this looks like a mirror of rfc:

gif vs jpeg:

thats all.. then i guess i went to print what have found & was satisfied with results..
 
that's so useful.
now i know where the width and height come from... and understand more then functions that tracy provided

thanks cheer
kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top