Uploading Images using HTML and ASP: Decoding the Upload/Manipulating Image
Home
/ Developer's Corner
/ upload
Uploading Images using HTML and ASP: Decoding the Upload/Manipulating Image
Decoding the Upload/Manipulating the Image
Uploading Images using HTML and ASP
Decoding the Upload
In which our server grabs the image out of
the data it's been sent.
From the ASP side we need to get the picture
file out of the data we receive. Using ImageGoo the code to do this is
pretty straightforward. We have already set the file 'photoupload.asp' as
our form action. In this we include the following
code.
Set mp =
Server.CreateObject("ImageGoo.XMultipart")
data = Request.BinaryRead(Request.TotalBytes)
mp.SetData(data)
Set field1 = mp.Item("filefield")
Set field2 = mp.Item("textfield")
That's all there is to it! We start off by creating the
object we're going to be using (the ImageGoo XMultipart object). We then
read the raw data that's been sent to us and put it into the object.
To get the fields out we just ask for the Item with the same name as the
form field. So we ask for the field called 'filefield' and then for the
field called 'textfield'. Each of these is an object that holds everything
we need to know about the contents of each field. We'll keep these field
objects handy for later on.
Manipulating the Image
In which ImageGoo makes the picture all
respectable.
Who knows what the image we have is like? It's
probably the wrong size and shape. It's probably the wrong format. The only
thing you can guarantee for sure is that it's not going to look good if we
just insert it into our web page. So let's get ImageGoo to turn it into
something more respectable.
First the size. We have a fixed amount of space for our image. Generally
the height doesn't matter too much because a tall image will just push
other content down the page, however the width does. If the image is too
wide you won't be able to see all of it. If the image is too narrow your
page will look plain silly and tables may slip into the space that's been
left.
To keep things simple let's make the width fixed. If the image is too
big we'll scale it down. If it's too small we won't scale it up (because it
would result in horrible pixellation) but we'll center it. To do this we
use the following code.
Set canvas =
Server.CreateObject("ImageGoo.Canvas")
canvas.Width = 290
rect = canvas.DrawData(field1.Data, field1.Filename, "Fit=True
VAlign=middle HAlign=middle")