Image Dimensions Returned in asp

Discussion in 'C#' started by red_fiesta, Nov 23, 2006.

  1. #1
    I am using code similar to

    http://www.brainjar.com/asp/dirlist/

    i am using the code to view contents of an image directory..

    is there anyway of writing to the screen the dimensions of the image??

    thanks
     
    red_fiesta, Nov 23, 2006 IP
  2. Garve

    Garve Peon

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are supposed to be some unreliable and messy ways to do this in ASP, but it's much better if you have a component like ASPJpeg installed.

    ASP.net has facilities built in to do this, but it's too complicated for me.
     
    Garve, Nov 23, 2006 IP
  3. red_fiesta

    red_fiesta Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what is this aspJpeg component..

    it has to be asp as it is for university

    I have managed to get to see file name, file size, dimensions using the code below..


    how do i use the code to make sure i dont return some file types. (ie thumbs.db?)

    <%@ LANGUAGE="VBScript" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>ASP Directory Listing Demo</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="/common/default.css" />
    </head>
    <body>
    
    <div id="demoBox">
    
    <h3>ASP Directory Listing Demo</h3>
    
    <p>Three separate directories from the web server root are displayed below
    ("asp", "dhtml" and "js"), showing all subdirectories and files. Additionally,
    each file is displayed as a link.</p>
    
    </div>
    
    <!-- List these three folders. -->
    <ul>
    <% ListFolderContents(Server.MapPath("/images/")) %>
    
    </ul>
    
    </body>
    </html>
    
    <% sub ListFolderContents(path)
    
         dim fs, folder, file, item, url
    
         set fs = CreateObject("Scripting.FileSystemObject")
         set folder = fs.GetFolder(path)
    
        'Display the target folder and info.
    
         Response.Write("<li><b>" & folder.Name & "</b> - " _
           & folder.Files.Count & " files, ")
         if folder.SubFolders.Count > 0 then
           Response.Write(folder.SubFolders.Count & " directories, ")
         end if
         Response.Write(Round(folder.Size / 1024) & " KB total." _
           & vbCrLf)
    
         Response.Write("<ul>" & vbCrLf)
    
         'Display a list of sub folders.
    
         for each item in folder.SubFolders
           ListFolderContents(item.Path)
         next
    
         'Display a list of files.
    
         for each item in folder.Files
           url = MapURL(item.path)
    	   
    	   myURl2=server.mappath("/cm/website/image/" & item.Name & "/")
    	   call getSize(myURl2, width, height, depth, "GIF")
    	   
           Response.Write("<li><a href=""" & url & """>" & item.Name & "</a> - " _
             & item.Size & " bytes, " _
             & myURl2 &"</li>" & vbCrLf)
    		 
    		 
    		  
     Response.Write "height=" & Height 
     Response.Write "width=" & Width 
     
     
     
    		 
         next
    
         Response.Write("</ul>" & vbCrLf)
    
         Response.Write("</li>" & vbCrLf)
    
       end sub
    
       function MapURL(path)
    
         dim rootPath, url
    
         'Convert a physical file path to a URL for hypertext links.
    
         rootPath = Server.MapPath("/")
         url = Right(path, Len(path) - Len(rootPath))
         MapURL = Replace(url, "\", "/")
    
       end function %>
    
    
    <%
    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 
    			 Session("ImgHeight") = Height
    			 Session("ImgWidth") = Width
        
              else 
    
                 Height = lngConvert2(midb(strBuff, lngPos + 4, 2)) 
                 Width = lngConvert2(midb(strBuff, lngPos + 6, 2)) 
    			 Session("ImgHeight") = Height
    			 Session("ImgWidth") = Width
                 Depth = 2 ^ (ascb(midb(strBuff, lngPos + 8, 1)) * 8) 
                 getSize = True 
    
              end if 
                      
        end if 
      
     set obj = Nothing 
     end function
     
     
    %>
    Code (markup):
     
    red_fiesta, Nov 23, 2006 IP