Results 1 to 8 of 8

Thread: [RESOLVED] Picture Height/Width

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Resolved [RESOLVED] Picture Height/Width

    Does anyone have a asp/vbscript way of getting the height and width of a picture of any kind for server side processing???
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

  2. #2
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Picture Height/Width

    i have used code based on this for a while.
    http://www.4guysfromrolla.com/webtec...imgsz.asp.html

    here is my slightly modded version.
    (You will need to add your own error checking though)
    Add the original authors credit in somewhere also,
    i just cleaned it up for this example.
    Code:
    <%@ Language=VBScript %>
    <%
    Option Explicit
    Response.Buffer = True
    
    Dim imgWidth, imgHeight, imgColors, imgFile
    imgFile = Server.Mappath(Request("img")) 
    
    If gfxSpex(imgFile, imgWidth, imgHeight, imgColors) = True Then
        Response.Write imgFile & " (" & imgWidth & " x " & imgHeight & ")"
    Else
        Response.Write imgFile & " (Unknown Size)"
    End If
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::                                                             :::
    ':::       ****** FILE FUNCTIONS ******                          :::
    ':::                                                             :::
    ':::       flnm        => Filespec of file to read               :::
    ':::       width       => width of image                         :::
    ':::       height      => height of image                        :::
    ':::       depth       => color depth (in number of colors)      :::
    ':::                                                             :::
    ':::       Copyright *c* MM, Mike Shaffer                        ::: 
    ':::       Based on ideas presented by David Crowell             :::
    ':::       ALL RIGHTS RESERVED WORLDWIDE                         :::
    ':::                                                             :::
    ':::       Modified from Original                                :::
    ':::                                                             :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    Function gfxSpex(flnm, width, height, depth)
        dim strType
        dim strImageType 
        dim strTarget
        dim strPNG 
        dim strGIF
        dim strJPG
        dim strBMP
        dim ExitLoop 
        dim strBuff
        dim flgFound
        dim lngMarkerSize
        dim lngSize	
        dim lngPos 
        strType = ""
        gfxSpex = False
        strPNG = chr(137) & chr(80) & chr(78)
        strGIF = "GIF"
        strJPG	= "JPG"
        strBMP = chr(66) & chr(77)
        strType = GetBytes(flnm, 0, 3)
        if strType = strGIF then	
           strImageType = "GIF"
           Width = lngConvert(GetBytes(flnm, 7, 2))
           Height = lngConvert(GetBytes(flnm, 9, 2))
           Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
           gfxSpex = True
        elseif left(strType, 2) = strBMP then
           strImageType = "BMP"
           Width = lngConvert(GetBytes(flnm, 19, 2))
           Height = lngConvert(GetBytes(flnm, 23, 2))
           Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))
           gfxSpex = True
        elseif strType = strPNG then	
           strImageType = "PNG"
           Width = lngConvert2(GetBytes(flnm, 19, 2))
           Height = lngConvert2(GetBytes(flnm, 23, 2))
           Depth = getBytes(flnm, 25, 2)
           select case asc(right(Depth,1))
               case 0
                  Depth = 2 ^ (asc(left(Depth, 1)))
                  gfxSpex = True
               case 2
                  Depth = 2 ^ (asc(left(Depth, 1)) * 3)
                  gfxSpex = True
               case 3
                  Depth = 2 ^ (asc(left(Depth, 1)))  '8
                  gfxSpex = True
               case 4
                  Depth = 2 ^ (asc(left(Depth, 1)) * 2)
                  gfxSpex = True
               case 6
                  Depth = 2 ^ (asc(left(Depth, 1)) * 4)
                  gfxSpex = True
               case else
                  Depth = -1
            end select
        else	
            strBuff = GetBytes(flnm, 0, -1)	
            lngSize = len(strBuff)
            flgFound = 0
            strTarget = chr(255) & chr(216) & chr(255)
            flgFound = instr(strBuff, strTarget)
            if flgFound = 0 then
               exit Function
            end if
            strImageType = "JPG"
            lngPos = flgFound + 2
            ExitLoop = false
            do while ExitLoop = False and lngPos < lngSize
               do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
                  lngPos = lngPos + 1
               loop
               if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
                  lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
                  lngPos = lngPos + lngMarkerSize  + 1
               else
                  ExitLoop = True
               end if
           loop
           if ExitLoop = False then
              Width = -1
              Height = -1
              Depth = -1
           else
              Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
              Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
              Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
              gfxSpex = True
           end if                   
        end if
    End Function
    
    Function GetBytes(flnm, offset, bytes)
        Dim objFSO
        Dim objFTemp
        Dim objTextStream
        Dim lngSize
        Dim fsoForReading	 
        on error resume next
        Set objFSO = CreateObject("Scripting.FileSystemObject")  
        Set objFTemp = objFSO.GetFile(flnm)
        lngSize = objFTemp.Size
        set objFTemp = nothing
        fsoForReading = 1
        Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)
        if offset > 0 then
            strBuff = objTextStream.Read(offset - 1)
        end if
        if bytes = -1 then
            GetBytes = objTextStream.Read(lngSize)
        else
            GetBytes = objTextStream.Read(bytes)
        end if
        objTextStream.Close
        set objTextStream = nothing
        set objFSO = nothing
    End Function
    
    Function lngConvert(strTemp)
        lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
    End Function
    
    Function lngConvert2(strTemp)
        lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
    End Function
    %>
    Last edited by rory; Dec 10th, 2006 at 01:57 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Re: Picture Height/Width

    i assume that i would use it like this
    [Highlight=VB]
    <!--#include virtual ="grfxfunctions.asp" -->
    <%
    dim filename, hh, ww, dd
    filename = "/somepic.jpg"
    gfxSpex filename, ww, hh, dd
    response.write("width: " & ww & "<BR>")
    response.write("height: " & hh & "<BR>")
    response.write("depth: " & dd & "<BR>")
    %>
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

  4. #4
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Picture Height/Width

    yep, except you need the full physical path of the image.
    filename = Server.Mappath("./somepic.jpg")

    Code:
    <!--#INCLUDE FILE="grfxfunctions.asp"-->
    <%
    dim filename, hh, ww, dd
    filename = Server.MapPath("./somepic.jpg")
    gfxSpex filename, ww, hh, dd
    response.write("width: " & ww & "<BR>")
    response.write("height: " & hh & "<BR>")
    response.write("depth: " & dd & "<BR>")
    %>

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Re: Picture Height/Width

    ty rory for the help...now i can finally make 3 - 4 site i have half finished
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Picture Height/Width

    cool, good luck

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Re: Picture Height/Width

    ty for then help...i posted this question about a year ago with no resolution.
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Re: Picture Height/Width

    For all of those who are like me and still need help, I have created a simple function to automatically put the graphics into a gallery style 5 by 5 table with next and back buttons. It also has the CCS class of pictable in all tags for 100% customization.

    You use it like a function. the syntax for the function is:

    VB Code:
    1. <!--#INCLUDE FILE="grfx.asp"-->
    2. <%=pictable(MaxHeight, MaxWidth, FileDirectory%>

    Here is the code as well as the file to attach.

    VB Code:
    1. <%
    2. '       pictable Syntax
    3. '                                                            
    4. '       maxh        => Filespec of file to read              
    5. '       maxw        => width of image                        
    6. '       fdir        => height of image                        
    7. '
    8. '       style sheet class = "pictable"
    9. '       Has a CCS in every tag for maximum customization.                                                            
    10. '
    11. '       description:
    12. '       returns a table containing resized images. will work with
    13. '       JPG, PNG, BMP, and GIF. Has Error Handling.
    14. '
    15. '       Example:
    16. '       <%=pictable(150,150,".")%>
    17.  
    18.  
    19. function pictable(maxh, maxw, fdir)
    20.     on error resume next
    21.     if len(maxh) < 1 then
    22.         maxh = 150
    23.     elseif maxh =< 0 then
    24.         maxh = 150
    25.     end if
    26.     if len(maxw) < 1 then
    27.         maxw = 150
    28.     elseif maxw =< 0 then
    29.         maxw = 150
    30.     end if
    31.     if len(fdir) < 1 then fdir = "."
    32.     Page = Request.ServerVariables("URL")  
    33.     Page = mid(Page,InStrRev(Page,"/") + 1)
    34.     skip = int(request.querystring("skip"))
    35.     if len(skip) = 0 then skip = 0
    36.     Set fso =  Server.CreateObject("Scripting.FileSystemObject")
    37.     Set fldr =  fso.GetFolder(Server.MapPath(fdir))
    38.     if err = 0 then
    39.         **** = 0
    40.         t**** = 0
    41.         For Each f in fldr.Files
    42.             if ext(ucase(f.name)) = "JPG" or ext(ucase(f.name)) = "PNG" or ext(ucase(f.name)) = "GIF" or ext(ucase(f.name)) = "BMP" then
    43.                 if t**** = 0 then pictable = pictable & "<table style=""pictable""><tr>"
    44.                 if (skip > 0 and skip > t****) or t**** - skip => 25 then
    45.                     t**** = t**** + 1
    46.                 else
    47.                     filename = Server.MapPath(fdir & "/" & f.name)
    48.                     gfxSpex filename, ww, hh, dd
    49.                     if ww > maxw and hh > maxh then
    50.                         if ww > hh then
    51.                             temp = ww / maxw
    52.                             tempw = maxw
    53.                             temph = hh / temp
    54.                         else
    55.                             temp = hh / maxh
    56.                             temph = maxh
    57.                             tempw = ww / temp
    58.                         end if
    59.                     elseif ww =< maxw and hh > maxh then
    60.                         temp = hh / maxh
    61.                         temph = naxh
    62.                         tempw = ww / temp
    63.                     elseif ww > maxw and hh =< maxh then
    64.                         temp = ww / maxw
    65.                         tempw = maxw
    66.                         temph = hh / temp
    67.                     elseif ww =< maxw and hh =< maxh then
    68.                         tempw = ww
    69.                         temph = hh
    70.                     end if
    71.                     pictable = pictable & "<td style=""pictable"" witdh=""" & maxw & """ align=""center"" valign=""middle""><a style=""pictable"" href=""" & fdir & "/" & f.name & """><img style=""pictable"" src=""" & fdir & "/" & f.name & """ width=""" & tempw & """ height=""" & temph & """></a></td>"
    72.                     t**** = t**** + 1
    73.                     **** = **** + 1
    74.                     if **** = 5 then
    75.                         pictable = pictable & "</tr><tr>"
    76.                         **** = 0
    77.                     end if
    78.                 end if
    79.             else
    80.             end if
    81.         Next
    82.         if (t****) = 0 then
    83.             pictable = pictable & "No Images In Specified Directory"
    84.         else
    85.             pictable = pictable & "</td></tr><tr><td>"
    86.             **** = t**** - skip - 25
    87.             if skip => 25 then
    88.                 pictable = pictable & "<form style=""pictable"" action=""" & page & """ method=""get""><input name=""skip"" type=""hidden"" value=""" & skip-25 & """ /><input style=""pictable"" value=""&lt; Back 25"" type=""submit"" /></form>"
    89.             else
    90.                 pictable = pictable & "&nbsp;"
    91.             end if
    92.             pictable = pictable & "</td><td style=""pictable"" colspan=3>&nbsp;</td><td>"
    93.             if **** > 0 then
    94.                 if **** < 25 then
    95.                     pictable = pictable & "<form style=""pictable"" action=""" & page & """ method=""get""><input name=""skip"" type=""hidden"" value=""" & skip+25 & """ /><input  style=""pictable"" value=""Next " & **** & "&gt;"" type=""submit"" /></form>"
    96.                 else
    97.                     pictable = pictable & "<form style=""pictable"" action=""" & page & """ method=""get""><input name=""skip"" type=""hidden"" value=""" & skip+25 & """ /><input  style=""pictable"" value=""Next 25 &gt;"" type=""submit"" /></form>"
    98.                 end if
    99.             else
    100.                 pictable = pictable & "&nbsp;"
    101.             end if
    102.             pictable = pictable & "</td></tr></table>"
    103.         end if
    104.     elseif err = 76 then
    105.         pictable = pictable & "Directory Doesn't Exist Or Is In Wrong Syntax"
    106.     elseif err = -2147467259 then
    107.         pictable = pictable & ".. Directory Cannot Be Used"
    108.     elseif err = 13 then
    109.         pictable = pictable & "Please enter a MaxHeight and/or MaxWidth"
    110.     else
    111.         pictable = pictable & "Unknown Error #" & err
    112.     end if
    113. end function
    114.  
    115. '       gfxSpex Syntax
    116. '                                                            
    117. '       flnm        => Filespec of file to read              
    118. '       width       => width of image                        
    119. '       height      => height of image                        
    120. '       depth       => color depth (in number of colors)      
    121. '                                                            
    122.  
    123. Function gfxSpex(flnm, width, height, depth)
    124.     dim strType
    125.     dim strImageType
    126.     dim strTarget
    127.     dim strPNG
    128.     dim strGIF
    129.     dim strJPG
    130.     dim strBMP
    131.     dim ExitLoop
    132.     dim strBuff
    133.     dim flgFound
    134.     dim lngMarkerSize
    135.     dim lngSize
    136.     dim lngPos
    137.     strType = ""
    138.     gfxSpex = False
    139.     strPNG = chr(137) & chr(80) & chr(78)
    140.     strGIF = "GIF"
    141.     strJPG  = "JPG"
    142.     strBMP = chr(66) & chr(77)
    143.     strType = GetBytes(flnm, 0, 3)
    144.     if strType = strGIF then   
    145.        strImageType = "GIF"
    146.        Width = lngConvert(GetBytes(flnm, 7, 2))
    147.        Height = lngConvert(GetBytes(flnm, 9, 2))
    148.        Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
    149.        gfxSpex = True
    150.     elseif left(strType, 2) = strBMP then
    151.        strImageType = "BMP"
    152.        Width = lngConvert(GetBytes(flnm, 19, 2))
    153.        Height = lngConvert(GetBytes(flnm, 23, 2))
    154.        Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))
    155.        gfxSpex = True
    156.     elseif strType = strPNG then   
    157.        strImageType = "PNG"
    158.        Width = lngConvert2(GetBytes(flnm, 19, 2))
    159.        Height = lngConvert2(GetBytes(flnm, 23, 2))
    160.        Depth = getBytes(flnm, 25, 2)
    161.        select case asc(right(Depth,1))
    162.            case 0
    163.               Depth = 2 ^ (asc(left(Depth, 1)))
    164.               gfxSpex = True
    165.            case 2
    166.               Depth = 2 ^ (asc(left(Depth, 1)) * 3)
    167.               gfxSpex = True
    168.            case 3
    169.               Depth = 2 ^ (asc(left(Depth, 1)))  '8
    170.               gfxSpex = True
    171.            case 4
    172.               Depth = 2 ^ (asc(left(Depth, 1)) * 2)
    173.               gfxSpex = True
    174.            case 6
    175.               Depth = 2 ^ (asc(left(Depth, 1)) * 4)
    176.               gfxSpex = True
    177.            case else
    178.               Depth = -1
    179.         end select
    180.     else   
    181.         strBuff = GetBytes(flnm, 0, -1)
    182.         lngSize = len(strBuff)
    183.         flgFound = 0
    184.         strTarget = chr(255) & chr(216) & chr(255)
    185.         flgFound = instr(strBuff, strTarget)
    186.         if flgFound = 0 then
    187.            exit Function
    188.         end if
    189.         strImageType = "JPG"
    190.         lngPos = flgFound + 2
    191.         ExitLoop = false
    192.         do while ExitLoop = False and lngPos < lngSize
    193.            do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
    194.               lngPos = lngPos + 1
    195.            loop
    196.            if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
    197.               lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
    198.               lngPos = lngPos + lngMarkerSize  + 1
    199.            else
    200.               ExitLoop = True
    201.            end if
    202.        loop
    203.        if ExitLoop = False then
    204.           Width = -1
    205.           Height = -1
    206.           Depth = -1
    207.        else
    208.           Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
    209.           Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
    210.           Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
    211.           gfxSpex = True
    212.        end if                  
    213.     end if
    214. End Function
    215.  
    216. Function GetBytes(flnm, offset, bytes)
    217.     Dim objFSO
    218.     Dim objFTemp
    219.     Dim objTextStream
    220.     Dim lngSize
    221.     Dim fsoForReading    
    222.     on error resume next
    223.     Set objFSO = CreateObject("Scripting.FileSystemObject")  
    224.     Set objFTemp = objFSO.GetFile(flnm)
    225.     lngSize = objFTemp.Size
    226.     set objFTemp = nothing
    227.     fsoForReading = 1
    228.     Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)
    229.     if offset > 0 then
    230.         strBuff = objTextStream.Read(offset - 1)
    231.     end if
    232.     if bytes = -1 then
    233.         GetBytes = objTextStream.Read(lngSize)
    234.     else
    235.         GetBytes = objTextStream.Read(bytes)
    236.     end if
    237.     objTextStream.Close
    238.     set objTextStream = nothing
    239.     set objFSO = nothing
    240. End Function
    241.  
    242. Function lngConvert(strTemp)
    243.     lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
    244. End Function
    245.  
    246. Function lngConvert2(strTemp)
    247.     lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
    248. End Function
    249.  
    250. Function ext(strTemp)
    251.     ext = mid(strtemp, len(strtemp) - 2, 3)
    252. End Function
    253. %>
    Attached Files Attached Files
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

Posting Permissions

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



Click Here to Expand Forum to Full Width