O AspJpeg é um componente adicionado ao ASP que permite a manipulação de imagens GIF, JPEG e PNG através de linhas de comando em sua programação ASP.
Abaixo, segue um exemplo de como pode ser usado o componente em linguagem ASP, a partir de um formulário de Upload. Informações sobre o componente você encontra no site: www.aspjpeg.com. O Manual completo pode ser encontrado em www.aspjpeg.com/manual.html
Esse exemplo será formado por 4 arquivos. Daremos a eles o nomes de pick_jpeg.asp, upload_jpeg.asp, send_binary.asp e thumbnail.asp.
O primeiro arquivo é o pick_jpeg.asp. Através dele será feito o upload da imagem de sua máquina para o seu site.
Código pick_jpeg.asp :
<html>
<head>
<title>CreativeHost - AspJpeg</title>
</head>
<body>
<table>
<tr>
<td>
<h3>AspJpeg</h3>
<font size="2"><b>Instruções:</b>Selecione uma imagem em seu computador, que você gostaria de criar thumbnails. Faça o upload para o Servidor através do formulário abaixo. Assim que o upload da imagem estiver concluído, você terá várias opções de redimensionamento disponíveis.</font>
<form action="upload_jpeg.asp" method="post" enctype="multipart/form-data">
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<td bgcolor="#E0FFF0" align="center">
<font face="Arial" size="2" color="#000000"><b>Selecione uma imagem</b></font>
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE">
<input type="file" NAME="myFile" size="40">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE">
<input type="submit" value="Publicar Imagem">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
O próximo arquivo é o upload_jpeg.asp, encarregado de verificar se a imagem está no formato e resolução correta, para liberar a opção de modificação no tamanho da imagem. Esse arquivo exibe também o caminho dessa imagem, tamanho (em bytes) e dimensão (em pixels).
Código upload_jpeg.asp:
<html>
<head>
<title>CreativeHost - JPEG Upload</title>
</head>
<body bgcolor="#FFFFFF">
<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = False
Upload.SetMaxSize 3000000, true
On Error Resume Next
Count = Upload.Save("D:\web\seulogin\www\diretorio\upload")
If Err <> 0 or Count = 0 Then
%>
<font size="3" face="Arial" color="#FF0000"><b><% If Err <> 0 Then Response.Write "Um erro ocorreu" & Err.Description Else Response.Write "Nada foi enviado."%></b></font>
<p>
<font size="2" face="Arial"><a href="pick_jpeg.asp">Tente de novo</a>.</font>
<%
Else
On Error Goto 0
Set File = Upload.Files(1)
If File.Imagetype = "UNKNOWN" Then
File.Delete
%>
<font size="3" face="Arial" color="#FF0000"><b>Esta não é uma imagem válida</b></font>
<p>
<font size="3" face="Arial"><a href="pick_jpeg.asp"><font size="2">Tente de novo</font></a>.</font>
<% Else
%>
<font size="2" face="Arial">
<b>A seguinte imagem <% = File.Imagetype%> foi enviada:</b>
<p>
<table cellspacing="0" cellpadding="2" border="1">
<tr>
<td>
<font size="2" face="Arial"><b>Path:</b></font>
</td>
<td>
<font size="2" face="Arial"><% = File.OriginalPath %></font>
</td>
</tr>
<tr>
<td>
<font size="2" face="Arial"><b>Size:</b></font>
</td>
<td>
<font size="2" face="Arial"><% = File.Size %> bytes</font>
</td>
</tr>
<tr>
<td>
<font size="2" face="Arial"><b>Dimensions:</b></font>
</td>
<td>
<font size="2" face="Arial"><% = File.ImageWidth %> x <% = File.ImageHeight %> pixels</font>
</td>
</tr>
</table>
<p>
<form action="thumbnail.asp" method="get">
<input type="hidden" name="Path" value="<% = File.Path%>">
<input type="hidden" name="Height" value="<% = File.ImageHeight %>">
<input type="hidden" name="Width" value="<% = File.ImageWidth %>">
<input type="hidden" name="scale" value="50">
<input type="hidden" name="NewWidth" value="100">
<input type="hidden" name="NewHeight" value="100">
<input type="hidden" name="ResizeOption" value="1">
<input type="hidden" name="Quality" value="1">
<input type="hidden" name="Sharpen" value="">
<input type="hidden" name="Sharpenvalue" value="130">
<input type="hidden" name="Rotate" value="0">
<input type="submit" value="Continue...">
</form>
<p>
<img src="/diretorio/<% = File.ExtractFilename %>">
<p>
<font size="2" face="Arial"><a href="pick_jpeg.asp">Enviar outra imagem</a></font>
</font>
<%
End If
End If
%>
</body>
</html>
, altere o nome "seulogin" para o login de seu domínio. Altere também o local onde essas imagens ficarão armazenadas.
Na linha
<img src="/diretorio/<% = File.ExtractFilename %>">
informe o mesmo caminho utilizado na alteração feita acima.
O arquivo seguinte é send_binary.asp. Esse arquivo não deve ser alterado.
Código send_binary.asp :
<%
Response.Expires = 0
' create instance of AspJpeg
Set jpg = Server.CreateObject("Persits.Jpeg")
' Open source file
jpg.Open( Request("path") )
' Set resizing algorithm
jpg.Interpolation = Request("Interpolation")
' Set new height and width
jpg.Width = Request("Width")
jpg.Height = Request("Height")
' Sharpen resultant image
If Request("Sharpen") <> "0" Then
jpg.Sharpen 1, Request("SharpenValue")
End If
' Rotate if necessary. Only available in version 1.2
If Request("Rotate") = 1 Then jpg.RotateL
If Request("Rotate") = 2 Then jpg.RotateR
' Perform resizing and
' send resultant image to client browser
jpg.SendBinary
%>
Finalizando, o último arquivo, thumbnail.asp. Este arquivo terá a função de gerenciar o redimensionamento das imagens de acordo com sua necessidade.
Código thumbnail.asp:
<html>
<head>
<title>CreativeHost - Crie Thumbnails</title>
</head>
<body bgcolor="#FFFFFF">
<font face="Arial" size=2"><b>A dimensão original da imagem é <% = Request("Width") %> x <% = Request("Height") %></b></font>
<p>
<font size="2" face="Arial"><a href="pick_jpeg.asp">Enviar outra imagem</a></font>
<p>
<form action="thumbnail.asp" method="post" name="Mainform">
<input type="hidden" name="Path" value="<% = Request("Path") %>">
<input type="hidden" name="Width" value="<% = Request("Width") %>">
<input type="hidden" name="Height" value="<% = Request("Height") %>">
<table bgcolor="#E0FFF0" border="1" cellspacing="0" cellpadding="2">
<tr>
<td>
<input type="radio" name="Resizeoption" value="1" <% If Request("Resizeoption") = "1" Then Response.Write "checked" %>><font size="2" face="Arial">Scale to</font>
</td>
<td>
<select name="scale" onfocus="document.Mainform.Resizeoption[0].checked = true">
<option value="200" <% If Request("scale") = "200" Then Response.Write "selectED" %>>200%
<option value="80" <% If Request("scale") = "80" Then Response.Write "selectED" %>>80%
<option value="50" <% If Request("scale") = "50" Then Response.Write "selectED" %>>50%
<option value="25" <% If Request("scale") = "25" Then Response.Write "selectED" %>>25%
<option value="10" <% If Request("scale") = "10" Then Response.Write "selectED" %>>10%
</select>
</td>
</tr>
<tr>
<td>
<input type="radio" name="Resizeoption" value="2" <% If Request("Resizeoption") = "2" Then Response.Write "checked" %>><font size="2" face="Arial">Set width to</font>
</td>
<td>
<input type="text" name="NewWidth" size="5" value="<% = Request("NewWidth")%>" onfocus="document.Mainform.Resizeoption[1].checked = true"> <font size="2" face="Arial">pixels, preserve Width/Height ratio</font>
</td>
</tr>
<tr>
<td>
<input type="radio" name="Resizeoption" value="3" <% If Request("Resizeoption") = "3" Then Response.Write "checked" %>><font size="2" face="Arial">Set height to</font>
</td>
<td>
<input type="text" name="NewHeight" size="5" value="<% = Request("NewHeight")%>" onfocus="document.Mainform.Resizeoption[2].checked = true"> <font size="2" face="Arial">pixels, preserve Width/Height ratio</font>
</td>
</tr>
<tr>
<td colspan="1">
<input type="checkbox" name="Quality" <% If Request("Quality") <> "" Then Response.Write "checked" %>><font size="2" face="Arial">High Quality</font>
</td>
<td colspan="1">
<input type="checkbox" name="Sharpen" <% If Request("Sharpen") <> "" Then Response.Write "checked" %>>
<font size="2" face="Arial">Sharpen at</font>
<input type="text" size="4" name="Sharpenvalue" value="<% = Request("Sharpenvalue")%>" onfocus="document.Mainform.Sharpen.checked = true">
<font size="2" face="Arial">% (must be > 100)</font>
</td>
</tr>
<!-- this will be implemented with version 1.2+-->
<tr>
<td colspan="2" align="center">
<font size="2" face="Arial">
<input type="radio" name="Rotate" value="1" <% If Request("Rotate") = "1" Then Response.Write "checked" %>>
Rotate left
<input type="radio" name="Rotate" value="0" <% If Request("Rotate") = "0" Then Response.Write "checked" %>>
No rotation
<input type="radio" name="Rotate" value="2" <% If Request("Rotate") = "2" Then Response.Write "checked" %>>
Rotate right</font>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="create" value="Create Thumbnail">
</td>
</tr>
</table>
</form>
<p>
<%
If Request("Create") <> "" Then
Path = Request("Path")
If Request("Quality") <> "" Then
Interpolation = 1 ' use Bilinear interpolation
Else
Interpolation = 0 ' use Nearest-neighbor algorithm
End If
If Request("Sharpen") <> "" Then
Sharpen = "1"
Sharpenvalue = Request("Sharpenvalue")
If Sharpenvalue <= 100 Then
Response.Write "<font face=""Arial""><b>Sharpening value must be greater than 100</b></font>"
Response.End
End If
Else
Sharpen = "0"
End If
' resize according to user selection
' Percentage scaling
If Request("Resizeoption") = 1 Then
Scale = Request("scale") / 100
Height = Request("Height") * Scale
Width = Request("Width") * Scale
End If
' user-specified width
If Request("Resizeoption") = 2 Then
If IsNumeric(Request("NewWidth")) Then Width = Request("NewWidth") Else Width = 0
If Width > 0 and Width < 2000 Then
Height = Request("Height") * Width / Request("Width")
Else
Response.Write "<font face=""Arial""><b>Invalid Width value</b></font>"
Response.End
End If
End If
' user-specified height
If Request("Resizeoption") = 3 Then
If IsNumeric(Request("NewHeight")) Then Height = Request("NewHeight") Else Height = 0
If Height > 0 and Height < 2000 Then
Width = Request("Width") * Height / Request("Height")
Else
Response.Write "<font face=""Arial""><b>Invalid Height value</b></font>"
Response.End
End If
End If
Rotate = Request("Rotate")
' Display image
%>
<img src="send_binary.asp?Path=<% = Server.URLEncode(Path) %>&Width=<% = Width%>&Height=<% = Height %>&Interpolation=<% = Interpolation %>&sharpen=<% = Sharpen %>&sharpenvalue=<% = Sharpenvalue%>&Rotate=<% = Rotate %>">
<%
End If
%>
</body>
</html>