Print Page | Close Window

Needs Help With Restricting Image Sizes

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=23150
Printed Date: 28 March 2026 at 11:07pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Needs Help With Restricting Image Sizes
Posted By: Misty
Subject: Needs Help With Restricting Image Sizes
Date Posted: 20 April 2007 at 8:20am
I am using ASPUpload ( http://www.aspjpeg.com/manual_03.html - http://www.aspjpeg.com/manual_03.html )
 
My code below works fine. But I would like to add two restrictions: 1.) Not allowing users to upload an image that is larger than 50 KB in size. 2.) Not allowing users to upload image file names that have spaces between file names (ex: house 001.jpg). The file names need to be house001.jpg. Can someone please help me to add those conditions to the code below?
 
 
<%

' Create an instance of AspUpload object

Set Upload = Server.CreateObject("Persits.Upload")

' Compute path to save uploaded files to

Path = "d:\inetpub\wwwroot\"

 

 

' Capture uploaded file. Return the number of files uploaded

Count = Upload.Save(Path)

 

 

If Count = 0 Then

  Response.Write "No images selected."

  Response.End

Else

  ' Obtain File object representing uploaded file

  Set File = Upload.Files(1)

 

  ' Is this a valid image file?

  If File.ImageType <> "UNKNOWN" Then

 

    ' Create instance of AspJpeg object

    Set jpeg = Server.CreateObject("Persits.Jpeg")

     

     

     

 

    ' Open uploaded file

    jpeg.Open( File.Path )

 

    ' Resize image according to "scale" option.

    ' We cannot use Request.Form, so we use Upload.Form instead.

 

 

jpeg.Width = 160

jpeg.Height = 120

 

    SavePath = Path & "\small_" & File.ExtractFileName

 

    ' AspJpeg always generates thumbnails in JPEG format.

    ' If the original file was not a JPEG, append .JPG ext.

    If UCase(Right(SavePath, 3)) <> "JPG" Then

      SavePath = SavePath & ".jpg"

    End If

 

   




Replies:
Posted By: Brolin99
Date Posted: 22 April 2007 at 8:35am
Hi Misty,
 
I have had a similar problem in the past with images that have spaces in them. What I generally do is provide a naming convention for all images when I save them, and use a random number generator to give them a unique file name.
 
For example, news images on a site I have worked on get renamed to be something like "news-image-RANDOMNUMBER.jpg"
 
On your script, I would suggest changing:
 
SavePath = Path & "\small_" & File.ExtractFileName
 
to be:
 
UpperBound = 100000000
LowerBound = 1
Randomize 'Starts the random generator
RandomNumber = Int(Rnd * UpperBound) + LowerBound
 
SavePath = Path & "\small_" & RandomNumber & ".jpg"
 
This will return a file called "small_(Random number between 1 and 1000000000 here).jpg"
 
An easy way around the file size issue, it simply to get ASPJpeg to do a check on the width and height of the image - as that is usually a good guage of the image size anyway. For example:
 
If Jpeg.Width > 300 Then
Response.Redirect("error.asp")
End If
 
Hope that helps!
 
xxx
 
 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net