Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 29th, 2004, 02:13 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default check if file is an image file

Hi folks,

Is there any method to check whether a given file is an IMAGE file ??

Sudhan.

__________________
--------------------------------------------------
Sudhan Kanitkar.
Everything Is In Our Hands.
Reply With Quote
  #2 (permalink)  
Old June 29th, 2004, 02:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Sudhan,

You can check with FileSystemObject properties and methods.

You can first check if the file exists, then there is a method which takes filename with exact path as parameter, using which you can get its extension name alone and compare it agains jpg/jpeg/gif/bmp... and proceed further., if not able to get the extension name, you can use mid() or split() to extract the extension and that way you can find if that is an image file.

But remember, I may even rename the .doc file as .jpg, which you can't check if that is a real valid image.

Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #3 (permalink)  
Old June 29th, 2004, 02:34 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi Vijay,

I had thought about the rename thing and hence i am looking for some inbuilt method on this ...

my earlier plan was the same one use the Right(filename,3) to extract the extention and then do the comparision .. Only thing that comes in the way is the rename thing ...

Well If you find something let me know ....

Sudhan.

Reply With Quote
  #4 (permalink)  
Old June 29th, 2004, 02:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

I have done an intranet site long long ago, where it deals with displaying all documents and with search facility too. It was database driven, having stored the path of images, catagorised etc... During when I too had the same thought, but did not want to break my head on it, as the upload everything has to be approved by a person and after checking the documnets validity, it was he/she who upload the document to the right folder and there by makes entry of its path into the DB.

So on that situation, you can always have faith in person who does that upload job, and so I wasn't really pushed hard to check on the RENAME thing. But logically, how can one check that if it is a valid .doc/.jpg/.xls file, it is to be interpretted by the application that is able to open the file for us. So I don't really want to conclude that one cannot check that. If at all there is a way to find that, let me know. This should be a question to be thrown open and lets see what the answer comes up.

Still I don't say, give up, lets find if thats possible.

Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #5 (permalink)  
Old June 29th, 2004, 02:46 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cochin, Kerala, India.
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

Dear Sudhan,

This code was found at http://www.4guysfromrolla.com/ and is very useful.

I have used it as an include file and it worked successfully in many projects.

<%
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: :::
  '::: This routine will attempt to identify any filespec passed :::
  '::: as a graphic file (regardless of the extension). This will :::
  '::: work with BMP, GIF, JPG and PNG files. :::
  '::: :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: Based on ideas presented by David Crowell :::
  '::: (credit where due) :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
  '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
  '::: blah blah Copyright *c* MM, Mike Shaffer blah blah :::
  '::: blah blah ALL RIGHTS RESERVED WORLDWIDE blah blah :::
  '::: blah blah Permission is granted to use this code blah blah :::
  '::: blah blah in your projects, as long as this blah blah :::
  '::: blah blah copyright notice is included blah blah :::
  '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
  '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::

  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: :::
  '::: This function gets a specified number of bytes from any :::
  '::: file, starting at the offset (base 1) :::
  '::: :::
  '::: Passed: :::
  '::: flnm => Filespec of file to read :::
  '::: offset => Offset at which to start reading :::
  '::: bytes => How many bytes to read :::
  '::: :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  function GetBytes(flnm, offset, bytes)

     Dim objFSO
     Dim objFTemp
     Dim objTextStream
     Dim lngSize

     on error resume next

     Set objFSO = CreateObject("Scripting.FileSystemObject")

     ' First, we get the filesize
     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 ' Get All!

        GetBytes = objTextStream.Read(lngSize) 'ReadAll

     else

        GetBytes = objTextStream.Read(bytes)

     end if

     objTextStream.Close
     set objTextStream = nothing
     set objFSO = nothing

  end function


  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: :::
  '::: Functions to convert two bytes to a numeric value (long) :::
  '::: (both little-endian and big-endian) :::
  '::: :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  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


  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  '::: :::
  '::: This function does most of the real work. It will attempt :::
  '::: to read any file, regardless of the extension, and will :::
  '::: identify if it is a graphical image. :::
  '::: :::
  '::: Passed: :::
  '::: flnm => Filespec of file to read :::
  '::: width => width of image :::
  '::: height => height of image :::
  '::: depth => color depth (in number of colors) :::
  '::: strImageType=> type of image (e.g. GIF, BMP, etc.) :::
  '::: :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::
  function gfxSpex(flnm, width, height, depth, strImageType)

     dim strPNG
     dim strGIF
     dim strBMP
     dim strType
     strType = ""
     strImageType = "(unknown)"

     gfxSpex = False

     strPNG = chr(137) & chr(80) & chr(78)
     strGIF = "GIF"
     strBMP = chr(66) & chr(77)

     strType = GetBytes(flnm, 0, 3)

     if strType = strGIF then ' is GIF

        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 ' is BMP

        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 ' Is PNG

        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) ' Get all bytes from file
        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



  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::
  '::: Test Harness :::
  '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::

  ' To test, we'll just try to show all files with a .GIF extension in the root of C:

  'Set objFSO = CreateObject("Scripting.FileSystemObject")
  'Set objF = objFSO.GetFolder("c:\")
  'Set objFC = objF.Files

  'response.write "<table border=""0"" cellpadding=""5"">"

  'For Each f1 in objFC
  ' if instr(ucase(f1.Name), ".GIF") then
  ' response.write "<tr><td>" & f1.name & "</td><td>" & f1.DateCreated & "</td><td>" & f1.Size & "</td><td>"

   ' if gfxSpex(f1.Path, w, h, c, strType) = true then
   ' response.write w & " x " & h & " " & c & " colors"
  ' else
  ' response.write "&nbsp;"
  ' end if

  ' response.write "</td></tr>"

  ' end if

  'Next

  'response.write "</table>"

  'set objFC = nothing
  'set objF = nothing
  'set objFSO = nothing
%>

Reply With Quote
  #6 (permalink)  
Old June 29th, 2004, 02:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Madhu,

That looks cool!!. But, should have posted the URL, than posting the long code here:)

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #7 (permalink)  
Old June 29th, 2004, 02:55 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi Vijay,

even i am not forced to do the check .. and even in my case this is an intranet site and only one user the ADMINISTRATOR can change the images and even i have stored the images in a database ...

so ADMIN has no idea that i am putting such kind of validation ... but i think that the application should be as complete as possible and i have still got 2 days to work on this with only one other task about CONVERTING PPT TO JPG as i asked in one of the other POST and hence i am trying to do it if possible ..

i will ask you and tell you if i find any clues or anything

Sudhan.

Reply With Quote
  #8 (permalink)  
Old June 29th, 2004, 04:00 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi Madhu,

Thats great,

Although i didnt understand most of the CODE i tried it out ... it works great ...
I think this will solve my problem ...

thanks,

Sudhan.

Reply With Quote
  #9 (permalink)  
Old June 29th, 2004, 04:25 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cochin, Kerala, India.
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

Sorry guys for a long post. But I could not trace the exact path to the particular code on that site. The code I got around 3 years back. I took it from my local repository.
Reply With Quote
  #10 (permalink)  
Old June 29th, 2004, 04:43 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi Madhu,

that is excellent repository .. i should say ...

Its perfectly what i want .. and its working in exactly the way i want it to ...

thanks.

Sudhan.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check a file status? tinywizard Visual Basic 2005 Basics 1 August 6th, 2008 09:07 AM
How can I save Crystal Report File to Image File? Muhammad Akbar Crystal Reports 0 July 10th, 2007 05:33 PM
displaying an image from file stream into an image sanjeet ADO.NET 0 September 23rd, 2005 10:28 AM
passing an image file to a .net file. sanjeet .NET Web Services 2 September 22nd, 2005 01:26 PM
check for file existance sentme_mail VB How-To 7 February 22nd, 2005 08:06 AM



All times are GMT -4. The time now is 04:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.