Page 1 of 8 123 ... Last
  • Jump to page:
    #1
  1. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925

    Image Resize Part II - resize image on the fly


    Attached is aspx file that can resize given image on the fly to the
    desired width and/or height.

    Credits for the code go to GunBlade who developed the core logic.

    To use the code and resize image, follow those steps:
    1. get server that support ASP.NET
    2. on that server, copy the aspx file as is.
    3. browse to the aspx file in such manner:
      Code:
      http://localhost/ImageResizer.aspx?image=[full image path here]&thumb=[full path of thumbnail to be created here]&width=[desired width]&height=[desired height]
    4. if you have desired width only and would like the height
      calculated automatically, then pass -1 as the height.
    5. if you have desired height only and would like the width
      calculated automatically, then pass -1 as the width.
    6. for example, suppose you got image called "house.jpg" in
      folder named "Images" and you want it resized to 500 pixels
      width:
      Code:
      http://localhost/ImageResizer.aspx?image=C:\Inetpub\wwwroot\Images\house.jpg&thumb=C:\Inetpub\wwwroot\Images\house_thumb.jpg&width=500&height=-1
      the above will save the thumbnail as house_thumb.jpg in the
      same folder.


    to use this in classic ASP code, use XMLHTTP component in
    order to "communicate" with the aspx file. simply pass the
    URL as demonstrated above:
    Code:
    <%
    Dim strImageName, strThumbName, objXML
    strImageName = "mypicture.jpg"
    strThumbName = "mypicture_thumb.jpg"
    Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    objXML.Open "GET", "http://localhost/ImageResizer.aspx?image=" & Server.MapPath(strImageName) & "&thumb=" & Server.MapPath(strThumbName) & "&width=500&height=-1", True
    objXML.Send
    Set objXML=Nothing
    Response.Write("original: <img src=""" & strImageName & """ /><br />")
    Response.Write("thumbnail: <img src=""" & strThumbName & """ /><br />")
    %>
    --Edit: (11/10/2009)
    Fixed the attached ASP.NET code to save the thumbnail in
    JPEG (aka JPG) format, making it smaller in size and more standard.
    To save in other format, change this line in the .aspx file:
    Code:
    wrapper.Save(strThumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    to any of the following:
    • Code:
      wrapper.Save(strThumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    • Code:
      wrapper.Save(strThumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
    • Code:
      wrapper.Save(strThumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
    • Code:
      wrapper.Save(strThumbnailPath, System.Drawing.Imaging.ImageFormat.Png);


    --Edit: (12/10/2009)
    Upgraded the code to support JPEG compression.
    To use this new feature, pass the desired compression level
    in the querystring, for example:
    Code:
    http://localhost/ImageResizer.aspx?image=C:\Inetpub\wwwroot\Images\house.jpg&thumb=C:\Inetpub\wwwroot\Images\house_thumb.jpg&width=500&height=-1&compression=90
    The above will save the thumbnail with compression level
    (quality) of 90 which means good quality but rather large
    file size.

    --Edit (17/10/2010)
    Upgraded the code to support:
    • Debug mode: given special key, it's now possible to debug
      the code and get vital information when it's not working.
      To activate the debug mode, add such flag on the URL:
      ImageResizer.aspx?....&debug=j1Ma82Pkl5xZ9
    • Supporting virtual path: when you don't know full path,
      or when image is inside the same web application, you can
      give only the virtual path (e.g. /images/myimage.jpg)
    • You don't have to specify thumbnail path anymore. If ommited,
      the code will use the image path and append "_thumb" to it.



    good luck and happy programming!

    Comments on this post

    • Lotus30 agrees : Much appreciated!
    Attached Files
    Last edited by Shadow Wizard; October 17th, 2010 at 12:09 PM. Reason: code upgrade
  2. #2
  3. Expert Learner
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Feb 2005
    Location
    Wisconsin
    Posts
    1,909
    Rep Power
    100
    Yahav, I looooooooooooooooooove you.

    Did I mention that I love you Yahav?

    Comments on this post

    • Shadow Wizard agrees : umm... I think so... :blush: ;)
  4. #3
  5. Expert Learner
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Feb 2005
    Location
    Wisconsin
    Posts
    1,909
    Rep Power
    100
    ** Disregard this...I just got it fixed! There was a permissions problem after all -- the internet guest account on this new server wasn't the default IUSR :\

    I've combined your upload script with the resizer to create thumbnails of images when they are uploaded. The following script piece only uploads the original file and nothing more. I tested to see that paths are right...what am I doing wrong?
    ASP Code:
    If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    	Dim objUpload
    	Set objUpload = New ShadowUpload
    	If objUpload.GetError <> "" Then
    		Response.Write("Sorry, could not upload: " & objUpload.GetError)
    	Else
    		Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    		strDir = strPath & dir
    		For x = 0 To objUpload.FileCount-1
    			If (objUpload.File(x).ImageWidth < 1) Or (objUpload.File(x).ImageHeight < 1) Then
    				Response.Write("Invalid file type!")
    				Response.End()
    			Else
    				Call objUpload.File(x).SaveToDisk(strDir, "")
    				u_bound = u_bound + 1
    				ReDim Preserve arrImages(u_bound)
    				arrImages(u_bound) = objUpload.File(x).FileName
    				strImageName = strDir & "\" & objUpload.File(x).FileName
    				strThumbName = strDir & "\" & getFileName(objUpload.File(x).FileName)
    				objXML.Open "GET", strPathToDir & "ImageResizer.aspx?image=" & strImageName & "&thumb=" & strThumbName & "&width=120&height=-1", True
    				objXML.Send()
    			End If
    		Next
    		Set objXML = Nothing
    	End If
    End If
    Last edited by baseballdude_; October 4th, 2006 at 12:22 AM.
  6. #4
  7. Expert Learner
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Feb 2005
    Location
    Wisconsin
    Posts
    1,909
    Rep Power
    100
    I actually am having the same problem again. The XMLHTTP doesn't seem to work right (even though it doesn't error) and doesn't create the thumbnail. I have this temporarily solved, having it create an iframe to be displayed, lol...but do you know what I'm doing wrong from the post above?
  8. #5
  9. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    debug the code by adding those lines:
    Code:
    Dim strURL,  strResult
    strURL = strPathToDir & "ImageResizer.aspx?image=" & strImageName & "&thumb=" & strThumbName & "&width=120&height=-1"
    Response.Write("URL: " & strURL & "<br />")
    objXML.Open "GET", strURL, True
    objXML.Send
    strResult = objXML.ResponseText
    Respose.Write("response: " & strResult & "<br />")
    and let me know the result of the debugging.
  10. #6
  11. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2006
    Posts
    3
    Rep Power
    0
    Does it support only JPG? Tried with GIF images and it didn't create thumbnail.
  12. #7
  13. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    it may fail on certain images. attach the image here if possible and
    I'll see for myself. did you try successfully with other images?
  14. #8
  15. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2006
    Posts
    3
    Rep Power
    0
    Originally Posted by Shadow Wizard
    it may fail on certain images. attach the image here if possible and
    I'll see for myself. did you try successfully with other images?
    Try it yourself with any image other than JPG. It works perfectly with JPG, but I had no luck with GIF or PNG.
  16. #9
  17. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by kaid
    I use classic ASP and don't have ASP.NET
    can I use this code ?
    if the server you work with does not have .NET framework you
    can't use my code, sorry.
  18. #10
  19. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by petrusj
    Try it yourself with any image other than JPG. It works perfectly with JPG, but I had no luck with GIF or PNG.
    ok, I was in rush when posting this code I will revise it soon and
    come with something better, that at least will show error or
    message if something fails.
  20. #11
  21. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2006
    Posts
    3
    Rep Power
    0
    Originally Posted by Shadow Wizard
    ok, I was in rush when posting this code I will revise it soon and
    come with something better, that at least will show error or
    message if something fails.
    Thanks!
  22. #12
  23. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2004
    Posts
    63
    Rep Power
    14
    This i just what i've been looking for! Is there any way of compressing the file size? When i resize an imge the file size is really big. When I resize an imgage which is 1024x768 (142kb) to 512x384 the file size is bigger then the original (397kb). ANy suggestions?
  24. #13
  25. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by fredde_pe
    This i just what i've been looking for! Is there any way of compressing the file size? When i resize an imge the file size is really big. When I resize an imgage which is 1024x768 (142kb) to 512x384 the file size is bigger then the original (397kb). ANy suggestions?
    well, it's using the .NET framework functions I don't have much control
    over the technical part of the process. I will try and search for alternatives
    though, will update this thread if and when I'm successful.
  26. #14
  27. Shemzilla
    ASP Scholar (3000 - 3499 posts)

    Join Date
    Aug 2005
    Location
    Table View, Cape Town, S.A
    Posts
    3,397
    Rep Power
    560
    does this script really make the thumbnail file size bigger?

    Shem
    Everyone has a photographic memory. Some don't have film.
    I am a nobody, nobody is perfect, therefore I am perfect.

  28. #15
  29. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by shem
    does this script really make the thumbnail file size bigger?

    Shem
    I think it depends how the image was originally created and compressed.
    you'll have to test with some images and see for yourself.
Page 1 of 8 123 ... Last
  • Jump to page:

Similar Threads

  1. ShadowResizer - image resize on the fly (classic ASP)
    By Shadow Wizard in forum Code Bank
    Replies: 23
    Last Post: June 26th, 2011, 02:51 AM
  2. vb: image resizing from stream to SQL Server
    By timandkids in forum .NET Development
    Replies: 0
    Last Post: November 10th, 2004, 12:54 PM
  3. Howto resize with stretch and save an image in jpg format?
    By zwonimir in forum Visual Basic Programming
    Replies: 3
    Last Post: August 24th, 2004, 07:08 AM
  4. resize external image.
    By telnoi in forum .NET Development
    Replies: 2
    Last Post: July 27th, 2004, 05:40 PM
  5. Image Resize Problem
    By adathelad in forum .NET Development
    Replies: 0
    Last Post: February 4th, 2004, 05:02 AM

IMN logo majestic logo threadwatch logo seochat tools logo