Replies Back to Article

Dynamically resize an image

Does this code DL the image with a size of the full image or the thumbnail...
May 9, 2001 by Gregory Van Horn

Example supposed the image is 900x900 and takes up 90K.  Using this code does the image DL to the browser the full 90K version?  My guess would be yes...

Also this is just an example...

Thanks,

-Greg

RE: Does this code DL the image with a size of the full image or the thumbnail...
May 9, 2001 by Dan Short
You got it. You would need something like the aspjpeg com or some similar server side technology to accomplish that.
Using A Dynamic Image....
May 10, 2001 by Gregory Van Horn

How would I go about using this with a dynamic image.  I will be pulling the image name from a recordset.  How would I go about doing this?

 

Thanks.

-Greg

RE: Using A Dynamic Image....
May 10, 2001 by Dan Short

<img src="<%=rs("File")%>" <% =ImageResize(Server.MapPath(rs("File")), 100, 100)%>>

Error...Need Help!
May 19, 2001 by Gregory Van Horn

Help! Pulling My Hair Out With This...

Heres the code:
<!--#include file="imgsz.asp" -->
<!--#include file="propresize.asp" -->
    <%
    strImage= rsMainModelPhoto("FileName")
    StrImageName1 = Server.MapPath("/images/" & strimage)
    %>
<img src="../images/<%=rsMainModelPhoto("FileName")%>" <% ImageResize
(strImageName1, 100, 75 )%>>



When I view this page I get this error:
Error Type:
Microsoft VBScript compilation (0x800A0414)
Cannot use parentheses when calling a Sub
/fineartfolios/dynamic/model.asp, line 143, column 37
ImageResize (strImageName1, 100, 75 )
------------------------------------^



I really need some help on this.  All and any help is appreciated.

Thanks in Advance,
-Greg

RE: Error...Need Help!
May 28, 2001 by George Petrov

You mis a = in your code

<% ImageResize (strImageName1, 100, 75 )%>

should be

<% =ImageResize(strImageName1, 100, 75 )%>



RE: RE: Using A Dynamic Image....
June 2, 2001 by Steve Hazelden

Help Please!

I store the filename of the picture I uploaded in my database as photo.

I use the following line to display the image:
<img src="images/personal/<%=(Recordset1.Fields.Item("photo").Value)%>">

I modified the string you supplied to read as follows:
<img src="<%=Recordset1.Fields.Item("photo").value%>" <% =ImageResize(Server.MapPath(Recordset1.Fields.Item("photo").value), 100, 100)%>>

I have tried it with and without the .value and it still gives me a type missmatch, also at the top of the page I see the entire contents of Imgsz.asp.

I really have tried, but sadly know very little about vbscript and ASP, just survive on Wonderfull Ultradev.

Thanks - Steve

Help
June 14, 2001 by Guy Murray
Can anyone tell what is wrong with this:

====================================================
<
%@LANGUAGE="VBSCRIPT"%>
<!--#include file="Connections/tmijobs.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
if (Request.QueryString("id") <> "") then Recordset1__MMColParam =
Request.QueryString("id")
%>
<%
   ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   ':::                                                   :::
   ':::  SCRIPT:   testpropresize.asp                     :::
   ':::  AUTHOR:   shaffer                                :::
   ':::  DATE:     11-Jan-01                              :::
   ':::  PURPOSE:  Test/show the operation of the resize  :::
   ':::            (proportional) function                :::
   ':::                                                   :::
   ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::

   %>
<!--#INCLUDE FILE='imgsz.asp'-->
<!--#INCLUDE FILE='propresize.asp'-->
<%

   dim objFSO, objF, objFC
   dim f1, w, h, c, strType

   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objF = objFSO.GetFolder(Server.MapPath("../tmi/jobs/images/"))
   Set objFC = objF.Files

%>




<%
strImage= recordset1("logo")
StrImageName1 = Server.MapPath("..tmi/jobs/images/" & strimage)

set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_tmijobs_STRING
Recordset1.Source = "SELECT * FROM jobs WHERE ID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
%>
<%
Recordset1.Close()
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<img src="..tmi/jobs/images/<%=recordset1("logo")%>"
<%=ImageResize(strImageName1, 100, 75 )%>>
</body>
</html>

===================================================================
I can get the image to appear but it does not resize.

Does anyone know why this is not working ??
RE: Help
June 14, 2001 by Dan Short

What is it writing out for the image size? What's being displayed in the source?

Part of the problem may be that you're trying to set a variable to a record, before you have a record:

<%
strImage= recordset1("logo")
StrImageName1 = Server.MapPath("..tmi/jobs/images/" & strimage)

set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_tmijobs_STRING
Recordset1.Source = "SELECT * FROM jobs WHERE ID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
%>

Change it to this:

<%
set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_tmijobs_STRING
Recordset1.Source = "SELECT * FROM jobs WHERE ID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
strImage= recordset1("logo")
StrImageName1 = Server.MapPath("..tmi/jobs/images/" & strimage)
%>

RE: RE: Help
June 15, 2001 by Guy Murray

Thanks Dan, that appears to work... however I am now getting the following error message:
==========================

ADODB.Field error '80020009'

Operation is not allowed when the object is closed.

?
===============================

Any ideas ??

RE: RE: RE: Help
June 15, 2001 by Dan Short

That means you've closed your recordset before you asked for the image field. Make sure there isn't a line like

<%Recordset1.Close%>

before you have your dynamic image.

Not Resizing
June 18, 2001 by Guy Murray

Image is not being resized, Can anyone explain why please ?

======================

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="Connections/tmidestinations.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
if (Request.QueryString("ID") <> "") then Recordset1__MMColParam = Request.QueryString("ID")
%>
<!--#INCLUDE FILE='imgsz.asp'-->
<!--#INCLUDE FILE='propresize.asp'-->
<%
set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_tmidestinations_STRING
Recordset1.Source = "SELECT * FROM tmidestinations WHERE ID = " + Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
strImage = Recordset1("destimage")
StrImageName1 = Server.MapPath("pages/inner/press_ticker/images/" & strImage)
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<img src="pages/inner/press_ticker/images/<%=recordset1("destimage")%>" <% =ImageResize(Server.MapPath(recordset1("destimage")), 50, 50)%>>
</body>
</html>
<%
Recordset1.Close()
%>

RE: Not Resizing
June 18, 2001 by Dan Short

Change this:

<% =ImageResize(Server.MapPath(recordset1("destimage")), 50, 50)% >

To This:

<% =ImageResize(strImageName1), 50, 50)% >

You were trying to do a Server.MapPath on a database record, which doesn't have a location on the server. It's in a database. Since you've already declared the location of the image using the StrImageName1, you should be able to resize it using that string.

OK Now I get an Object Expected error
June 25, 2001 by Robert F. Havens

I have tried with and without the extra rs stuff:
rsPTAppts.Fields.Item("Photo").Value
rsPTAppts("Photo")
Neither made a differance.

Here is my code

<%@LANGUAGE="JAVASCRIPT"%>
<!--#include file="Connections/OrthoProject.asp" -->
<%
var rsPTAppts = Server.CreateObject("ADODB.Recordset");
rsPTAppts.ActiveConnection = MM_OrthoProject_STRING;
rsPTAppts.Source = "{call dbo.upAppointments}";
rsPTAppts.CursorType = 0;
rsPTAppts.CursorLocation = 2;
rsPTAppts.LockType = 3;
rsPTAppts.Open();
var rsPTAppts_numRows = 0;
strImage= rsPTAppts("Photo")
strImageName1= Server.MapPath("PTImages/" & strImage)
%>
<%
var HLooper1__numRows = 10;
var HLooper1__index = 0;
rsPTAppts_numRows += HLooper1__numRows;
%>
<!--#INCLUDE FILE='imgsz.asp'-->
<!--#INCLUDE FILE='propresize.asp'-->

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="">
  <table>
    <%
var startrw="0;"
var endrw="HLooper1__index;"
var numbercolumns="2;"
var numrows="5;"
while((numrows-- !=0) && (!rsPTAppts.EOF)) {
 startrw=endrw + 1;
 endrw = endrw + numberColumns;
%>
    <tr align="center" valign="top">
      <%
 while ((startrw <= endrw) && (!rsPTAppts.EOF)) {
 %>
      <td> <img src="PTImages/<%=(rsPTAppts("Photo"))%>" <%=ImageResize(strImageName1, 100, 100)%>> </td>

All help appreciated,

Robert

RE: OK Now I get an Object Expected error
June 25, 2001 by Dan Short

What error are you receiving?

Dan.

RE: RE: OK Now I get an Object Expected error
June 25, 2001 by Robert F. Havens

I am getting this one:

  • Error Type:
    Microsoft JScript runtime (0x800A138F)
    Object expected
    /OrthoProject/TMP95fu7fi5ed.asp, line 44

  • Browser Type:
    Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)

  • Page:
    GET /OrthoProject/TMP95fu7fi5ed.asp
  • Line 44 reads:

          <td> <img src="PTImages/<%=(rsPTAppts("Photo"))%>" <%=ImageResize(strImageName1, 100, 100)%>> </td>

    Thanks

    RE: RE: RE: OK Now I get an Object Expected error
    July 5, 2001 by Robert F. Havens

    Helloooooooo,

    Dan I was wondering if you could check the above error. Boy do I wish for a SB for this.

    Robert

    Can't figure it out
    July 22, 2001 by boni boni

    I have been spending so much time trying to work this code out. Please help me to figure it out

    My code: [Deleted by Webmaster]
    RE: Can't figure it out
    July 22, 2001 by Waldo Smeets
    Boni, I edited your message. Please post this as a question in the TalkZones and refer to that topic in this comment. This page is not very friendly to discuss so much code. We are using this new policy because the comments section is not easy to use anymore. BTW: we are already working on a solution. Thanks - UDzone Webmaster
    Bringing the House Down
    August 6, 2001 by Marcos C Lopes

    The Server is hanging when trying to include the "imgsz.asp" The server admin doesn' know why. It runs fine on my win98 machine with pws.

    Any Ideas?

    Thanx

    problem to resize
    November 21, 2001 by tijger vandenhende

    I have made a test page, one image being a simple image located in a "images" named directory, the other one being dynamicaly linked to a database.

    Both of the images are appearing on the page but none are resized.

    Could someone tell me what's wrong ?

    Here is my code

    <%@LANGUAGE="VBSCRIPT"%>
    <!--#include file="Connections/wine.asp" -->
    <!--#INCLUDE FILE='imgsz.asp'-->
    <!--#INCLUDE FILE='propresize.asp'-->
    <%
    Dim Recordset1__MMColParam
    Recordset1__MMColParam = "1"
    if (Request.QueryString("ProductID") <> "") then Recordset1__MMColParam = Request.QueryString("ProductID")
    %>
    <%
    set Recordset1 = Server.CreateObject("ADODB.Recordset")
    Recordset1.ActiveConnection = MM_wine_STRING
    Recordset1.Source = "SELECT * FROM Products WHERE ProductID = " + Replace(Recordset1__MMColParam, "'", "''") + ""
    Recordset1.CursorType = 0
    Recordset1.CursorLocation = 2
    Recordset1.LockType = 3
    Recordset1.Open()
    Recordset1_numRows = 0
    strImage= recordset1("imgfile")
    StrImageName1 = Server.MapPath("dbimages/"& strimage)

    %>


    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">

    <p><img src="images/shiraz.jpg" <%=ImageResize(Server.MapPath("images/shiraz.jpg"), 50, 50)%>>
    </p>
    <p><img src="dbimages/<%=recordset1("imgfile") %>" <%=ImageResize(StrImageName1, 50, 50 )%>>
      <br>
    </p>

    </body>
    </html>
    <%
    Recordset1.Close()
    %>

    This article is very misleading
    July 11, 2002 by Alex Stevens

    Having read this article I though all of my dreams had come true. A piece of script that dynamically resizes "files" and delivers them to the client as thumbnails. Notice in the article how the third image is of a physically smaller file size than the first two. Confirmation I thought that the script IS actually resizing the file.

    Well after a couple of hours of getting it working in Javascript I find that this is in fact not true. The filesize remains exactly the same, effectively nullifying the whole idea. What this script appears to do is spend a couple of hundred lines of code chhosing whether to write and image width or height tag! Why can't you just say img width = "100" with no height in hte tag to resize a pic.

    I apologise if I have grabbed the wrong end of the stick, if the code does in fact resize pleeeease tell me how! But for now my advice would be just use the width tag, or the height tag to resize without losing proportion.

    Great script! Needed changes, but 5 of 5 vote from me!
    November 19, 2002 by Andrew Noce

    This script works great. I don't have a lot of experience with ASP and I had a problem getting the includes to work. I think the download is formatted wrong or something. I had to remove all the extraneous HTML from the pages and then combine all the functions into one file for the include, and I added a function around the TEST at the end so it doesn't do anything unless called.

    I was going to post my adjusted code but the interface won't let me paste?

    Now if I just had a (free to try, inexpensive to license) script to upload and resize user images on the fly...

    Good luck, and thanks for the script.

    RE: This article is very misleading
    January 3, 2003 by Edison Curry
    You need to invest in some type of image server if you want to do what you're talking about.  FlashPix or Live Picture Server and I think Adobe even makes a product that does on-the-fly image sizing.  Check out www.tigerwoods.com.  They utilize a technology that does what you're talking about.
    Here is the complete fix!!!!!
    July 16, 2003 by DMX ZONE

    Great code.  It took me about 20 minutes to fix all of it and get it to work.  Below is all the code needed.  You should be able to copy and paste everything.  Forget about downloading the above files.  He created it in HTML, not code.

    First create a page and call it "imgsz.asp" (without quotes!)

    Copy and Paste this code above the <html> tag:

    <%
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: :::
    '::: This routine will attempt to identify any filespec passed :::
    '::: as a graphic file (regardless of the extension). This will :::
    '::: work with BMP, GIF, JPG and PNG files. :::
    '::: :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: Based on ideas presented by David Crowell :::
    '::: (credit where due) :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah Copyright *c* MM, Mike Shaffer blah blah :::
    '::: blah blah ALL RIGHTS RESERVED WORLDWIDE blah blah :::
    '::: blah blah Permission is granted to use this code blah blah :::
    '::: blah blah in your projects, as long as this blah blah :::
    '::: blah blah copyright notice is included blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      '::: :::
      '::: This function gets a specified number of bytes from any :::
      '::: file, starting at the offset (base 1) :::
      '::: :::
      '::: Passed: :::
      '::: flnm => Filespec of file to read :::
      '::: offset => Offset at which to start reading :::
      '::: bytes => How many bytes to read :::
      '::: :::
      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      function GetBytes(flnm, offset, bytes)
     Dim objFSO
      Dim objFTemp
      Dim objTextStream
      Dim lngSize
     on error resume next
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     
      ' First, we get the filesize
      Set objFTemp = objFSO.GetFile(flnm)
      lngSize = objFTemp.Size
      set objFTemp = nothing
     fsoForReading = 1
      Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)
     if offset > 0 then
      strBuff = objTextStream.Read(offset - 1)
      end if
     if bytes = -1 then ' Get All!
     GetBytes = objTextStream.Read(lngSize) 'ReadAll
     else
     GetBytes = objTextStream.Read(bytes)
     end if
     objTextStream.Close
      set objTextStream = nothing
      set objFSO = nothing
     end function

      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      '::: :::
      '::: Functions to convert two bytes to a numeric value (long) :::
      '::: (both little-endian and big-endian) :::
      '::: :::
      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      function lngConvert(strTemp)
      lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
      end function
     function lngConvert2(strTemp)
      lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
      end function
     
      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      '::: :::
      '::: This function does most of the real work. It will attempt :::
      '::: to read any file, regardless of the extension, and will :::
      '::: identify if it is a graphical image. :::
      '::: :::
      '::: Passed: :::
      '::: flnm => Filespec of file to read :::
      '::: width => width of image :::
      '::: height => height of image :::
      '::: depth => color depth (in number of colors) :::
      '::: strImageType=> type of image (e.g. GIF, BMP, etc.) :::
      '::: :::
      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      function gfxSpex(flnm, width, height, depth, strImageType)
     dim strPNG
      dim strGIF
      dim strBMP
      dim strType
      strType = ""
      strImageType = "(unknown)"
     gfxSpex = False
     strPNG = chr(137) & chr(80) & chr(78)
      strGIF = "GIF"
      strBMP = chr(66) & chr(77)
     strType = GetBytes(flnm, 0, 3)
     if strType = strGIF then ' is GIF
     strImageType = "GIF"
      Width = lngConvert(GetBytes(flnm, 7, 2))
      Height = lngConvert(GetBytes(flnm, 9, 2))
      Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
      gfxSpex = True
     elseif left(strType, 2) = strBMP then ' is BMP
     strImageType = "BMP"
      Width = lngConvert(GetBytes(flnm, 19, 2))
      Height = lngConvert(GetBytes(flnm, 23, 2))
      Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))
      gfxSpex = True
     elseif strType = strPNG then ' Is PNG
     strImageType = "PNG"
      Width = lngConvert2(GetBytes(flnm, 19, 2))
      Height = lngConvert2(GetBytes(flnm, 23, 2))
      Depth = getBytes(flnm, 25, 2)
     select case asc(right(Depth,1))
      case 0
      Depth = 2 ^ (asc(left(Depth, 1)))
      gfxSpex = True
      case 2
      Depth = 2 ^ (asc(left(Depth, 1)) * 3)
      gfxSpex = True
      case 3
      Depth = 2 ^ (asc(left(Depth, 1))) '8
      gfxSpex = True
      case 4
      Depth = 2 ^ (asc(left(Depth, 1)) * 2)
      gfxSpex = True
      case 6
      Depth = 2 ^ (asc(left(Depth, 1)) * 4)
      gfxSpex = True
      case else
      Depth = -1
      end select

      else
     strBuff = GetBytes(flnm, 0, -1) ' Get all bytes from file
      lngSize = len(strBuff)
      flgFound = 0
     strTarget = chr(255) & chr(216) & chr(255)
      flgFound = instr(strBuff, strTarget)
     if flgFound = 0 then
      exit function
      end if
     strImageType = "JPG"
      lngPos = flgFound + 2
      ExitLoop = false
     do while ExitLoop = False and lngPos < lngSize
     do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
      lngPos = lngPos + 1
      loop
     if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
      lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
      lngPos = lngPos + lngMarkerSize + 1
      else
      ExitLoop = True
      end if
     loop
      '
      if ExitLoop = False then
     Width = -1
      Height = -1
      Depth = -1
     else
     Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
      Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
      Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
      gfxSpex = True
     end if
     
      end if
     end function

     ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      '::: Test Harness :::
      ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     
      ' To test, we'll just try to show all files with a .GIF extension in the root
      'of C:
     'Set objFSO = CreateObject("Scripting.FileSystemObject")
      'Set objF = objFSO.GetFolder("c:\")
      'Set objFC = objF.Files
     'response.write "<table border=""0"" cellpadding=""5"">"
     'For Each f1 in objFC
     ' if instr(ucase(f1.Name), ".GIF") then
    '  response.write "<tr><td>" & f1.name & "</td><td>"
     ' & f1.DateCreated & "</td><td>" & f1.Size &
     ' "</td><td>"
     'if gfxSpex(f1.Path, w, h, c, strType) = true then
     ' response.write w & " x " & h & " " & c &
    '  " colors"
    '  else
     ' response.write "&nbsp;"
    '  end if
     'response.write "</td></tr>"
     'end if
     'Next
     'response.write "</table>"
     'set objFC = nothing
     ' set objF = nothing
     ' set objFSO = nothing
    %>

    I have commented out the Test Harness.  It is more trouble than it's worth.

    Save the "imgsz.asp" page.

    Next,

    Create a page and call it "propresize.asp" (without the quotes!)

    Copy and paste this code above the <html> tag:

    <%
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: :::
    '::: SCRIPT: PropResize :::
    '::: AUTHOR: Mike Shaffer :::
    '::: DATE: 03-May-2000 :::
    '::: PURPOSE: Creates a WIDTH/HEIGHT parameter string :::
    '::: (for use with the &lt;IMG...> tag to allow :::
    '::: for proportional resizing :::
    '::: NOTE: Requires routines found in IMGSZ.ASP :::
    '::: http://www.4guysfromrolla.com/webtech/050300-1.shtml :::
    '::: :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah :::
    '::: blah blah blah Copyright *c* MM, Mike Shaffer blah blah blah :::
    '::: blah blah blah ALL RIGHTS RESERVED WORLDWIDE blah blah blah :::
    '::: blah blah blah Permission is granted to use this code blah blah blah :::
    '::: blah blah blah in your projects, as long as this blah blah blah :::
    '::: blah blah blah copyright notice is included blah blah blah :::
    '::: blah blah blah and is unaltered. blah blah blah :::
    '::: blah blah blah blah blah blah :::
    '::: blah blah blah CONTACT: mshaffer@nkn.net or blah blah blah :::
    '::: blah blah blah mike@mikeshaffer.com blah blah blah :::
    '::: blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah blah blah :::
    '::: blah blah blah blah blah blah blah blah blah blah blah blah blah blah :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

      function ImageResize(strImageName, intDesiredWidth, intDesiredHeight)
     dim TargetRatio
      dim CurrentRatio
      dim strResize
      dim w, h, c, strType
     if gfxSpex(strImageName, w, h, c, strType) = true then
      TargetRatio = intDesiredWidth / intDesiredHeight
      CurrentRatio = w / h
      if CurrentRatio > TargetRatio then ' We'll scale height
      strResize = "width=""" & intDesiredWidth & """"
      else
      strResize = "height=""" & intDesiredHeight & """"
      ' We'll scale width
      end if
      else
      strResize = ""
      end if
     ImageResize = strResize
     end Function
    %>

    Save the "propresize.asp" page.

    The following is the page that you have the image in.  In mine, I am using it with a file that is in a database for a members page.

    1.  You will need to call the files that you just created in an include command whether you are using a database or not:

    <!--#INCLUDE FILE="imgsz.asp"-->
    <!--#INCLUDE FILE="propresize.asp"-->

    If you have put these files in say a ScriptLibrary folder as I did, then it would look like this:

    <!--#INCLUDE FILE="ScriptLibrary/imgsz.asp"-->
    <!--#INCLUDE FILE="ScriptLibrary/propresize.asp"-->

    2. Create your recordset like you would normally do, and copy and paste the below lines that are in red: (This is just my recordset, so don't copy the entire thing, just what is in red)

    <%
    Dim rsMember__value
    rsMember__value = "0"
    If (request.cookies("UID") <> "") Then
      rsMember__value = request.cookies("UID")
    End If
    %>
    <%
    Dim rsMember
    Dim rsMember_numRows

    Set rsMember = Server.CreateObject("ADODB.Recordset")
    rsMember.ActiveConnection = MM_waea_STRING
    rsMember.Source = "SELECT *  FROM members  WHERE USERID='" + Replace(rsMember__value, "'", "''") + "'"
    rsMember.CursorType = 0
    rsMember.CursorLocation = 2
    rsMember.LockType = 1
    rsMember.Open()

    rsMember_numRows = 0
    strImage= (rsMember.Fields.Item("IMAGE").Value)
        StrImageName1 = Server.MapPath("memberimages/" & strimage)
    %>

    (rsMember.Fields.Item("Image").Value) is my binding, so change that to your recordset binding.

    The "memberimages/" is the folder that my image is in.  So change that with your folder.

    3. Insert your image where you would like it on your page, and then goto the line where your img src code is for your image.  It should look like this:

    <img src="memberimages/<%=(rsMember.Fields.Item("IMAGE").Value)%>">

    Add the following code after the last " and before the >

    <%=ImageResize(strImageName1, 100, 100)%>

     

    So the final img src code should look like this:

    <img src="memberimages/<%=(rsMember.Fields.Item("IMAGE").Value)%>"<%=ImageResize(strImageName1, 100, 100)%>>

    That's it folks.  If all of your syntax is correct, everything should work as advertised.

    If it doesn't, first things first:  CHECK YOUR SYNTAX since you are pasting and changing code!  If you still can't get it to work, post a reply WITH YOUR ERROR AND WITH YOUR CODE and I'll see if I can help.

    Brian Friedman

    hi, problem with image size
    March 7, 2004 by Yaniv Kri

    Let's say, I have an image which his size is 175k, when I use this command

     <img src="DefaultPic.jpg" <%=ImageResize(Server.MapPath("DefaultPic.jpg"), 100, 100)%>>

    it's works, it's gatting smaller, but hise is still 175k no smaller size, not like current sample of this page when the original image is 12k and the same small image is 4 k..

    where I wrong?

     

    Thanks.

    Followed code example, but only width constraining?
    August 31, 2004 by sean monhan

    I have a recordset with a "picture" field.  I followed the example and copied and pasted the code posted by Brian Friedman on page one and my images are being resized, but only constrained by width.  I must be missing something but wanted to ask if someone else may have had the same issue.  I tested this by using both a wide and a tall pic and the result is the same, viewing the source I can see that everything is set to width = 100.  I am using jpg files.  Below is my code:

    Set Recordset1 = Server.CreateObject("ADODB.Recordset")
    Recordset1.ActiveConnection = MM_dbo_STRING
    Recordset1.Source = "SELECT * FROM dbo.tblContacts"
    Recordset1.CursorType = 0
    Recordset1.CursorLocation = 2
    Recordset1.LockType = 1
    Recordset1.Open()
      Recordset1_numRows = 0
    strImage= (Recordset1.Fields.Item("Picture").Value)
        StrImageName1 = Server.MapPath("/webroot/uploads/" & strimage)
      %>   <td height="38" width="101"><a href="/webroot/uploads/<%=(Recordset1.Fields.Item("Picture").Value)%>"><img src="/webroot/uploads/<%=(Recordset1.Fields.Item("Picture").Value)%>"alt = "<%=(Recordset1.Fields.Item("Name").Value)%>" border="0" <%=ImageResize(strImageName1, 100, 100)%>></a></td>   Any help would be greatly appreciated!   Thanks, Sean  
    RE: RE: Here is the complete fix!!!!!
    September 1, 2004 by sean monhan
    Found my problem.  I am doing a repeat region and by defining strimageName1 outside of the repeat region, it was taking the first image properties and using that for everything.  I moved the following lines "strImage= (rsMember.Fields.Item("IMAGE").Value)
        StrImageName1 = Server.MapPath("memberimages/" & strimage
    )" within my repeat region, it now is picking up the properties as it cycles through.  I put this variable just after:  "While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))"
    resizer
    February 8, 2006 by Haskins Virginia
    does something exist when you develop in php ? I would like to automaticly resized the images people will download on the serveur and create the thumbnails associated as the same way. I can't find something simple to do that. Could you help me ?
    So that extra code does what for me?
    May 18, 2008 by student 101

    So that extra code does what for me? apart from resizing the image in the browser, how would or could that script help resize the uploaded image?

    Cheers