We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

change image size -  server side

itortu
itortu asked
on
Medium Priority
252 Views
Last Modified: 2008-02-01
I would like to ask, how can the size of an image be changed by passing a parameter via url?
This is an example of what I would like to know how to do, and if possible get help to accomplish on an smaller version:

http://weather.uclick.com/animmaps?map=wci_nxrd_DC&resize=yes&width=612

you can change the widht on the url to whatever you want and the image is resized.
any help is appreciated. thank you.
Comment
Watch Question

Top Expert 2005
Commented:
you would need an image component installed on the server.  something like http://www.aspjpeg.com/

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
why is a component neccesary?
are there any other ways to do this?
if a component is not used, what's the down side?

can the height and widht be passed and inserted like:

<img src="image.gif" width="<%=NewWidth%>" height="<%=NewHeight%>">

having the height and width default values if none are passed?
if this approach is used, what's the down side?

thank you very much.
Top Expert 2005

Commented:
using that approach you are still loading the full size image.  this could use bandwidth and it would be slow for larger pictures.
Top Expert 2005

Commented:
also, if the user were to right click -> save... that picture, it would save at the original size.  the browser is simply cutting it down for display purposes.  which reminds me too that the method the browser uses to shrink the image isnt the best usually.  you can end up with a choppy image.

Author

Commented:
are ther any free components that will allow me to do this?

if not, would you be able to show me how to just resizing it using only html?

i just wnat to resize the image to make it fit in a small window, not neccesarily to save load time.

using this image:
<img id="mainmap" src="../wp/maps/us_satellite.gif" name="mainimage" border="0" />

Author

Commented:
hi again WMIF,

I dowloaded a trial version of the aspjpeg component. I was wondering if you could show me how this is use, or someone else?
Top Expert 2005

Commented:
i would suggest that you read through the manual here:
http://www.aspjpeg.com/manual.html

they can explain all the features and how to do it all much better than i could.  are you still interested in doing only the html resize?  post back with any questions.

Author

Commented:
Hi WMIF, sorry I did not reply sooner. Answering your question about doing the html resize, I would say yes, since using the component will take me some time to learn. I hope you are still around.

Thank you.
Top Expert 2005

Commented:
im still here. :)

actually as im thinking about how to do it, it would require the component anyways to figure out the dimensions of the image.  although i seem to recall some way to get those without a component.  let me see if i can find a link for that.

Author

Commented:
excellent! thank you : )
Top Expert 2005

Commented:
i cant seem to find it yet.  i will keep my eyes open for it though.  in the mean time get reading in that manual. :)

Author

Commented:
i read the manual, but even though i downloaded a trial version of the component everytime i tried to use it, i get an error saying that i need to purchase a license, so i guess that's it for the component, it is almost 200 dollars.

if you remember how to do this, i mean get the dimensions for the image without a component, please let me know. thank you in the mean time.

Commented:
mayhaps this is what you were looking for WMIF:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6629&lngWId=4

@itortu
the manual is really straight forward and very easy to use  (depending on what you need to do of course), but it will show you how to resize the image, even by passing the size to the script (though not recommended as it is a memory hog)

Top Expert 2005

Commented:
that looks like it was probably what i was remembering.  have you played with that at all?

Commented:
unfortunately I have not (haven't had a need to), but I trust do Lew's code....and may give it a whirl later on today.

Author

Commented:
hello guys,

i am trying to incorporte one of the scripts found at aspjpeg manuals, but it is not placing the image on the browser, although it is writting the image with new dimensions on the directory folder.

i was wondering if you could tell me what i may be doing wrong on the way the script is been adapted, so i can change it.

here is the script, to test it you may need to download a copy of the component and have a .jpeg image.


thank yo very much.

<%
' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")

' Compute path to source image
Path = Server.MapPath("images/hero.jpg")

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
Jpeg.Width = Request.QueryString("Width")
Jpeg.Height = Request.QueryString("Height")

' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(FileName & ".jpg") 'input what you want the file to be called

Response.Write("<img src='" & Server.MapPath(FileName & ".jpg")  & "'>")

%>

Author

Commented:
by the way, if i see the source code of tghe page i run, this is what i see:

<img src='c:\inetpub\wwwroot\resize\.jpg'>
Top Expert 2005
Commented:
the browser doesnt know that location because it is on the server.  you need to give you virtual path.

<img src='resize/image.jpg'>


<%
' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")

filename = "hero.jpg"

' Compute path to source image
Path = Server.MapPath("images/" & filename)

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
Jpeg.Width = Request.QueryString("Width")
Jpeg.Height = Request.QueryString("Height")

' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(FileName) 'input what you want the file to be called

Response.Write("<img src='resize/" & FileName  & "'>")

%>

Author

Commented:
hi, thank you that worked.

i need to ask a couple of questions to better understand what this script does if you don't mind.

the original image file is located inside the images folder
C:\Inetpub\wwwroot\resize\images\hero.jpg

this image is never modified correct?

the image that is written when the width and height are place on the url string
is place here:
C:\Inetpub\wwwroot\resize\hero.jpg

is there a way to not overwrite the image but to keep a copy of each new image with its new dimensions?
if the widht and height are not placed on the url, how can the image still be displayed?

i hope you don't mind my numerous question. once again, thank you.

<%
' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")

filename = "hero.jpg"

' Compute path to source image
Path = Server.MapPath("images/" & filename)

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
Jpeg.Width = Request.QueryString("Width")
Jpeg.Height = Request.QueryString("Height")

' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(fileName) 'input what you want the file to be called

Response.Write("<img src='" & FileName  & "'>")

%>

Author

Commented:
when i don't specify a width and a height on the string, i get this error:

Error Type:
Persits.Jpeg.1 (0x80040002)
Image width must be from 1 to 65535.
/resize/aspjpeg.asp, line 14

>> Jpeg.Width = Request.QueryString("Width")
Commented:
you could set a random value to the filename before the jpeg component saves it.....and you are getting that error because you are not passing a value to resize it to.

do a check, and if there is no value for the querystring, assign one...something like

<%
' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")

filename = "hero.jpg"

' Compute path to source image
Path = Server.MapPath("images/" & filename)

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
strWidth = Request.QueryString("Width")
strHeight = Request.QueryString("Height")

if strWidth = "" or isnull(strWidth) then
   strWidth = 640
end if
if strHeight = "" or isnull(strHeight) then
   strHeight = 480
end if

Jpeg.Width = strWidth
Jpeg.Height = strHeight

' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(fileName) 'input what you want the file to be called

Response.Write("<img src='" & FileName  & "'>")

%>
Commented:
i use a function to generate a random string that I append to the filename:
Function rndID(byVal maxLen)
      Dim strNewCode
      Dim whatsNext, upper, lower, intCounter
      Randomize
      For intCounter = 1 To maxLen
            whatsNext = Int((1 - 0 + 1) * Rnd + 0)
            If whatsNext = 0 Then
                  upper = 90
                  lower = 65
            Else  
                  upper = 57
                  lower = 48
            End If
            strNewCode = strNewCode & Chr(Int((upper - lower + 1) * Rnd + lower))
      Next
      rndID = strNewCode
end function


and to use it, put that code at the top of your page, and then change this line:
Jpeg.Save Server.MapPath(fileName) 'input what you want the file to be called

to:
Jpeg.Save Server.MapPath(rndID(5)&fileName) 'input what you want the file to be called

Author

Commented:
thank you, now the image is saved everytime the page is run with a new (random) name.

One thing I realized, is that if I cahnge the dimensions of the image, the file is saved in the directory folder without any problem, but
the image with the new size is not displayed on the page, it remains the same.

Commented:
make sure this:
Response.Write("<img src='" & FileName  & "'>")

points to the correct file, in the correct folder

Author

Commented:
ther is where i get confused, the directory wher i keep the asp file and the images looks more less like this:

images {folder with the source hero.jpg file}
aspjpeg.asp {page with the code}
hero.jpg {image that is written, i'm not using yet your function code on the code below}

code:

<%

' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")

filename = "hero.jpg"

' Compute path to source image
Path = Server.MapPath("images/" & filename)

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
strWidth = Request.QueryString("Width")
strHeight = Request.QueryString("Height")

if strWidth = "" or isnull(strWidth) then
   strWidth = 640
end if
if strHeight = "" or isnull(strHeight) then
   strHeight = 480
end if

Jpeg.Width = strWidth
Jpeg.Height = strHeight

' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(fileName) 'input what you want the file to be called

Response.Write("<img src='" & FileName  & "'>")

%>

Author

Commented:
the only way i can view the new image, is by first hitting go (after changing width and height) and then reloading the entire page.
do you thin k this can work by just hitting go, or reloading the page, instead of having to do both?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.