Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Needs Help With Restricting Image Sizes
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Needs Help With Restricting Image Sizes

 Post Reply Post Reply
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Needs Help With Restricting Image Sizes
    Posted: 20 April 2007 at 8:20am
 
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

 

   

Back to Top
Brolin99 View Drop Down
Groupie
Groupie
Avatar

Joined: 03 May 2003
Location: New Zealand
Status: Offline
Points: 58
Post Options Post Options   Thanks (0) Thanks(0)   Quote Brolin99 Quote  Post ReplyReply Direct Link To This Post 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
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.