ASP klasik tidak mempunyai library terintegrasi untuk melakukan perubahan pada gambar. Sebagian library yang bagus di internet adalah library berbayar, misalkan aspjpeg dan ASPThumb.

Yang akan kita coba lakukan adalah menggunakan CxImage ActiveX wrapper, library yang bagus untuk manipulasi gambar khususnya untuk pemrograman ASP.

Dengan ini kita membuat contoh implementasi (1) untuk meresize gambar on the fly untuk langsung ditampilkan di-browser atau sebagai properti src dari tag img dan (2) meng-generate gambar baru dengan me-resize gambar lama kemudian menyimpan sebagai gambar baru di server, misalnya apabila membutuhkan untuk menampilkan gambar dengan berbagai ukuran di website dari sebuah gambar yang sudah di-upload.

Yang harus dilakukan pertama kali adalah mendownload library dari http://cximageatl.codeplex.com/, kemudian diinstall di komputer webserver, sehingga asp yang berjalan di IIS bisa mengambil library tersebut. Setelah itu tinggal jalankan beberapa skrip berikut dengan gambar yang ada di server.

Membuat Kumpulan Fungsi

<%
 
'om4g.us
'Function dengan menggunaa libary CxImage ActiveX wrapper
'http://cximageatl.codeplex.com/
 
'ATL-COM object that is a wrapper of the CxImage class of Davide Pizzolato. CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images. The ActiveX wrapper object can be used to create thumbnails in web sites.
 
'VFilePath: Virtual path of the original image. The only mandatory parameter. If the file specified does not exist or is not an image, a "no thumbnail" default image is generated from the NoThumb.gif file.
'Width, Height: Desired thumbnail size. If not given, it is read from appSettings; if not specified in appSettings, defaults to 150x150.
'Quality: JPEG quality parameter. Ranges from 1 (lowest quality) to 99 (highest quality). Defaults to 75.
'AllowStretch: Set to 'True' to allow stretching of thumbnail to fit the above size. If not given, defaults to False.
'ShellThumbnails: If not specified or set to 'False', the thumbnail is generated using the CxImage wrapper object implemented in CxImageATL.dll (ProgID='CxImageATL.CxImage'). If set to 'True', the thumbnail is generated using the COM object implemented in ThumbExtract.dll (ProgID='ThumbExtract.FileThumbExtract'). The latter object exploits the shell interface (IExtractImage) responsible for generating thumbnails when you click a file or select the Thumbnail view in Explorer. This allows you to create thumbnail views for various file types, e.g., PowerPoint presentations, like in the following figure.
 
Function createThumb(VFilePath, Width, Height, Fast, Quality, bStretch)
	Dim BinData
	FilePath = Server.MapPath(VFilePath)
 
	Set objCxImage = Server.CreateObject("CxImageATL.CxImage")
	Call objCxImage.Load(FilePath,GetFileType(FilePath))
	Call objCxImage.IncreaseBpp(24)
 
	If Not bStretch Then
	widthOrig = CDbl(objCxImage.GetWidth())
	heightOrig = CDbl(objCxImage.GetHeight())
	fx = widthOrig/Width
	fy = heightOrig/Height 
 
	If fx>fy Then f=fx Else f=fy  ' Max(fx,fy)
	If f<1 Then f=1
	widthTh = Int(widthOrig/f)
	heightTh = Int(heightOrig/f)
	Else
	widthTh = Width
	heightTh = Height
	End If
	Call objCxImage.Resample(widthTh,heightTh,Fast)
	BinData = objCxImage.ImageForASP(3,Quality)
 
	createThumb = BinData
 
End Function
 
Function createFile(VFilePath, Result, Width, Height, Fast, Quality, bStretch)
	BinData = createThumb(VFilePath, Width, Height, Fast, Quality, bStretch)
 
	Set ofs = Server.CreateObject("Scripting.FileSystemObject")
 
	Set oFile = ofs.CreateTextFile( Server.MapPath(Result), True)
	For nIndex = 1 to LenB(BinData)
		oFile.Write Chr(AscB(MidB(BinData,nIndex,1)))
	Next
 
	Set fso = nothing
 
End Function
 
Sub showThumb(VFilePath, Width, Height, Fast, Quality, bStretch)
	Response.ContentType = "image/jpeg"
 
	BinData = createThumb(VFilePath, Width, Height, Fast, Quality, bStretch)
	Response.BinaryWrite BinData
 
End Sub
 
Function GetFileType(sFile)
  dot = InStrRev(sFile, ".")
  filetype=2
  If dot > 0 Then sExt = LCase(Mid(sFile, dot + 1, 3))
  If sExt = "bmp" Then filetype = 1
  If sExt = "wmf" Then filetype = 1
  If sExt = "raw" Then filetype = 1
  If sExt = "gif" Then filetype = 2
  If sExt = "jpg" Then filetype = 3
  If sExt = "png" Then filetype = 4
  If sExt = "ico" Then filetype = 5
  If sExt = "tif" Then filetype = 6
  If sExt = "tga" Then filetype = 7
  If sExt = "pcx" Then filetype = 8
  If sExt = "jp2" Then filetype = 11
  If sExt = "jpc" Then filetype = 12
  If sExt = "jpx" Then filetype = 13
  If sExt = "pnm" Then filetype = 14
  If sExt = "ras" Then filetype = 15
  GetFileType=filetype
End Function
 
'showThumb "1.png", 90, 90, 0, 75, false
'createFile "2.jpg", "hasil.jpg", 90, 90, 0, 75, false
%>

Contoh me-resize gambar

<!--#include file="t2f.asp"-->
 
&lt;% showThumb "1.png", 90, 90, 0, 75, false %&gt;

Contoh meng-generate gambar thumbnail

<!--#include file="t2f.asp"-->
&lt;% createFile "2.jpg", "hasil.jpg", 90, 90, 0, 75, false %&gt;