We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 54,509 times

Downloads

Related Categories

Retrieving Remote Image Properties in ASP

LawrenceF

Recently, my friends and I decided to make a site similar to www.hotornot.com. We wanted to allow users to just put the URL of an image they want displayed on the site and the image would eventually be displayed. However, some people put images that were too large and distorted the layout of the website. We attempted to make every image a set length and width, but that was disproportional and stretched out most of the images. I found a file by Mike Shaffer called IMGSZ. I attempted to make the script work with local files, but I had troubles. I wrote a file based on the size defining algorithms in IMGSZ that would also pull the image from a remote website using XML. It works very similarly to what IMGSZ should and I would like to note his help on this. The only flaw is that it only supports GIF and JPG files, which are the only files I needed to support.

The syntax to use getSize is as follows:

someBoolean = getSize(URL, width, height, depth, flType)

  • someBoolean will be set to true or false based on whether the script was able to determine the images attributes
  • URL is an input stating the URL of the image, such as http://www.4guysfromrolla.com/images/newbanner.gif
  • Width will be set to the width of the image, or –1 if the script failed
  • Height will be set to the height of the image, or –1 if the script failed
  • Depth will be set to the depth of the image, or –1 if the script failed
  • FlType will be set to the type of file the image was, GIF or JPG

A major problem with IMGSZ was that it did not use binary string functions such as midb and lenb. Since all images are binary, these functions are required opposed to mid and len.

'This script was written by Lawrence Finn'
'Anyone is free to use this file or alter it at their discretion.
' lngconvert and lngconvert2 were written by Mike Shaffer
'The rest is based on his file IMGSZ with major modifications
'...now it actually works!

  function GetBytes(objHTTP, offset, bytes)

    Dim SizeofFile
    on error resume next
    SizeofFile = objHTTP.getResponseHeader("Content-Length")
    'gets the size of the file from the HTTP header    
    if offset > 0 then 'start getting bytes NOT at the beginning
    strbuff = midb(objHTTP.responseBody, offset, bytes)
    end if
    if bytes = -1 then        ' Get All!
       GetBytes = objHTTP.responseBody  'ReadAll
    else 'start at front and go some odd distance
       GetBytes = midb(objHTTP.responseBody, 1, bytes)
    end if
 end function


 function lngConvert(strTemp)
    lngConvert = clng(ascb(leftb(strTemp, 1)) + ((ascb(rightb(strTemp, 1)) * 256)))
 end function

 function lngConvert2(strTemp)
    lngConvert2 = clng(ascb(rightb(strTemp, 1)) + ((ascb(leftb(strTemp, 1)) * 256)))
 end function
'''''lngconvert was taken from Mike Shaffer's IMGSZ
 
   function getSize(URL, width, height, depth, flType)

    dim PNGflType
    dim GIFflType
    dim BMPflType
    dim flTypeOf
    dim obj
    flTypeOf = ""
    flType = "(unknown)"
    Set obj = Server.CreateObject ("Microsoft.XMLHTTP")
    obj.open "GET", URL, False
    obj.send
    'Here we have gotten the data for the image file
    getSize = False
    PNGflType = chr(137) & chr(80) & chr(78)
    GIFflType = chrb(71) & chrb(73) & chrb(70)
    BMPflType = chr(66) & chr(77)
    'Here are the definitions for the image flTypes, I only support GIF and JPG but you can add others :)
    flTypeOf = GetBytes(obj, 0, 3)
    'Find out what flType of image it is
    if flTypeOf = GIFflType then    'It is a GIF!!!
    flType = "GIF"    
    strbuffer = getbytes(obj, 0, -1) 'get all of the data of the image
    width= lngconvert(midb(strbuffer, 7, 2))
    Height = lngconvert(midb(strbuffer, 9, 2))
    Depth = 2 ^ ((ascb(GetBytes(obj, 11, 1)) and 7) + 1)
    'It is very important to note the ascB and midB, images ARE binary files
    getSize = True
    else
    strBuff = GetBytes(obj, 0, -1)        ' get the entire file
    SizeofFile = lenb(strBuff)
    flgFound = 0
    strTarget = chrb(255) & chrb(216) & chrb(255)
    flgFound = instrb(strBuff, strTarget)
    char = (midb(strbuff, 1, 3)) 'check out the first few characters
    if flgFound = 0 then 'not a jpg, and definatly not a GIF
     exit function
    end if
    flType = "JPG"
    lngPos = flgFound + 2
    ExitLoop = false
    do while ExitLoop = False and lngPos < SizeofFile
          do while ascb(midb(strBuff, lngPos, 1)) = 255 and lngPos < SizeofFile
             lngPos = lngPos + 1
          loop
          'search through to find the data
          if ascb(midb(strBuff, lngPos, 1)) < 192 or ascb(midb(strBuff, lngPos, 1)) > 195 then
             lngMarkerSize = lngConvert2(midb(strBuff, lngPos + 1, 2))
             lngPos = lngPos + lngMarkerSize  + 1
          else
             ExitLoop = True 'have everything we need
          end if

    loop

          if ExitLoop = False then 'oh no!

             Width = -1
             Height = -1
             Depth = -1
   
          else

             Height = lngConvert2(midb(strBuff, lngPos + 4, 2))
             Width = lngConvert2(midb(strBuff, lngPos + 6, 2))
             Depth = 2 ^ (ascb(midb(strBuff, lngPos + 8, 1)) * 8)
             getSize = True

          end if
                 
    end if
 
 set obj = Nothing
 end function

Comments

  • Save file from url in asp

    Posted by Youk88 on 07 Sep 2005

    Hi,

    I wrote a code to save on my site a file from an URL in ASP. You can save an image (jpg,gif,...) or another binary file.

    The code is here : http://www.eromandie.com/codesource/asp/save_file_...

  • BoundImage function

    Posted by subsystems on 25 Jul 2005

    Don't forget to set theHeight, theWidth, and theURL before running this code.
    I styled it based on the first post. Since it was assumed the variables were set before running this code, I did the same...

  • Posted by shem on 25 Jul 2005

    Howzit

    i'm new to asp, i tried to apply the code, now my pictures do not display at all?

    do u need a component to run this code or issit asp.net?

    thanks

  • Using the aspect ratio makes the job easier.

    Posted by subsystems on 24 May 2005

    I adapted some code I used so it used the same style and variable names as your code.
    The idea of this code is to make sure the image will fit within a max rectangle and keep the image's aspect ratio...

  • 800c0005

    Posted by michail1 on 16 Apr 2005

    Been searching on this for days.

    I am confirming that is has something to do with a firewall.

    I have access to 4 servers. 2 running Windows 2000 SBS (small business server) with ISA firewall - ...