3초기억력

ASP - LoadPicture 없이도 이미지 파일 확장자, 가로세로 길이 구하기 본문

플밍_ASP

ASP - LoadPicture 없이도 이미지 파일 확장자, 가로세로 길이 구하기

잠수콩 2011. 4. 27. 14:02


제목 : ASP - LoadPicture 없이도 이미지 파일 확장자, 가로세로 길이 구하기


Class ImageClass

	   Private m_Width
	   Private m_Height
	   Private m_ImageType
	   Private BinFile

	   Private BUFFERSIZE
	   Private objStream

	   Private Sub class_initialize()

	   	   BUFFERSIZE = 65535

	   	   ' Set all properties to default values
	   	   m_Width	   = 0
	   	   m_Height	   = 0
	   	   m_Depth	   = 0
	   	   m_ImageType = Null

	   	   Set objStream = Server.CreateObject("ADODB.Stream")

	   End Sub

	   Private Sub class_terminate()

	   	   Set objStream = Nothing

	   End Sub

	   Public Property Get Width()
	   	   Width = m_Width
	   End Property

	   Public Property Get Height()
	   	   Height = m_Height
	   End Property

	   Public Property Get ImageType()
	   	   ImageType = m_ImageType
	   End Property

	   Private Function Mult(lsb, msb)
	   	   Mult = lsb + (msb * CLng(256))
	   End Function

	   Private Function BinToAsc(ipos)
	   	   BinToAsc = AscB(MidB(BinFile, (ipos+1), 1))
	   End Function

	   Public Sub LoadFilePath(strPath)
	   	   If InStr(strPath, ":") = 0 Then
	   	   	   strPath = Server.MapPath(strPath)
	   	   End If

	   	   objStream.Open
	   	   objStream.LoadFromFile(strPath)
	   	   BinFile = objStream.ReadText(-1)

	   End Sub

	   Public Sub LoadBinary(BinaryFile)

	   	   BinFile = BinaryFile

	   End Sub

	   Public Sub ImageRead

	   	   If  BinToAsc(0) = 137 And BinToAsc(1) = 80 And BinToAsc(2) = 78 Then
	   	   	   ' this is a PNG file
	   	   	   m_ImageType = "png"

	   	   	   ' get bit depth
	   	   	   Select Case BinToAsc(25)
	   	   	   	   Case 0
	   	   	   	   ' greyscale
	   	   	   	   	   Depth = BinToAsc(24)
	   	   	   	   Case 2
	   	   	   	   ' RGB encoded
	   	   	   	   	   Depth = BinToAsc(24) * 3
	   	   	   	   Case 3
	   	   	   	   ' Palette based, 8 bpp
	   	   	   	   	   Depth = 8
	   	   	   	   Case 4
	   	   	   	   ' greyscale with alpha
	   	   	   	   	   Depth = BinToAsc(24) * 2
	   	   	   	   Case 6
	   	   	   	   ' RGB encoded with alpha
	   	   	   	   	   Depth = BinToAsc(24) * 4
	   	   	   	   Case Else
	   	   	   	   ' This value is outside of it's normal range, so we'll assume that this is not a valid file
	   	   	   	   	   m_ImageType = Null
	   	   	   End Select

	   	   	   If not IsNull(m_ImageType) Then
	   	   	   	   ' if the image is valid then

	   	   	   	   ' get the width
	   	   	   	   m_Width = Mult(BinToAsc(19), BinToAsc(18))

	   	   	   	   ' get the height
	   	   	   	   m_Height = Mult(BinToAsc(23), BinToAsc(22))
	   	   	   End If
	   	   End If

	   	   If BinToAsc(0) = 71 And BinToAsc(1) = 73 And BinToAsc(2) = 70 Then
	   	   	   ' this is a GIF file
	   	   	   m_ImageType = "gif"

	   	   	   ' get the width
	   	   	   m_Width = Mult(BinToAsc(6), BinToAsc(7))

	   	   	   ' get the height
	   	   	   m_Height = Mult(BinToAsc(8), BinToAsc(9))

	   	   	   ' get bit depth
	   	   	   m_Depth = (BinToAsc(10) And 7) + 1
	   	   End If

	   	   If BinToAsc(0) = 66 And BinToAsc(1) = 77 Then
	   	   	   ' this is a BMP file

	   	   	   m_ImageType = "bmp"

	   	   	   ' get the width
	   	   	   m_Width = Mult(BinToAsc(18), BinToAsc(19))

	                   	    ' get the height
	   	   	   m_Height = Mult(BinToAsc(22), BinToAsc(23))

	   	   	   ' get bit depth
	   	   	   m_Depth = BinToAsc(28)
	   	   End If


	   	   If IsNull(m_ImageType) Then
	   	   	   ' if the file is not one of the above type then
	   	   	   ' check to see if it is a JPEG file
	   	   	   Dim lPos : lPos = 0

	   	   	   Do
	   	   	   	   ' loop through looking for the byte sequence FF,D8,FF
	   	   	   	   ' which marks the begining of a JPEG file
	   	   	   	   ' lPos will be left at the postion of the start
'###########################요기 엔터값 없애야함
If (BinToAsc(lPos) = &HFF And BinToAsc(lPos + 1) = &HD8 And
BinToAsc(lPos + 2) = &HFF) Or (lPos >= BUFFERSIZE - 10) Then Exit Do ' move our pointer up lPos = lPos + 1 ' and continue Loop lPos = lPos + 2 If lPos >= BUFFERSIZE - 10 Then Exit Sub Do ' loop through the markers until we find the one ' starting with FF,C0 which is the block containing the ' image information Do ' loop until we find the beginning of the next marker If BinToAsc(lPos) = &HFF And BinToAsc(lPos + 1) <> &HFF Then Exit Do lPos = lPos + 1 If lPos >= BUFFERSIZE - 10 Then Exit Sub Loop ' move pointer up lPos = lPos + 1
'###########################요기 엔터값 없애야함 If (BinToAsc(lPos) >= &HC0 And BinToAsc(lPos) <= &HC3) Or
(BinToAsc(lPos) >= &HC5 And BinToAsc(lPos) <= &HC7) Or
(BinToAsc(lPos) >= &HC9 And BinToAsc(lPos) <= &HCB) Or
(BinToAsc(lPos) >= &HCD And BinToAsc(lPos) <= &HCF) Then Exit Do End If ' otherwise keep looking lPos = lPos + Mult(BinToAsc(lPos + 2), BinToAsc(lPos + 1)) ' check for end of buffer If lPos >= BUFFERSIZE - 10 Then Exit Sub Loop ' If we've gotten this far it is a JPEG and we are ready ' to grab the information. m_ImageType = "jpg" ' get the height m_Height = Mult(BinToAsc(lPos + 5), BinToAsc(lPos + 4)) ' get the width m_Width = Mult(BinToAsc(lPos + 7), BinToAsc(lPos + 6)) ' get the color depth m_Depth = BinToAsc(lPos + 8) * 8 End If End Sub End Class 사용법입니다. Dim Image, iType, iWidth, iHeight, FileSize Set Image = new ImageClass With Image    .LoadFilePath("가상경로 or 물리적 경로 어떤것을 입력하던 관계없습니다.")
''''''''''''''''''''''''.LoadBinary("바이너리로 읽었을때...")
   .ImageRead iType = .ImageType iWidth = .Width iHeight = .Height End With
FileSize = iWidth & "," & iHeight & "," & iType Set Image = Nothing


내용 : LoadPicture 없이도 파일 확장자 , 가로세로 길이 구하는 클래스 - 퍼옴

퍼온 스크립트에 오타 발견, 수정함 


약간 수정하여 실제 페이지에서 사용하게 변경함 




test.asp
- 가로 길이가 400이 넘는 이미지는 400으로 제한, 그 이하는 그냥 그대로...
<%
    '이미지 가로,세로 최고 길이
    img_width = "400"
    img_height = "400"
 img_url = "\upload\file\test.jpg"

 Set ImageFile = new ImageClass
  With ImageFile
   .LoadFilePath(img_url)
   .ImageRead
   iType = .ImageType
   iWidth = .Width
   iHeight = .Height
 End With
 ImageFileInfo = iWidth & "," & iHeight
 Set ImageFile = Nothing

  Filesize = Split(ImageFileInfo, ",")
  OriFileWidth = CInt(Filesize(0))
  OriFileHeight = CInt(Filesize(1))
%>
<img src="<%=img_url%>" width="<% If CInt(OriFileWidth)>CInt(img_width) Then response.write img_width Else response.write OriFileWidth End If %>">
Comments