Obtain the image size of an image file

Source: Internet
Author: User
// ================================================ ====================================
// Function: of_getpicturesize ()
//--------------------------------------------------------------------
// Description: obtains the image size of an image file (supported in GIF, JPG, and BMP formats)
//--------------------------------------------------------------------
// Parameters:
// Value string as_filename name of the image file
// Reference long al_picturewidth returns the Image Width
// Reference long al_pictureheight returns the Image Height
//--------------------------------------------------------------------
// Return value: integer 1-success, 0-Failure
//--------------------------------------------------------------------

Integer li_file, li_dataread
Blob lb_data
Long ll_filelength, ll_picturewidth, ll_pictureheight
Long ll_datalen, ll_datapos, ll_filepos
Boolean lb_loopflag = true
Char lc_char1, lc_char2

// The file does not exist.
If not fileexists (as_filename) then return 0

// Obtain the file size
Ll_filelength = filelength (as_filename)

// Open the file
Li_file = fileopen (as_filename, streammode !)
If li_file =-1 then return 0

// Read the file
Li_dataread = fileread (li_file, lb_data)
If li_dataread <= 0 then
Fileclose (li_file)
Return 0
End if

// Currently, there are two main types of GIF
// 1. It is identified as gif87a and is only used to store a single static image.
// 2. Marked as gif89a. Several static images can be stored at the same time to form continuous animation.
// The first 6 bytes of the file are marked as: gif87a or gif89a, and the 7 or 8 bytes are the image width (width ),
// The 9th and 10th bytes are the height of the image. Pay attention to the two bytes before the low position

// Determine the GIF File Format
If string (blobmid (lb_data,) = 'gif 8' then
Ll_picturewidth = ASC (string (blobmid (lb_data, 7,1) + ASC (string (blobmid (lb_data, 8, 1 )))*

256
Ll_pictureheight = ASC (string (blobmid (lb_data, 9, 1) + ASC (string (blobmid (lb_data, 10, 1 )))

* 256
If ll_picturewidth> 0 and ll_pictureheight> 0 then
Al_picturewidth = ll_picturewidth
Al_pictureheight = ll_pictureheight
Fileclose (li_file)
Return 1
Else
Fileclose (li_file)
Return 0
End if
End if

// JPEG file format
// The first three bytes are marked as 0xff, 0xd8, and 0xff.
If string (blobmid (lb_data, 255) = char (216) + char (255) + char () then
Ll_datalen = Len (lb_data)
Ll_datapos = 3
Ll_filepos = 3
Do While lb_loopflag
Ll_datapos = ll_datapos + 1
Ll_filepos = ll_filepos + 1
Lc_char1 = string (blobmid (lb_data, ll_datapos, 1 ))
Lc_char2 = string (blobmid (lb_data, ll_datapos + 1,1 ))
If lc_char1 = char (255) and lc_char2 <> char (255) then
If lc_char2> = char (192) and lc_char2 <= char (195) then
// Locate the Dimension Data
Ll_picturewidth = ASC (string (blobmid (lb_data, ll_datapos + 7,1 )))*

256 + ASC (string (blobmid (lb_data, ll_datapos + 8, 1 )))
Ll_pictureheight = ASC (string (blobmid (lb_data, ll_datapos + 5, 1 )))

* 256 + ASC (string (blobmid (lb_data, ll_datapos + 6, 1 )))
Lb_loopflag = false
Else
// No size data is found and the file is read again
Ll_filepos = ll_filepos + ASC (string (blobmid (lb_data, ll_datapos +

3, 1) * 256 + ASC (string (blobmid (lb_data, ll_datapos +) + 1
If ll_filepos> ll_filelength then
Fileclose (li_file)
Return 0
Else
FileSeek (li_file, ll_filepos)
Fileread (li_file, lb_data)
Ll_datalen = Len (lb_data)
Ll_datapos = 0
End if
End if
End if
If ll_datapos = ll_datalen-9 and lb_loopflag = true then
Ll_filepos = ll_filepos-9
FileSeek (li_file, ll_filepos)
Fileread (li_file, lb_data)
Ll_datalen = Len (lb_data)
Ll_datapos = 0
End if
If ll_filepos> = ll_filelength then
Lb_loopflag = false
End if
Loop
If ll_picturewidth> 0 and ll_pictureheight> 0 then
Al_picturewidth = ll_picturewidth
Al_pictureheight = ll_pictureheight
Fileclose (li_file)
Return 1
Else
Fileclose (li_file)
Return 0
End if
End if

// BMP file format
// The first two bytes are identifiers: there may be many types of identifiers
// The 19,20, bytes are the width (width), 25, and 26 bytes are the height (height)
// DWORD is composed of four bytes.
If string (blobmid (lb_data, 1, 2) = 'bm 'then
Ll_picturewidth = ASC (string (blobmid (lb_data, 19,1) + ASC (string (blobmid (lb_data, 20,1 )))

* 256 + ASC (string (blobmid (lb_data, 21,1) * 65536 + ASC (string (blobmid (lb_data, 16777216) *
Ll_pictureheight = ASC (string (blobmid (lb_data,) + ASC (string (blobmid (lb_data, 24, 1 )))

* 256 + ASC (string (blobmid (lb_data, 65536) * 16777216 + ASC (string (blobmid (lb_data ,) *
If ll_picturewidth> 0 and ll_pictureheight> 0 then
Al_picturewidth = ll_picturewidth
Al_pictureheight = ll_pictureheight
//// Convert the actual image size to the pbunit size and return
// Al_picturewidth = pixelstounits (ll_picturewidth, xpixelstounits !)
// Al_pictureheight = pixelstounits (ll_pictureheight, ypixelstounits !)
Fileclose (li_file)
Return 1
Else
Fileclose (li_file)
Return 0
End if
End if

Return 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.