Solved

change image size -  server side

Posted on 2006-11-02
26
200 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.
0
Comment
Question by:itortu
  • 13
  • 8
  • 5
26 Comments
 
LVL 22

Assisted Solution

by:WMIF
WMIF earned 125 total points
ID: 17862559
you would need an image component installed on the server.  something like http://www.aspjpeg.com/
0
 

Author Comment

by:itortu
ID: 17862762
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.
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17862793
using that approach you are still loading the full size image.  this could use bandwidth and it would be slow for larger pictures.
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17862798
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.
0
 

Author Comment

by:itortu
ID: 17862818
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" />
0
 

Author Comment

by:itortu
ID: 17862909
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?
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17862952
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.
0
 

Author Comment

by:itortu
ID: 17866892
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.
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17868845
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.
0
 

Author Comment

by:itortu
ID: 17868859
excellent! thank you : )
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17869325
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. :)
0
 

Author Comment

by:itortu
ID: 17870680
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.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17871485
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)

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Expert Comment

by:WMIF
ID: 17872322
that looks like it was probably what i was remembering.  have you played with that at all?
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17873526
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.
0
 

Author Comment

by:itortu
ID: 17874357
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")  & "'>")

%>
0
 

Author Comment

by:itortu
ID: 17874393
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'>
0
 
LVL 22

Assisted Solution

by:WMIF
WMIF earned 125 total points
ID: 17874557
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  & "'>")

%>
0
 

Author Comment

by:itortu
ID: 17874636
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  & "'>")

%>
0
 

Author Comment

by:itortu
ID: 17874641
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")
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 125 total points
ID: 17874896
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  & "'>")

%>
0
 
LVL 25

Assisted Solution

by:kevp75
kevp75 earned 125 total points
ID: 17874897
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
0
 

Author Comment

by:itortu
ID: 17874938
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.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17874978
make sure this:
Response.Write("<img src='" & FileName  & "'>")

points to the correct file, in the correct folder
0
 

Author Comment

by:itortu
ID: 17875006
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  & "'>")

%>
0
 

Author Comment

by:itortu
ID: 17875041
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?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now