Chapter 7: Image Handling
Contents
7.1 Image Size and Type Extraction
AspUpload is capable of determining the type and pixel size of uploaded images. The formats currently supported are GIF, JPEG, BMP, PNG and WEBP (the latter is supported as of version 3.1.0.4). The type and size information is returned via the properties File.ImageType, File.ImageWidth, and File.ImageHeight.
The property File.ImageType returns the strings "GIF", "JPG", "BMP", "PNG", "WEBP" (for GIF, JPEG, BMP, PNG and WEBP images, respectively), or "UNKNOWN" if the file is not an image or of an unknown image type.
The properties File.ImageWidth and File.ImageHeight return the image width and height in pixels, or 0 if the file is not an image.
The code samples size.asp and size_upload.asp demonstrate the usage of these properties.
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' Capture and save uploaded image
Upload.Save "c:\upload"
For Each File in Upload.Files
If File.ImageType = "UNKNOWN" Then
Response.Write "This is not an image."
Response.End
End If
Response.Write File.Path & "<BR>"
Response.Write "Type: " & File.ImageType & "<BR>"
Response.Write "Size: " & File.ImageWidth & " x " & File.ImageHeight & " pixels"
Next
%>
</BODY>
</HTML>
Click the link below to run this code sample:
7.2 Image Resizing
AspUpload can be used in conjunction with another Persits Software component, AspJpeg, to create resized versions and thumbnails of JPEG images. An evaluation copy of AspJpeg can be downloaded here.
The code samples resize.asp and resize_upload.asp demonstrate how AspJpeg can be used to resize a newly uploaded JPEG image. The uploaded and resized images are saved in the same directory as the ASP scripts, rather than the usual c:\upload folder, to be able to display both images in the browser easily.
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' Use AspJpeg to resize image
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Capture and save uploaded image to the same directory as script
Upload.Save Server.MapPath(".")
For Each File in Upload.Files
If File.ImageType <> "JPG" Then
Response.Write "This is not a JPGE image."
File.Delete
Response.End
End If
Jpeg.Open File.Path
Scale = Upload.Form("Scale")
Jpeg.Width = Jpeg.OriginalWidth * Scale / 100
Jpeg.Height = Jpeg.OriginalHeight * Scale / 100
Response.Write Jpeg.OriginalHeight & "-" & Jpeg.Height
Jpeg.Save Server.MapPath(".") & "\small_" & File.FileName
%>
<IMG SRC="<% = File.FileName %>"><BR>
<IMG SRC="<% = "small_" & File.FileName %>"><P>
<%
Next
%>
</BODY>
</HTML>
Click the link below to run this code sample:
AspJpeg comes with many more code samples. A live demo is also available.