<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>effengud software &#187; vbscript</title>
	<atom:link href="http://www.effengud.com/index.php/tag/vbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.effengud.com</link>
	<description>It&#039;s Simple. It&#039;s Elegant. It&#039;s effengud!™</description>
	<lastBuildDate>Tue, 10 Aug 2010 16:14:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Faster VBScript Base64 Encode</title>
		<link>http://www.effengud.com/index.php/2009/09/03/a-faster-base64-encode/</link>
		<comments>http://www.effengud.com/index.php/2009/09/03/a-faster-base64-encode/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 18:10:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=37</guid>
		<description><![CDATA[The RC4 encryption article has always been a popular one on our website. In fact, several commercial software packages have utilized the routines in that article over the past few years. However, often when individual developers try to implement those routines, there is a question that crops up, and that question is invariably related to the same thing... ]]></description>
			<content:encoded><![CDATA[<p>When passing this encrypted data over the wire, it is BINARY data, which means that you can&#8217;t really pass it as part of a querystring. So many developers try saucing it up with a Server.URLEncode function, but what they don&#8217;t always realize is that Microsoft&#8217;s URLEncode function does NOT play well with embedded ASCII NULs. In fact, it assumes that the ASCII 0 (zero) signifies the end-of-string marker, and will stop as soon as the first NUL is encountered.</p>
<h2>So how do we pass binary information?</h2>
<p>The solution I have recommended (and used myself) is to further ENCODE the data with Base64 encoding. And the solution I have often recommended is the one found at Motobits.com (found here: <script>document.write('<a hr'+'ef='+'ht'+'tp:/'+'/ww'+'w.motobi'+'t.com/tips/detpg_Bas'+'e64Encode/'+' targ'+'et=_bla'+'nk>ht'+'tp:/'+'/ww'+'w.motobi'+'t.com/tips/detpg_Bas'+'e64Encode/</'+'a>');</script>. It is a proven solution and I have personally used it in many of my own projects. Recently, however, one of our own applications seemed to be getting bottle-necked at the Base64 encode and decode functions. The application in question had to encode blocks of data that were 64K-bytes in size, and it sometimes had to do several thousand of those blocks. </p>
<p>So I started looking at the old MotoBits code, with an eye toward optimizing it. And what I ended up with is new version that is much faster (over 40 times faster in most cases!). Interested?  Well, if you&#8217;re currently doing Base64 encoding with VBScript, you should be. </P></p>
<p>As an example (rough and unscientific, but useful for comparison), to encode 10 blocks of 64K Bytes each, the original Motobits version takes 13.94 seconds on a dual-core system. On that same system, the new version takes about 0.31 seconds. So, whereas the former version managed around 47,000 bytes per second, the new one does over 2,097,000 in that same time period.</p>
<p>Here is the new, optimized code:</p>
<pre>
Function FasterBase64Encode(inData)
  'rfc1521
  ' Notice: This routine is based on:
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz

  'Speed enhancements by Mike Shaffer

  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim arybase64(64)
  For intX = 1 To 64
     aryBase64(intX) = Mid(Base64,intX,1)
  Next

  Dim cOut, sOut, I, L2
  Dim nGroup, pOut, sGroup
  Dim ary1(), aryCTR

  aryCTR = -1

  L2 = Len(inData)
  ReDim ary1(Int(L2*1.5))

  'For each group of 3 bytes
  For I = 1 To L2 Step 3    

    nGroup = Oct(&#038;H10000 * Asc(Mid(inData, I, 1)) + &#038;H100 * _
                 MyASC(Mid(inData, I + 1, 1)) + _
                 MyASC(Mid(inData, I + 2, 1)))

    'Add leading zeros
    nGroup = Right("00000000" &#038; nGroup, 8)

    'Convert To base64
    aryCTR = aryCTR + 1
    ary1(aryCTR) = arybase64(CLng("&#038;o" &#038; Mid(nGroup, 1, 2)) + 1)
    aryCTR = aryCTR + 1
    ary1(aryCTR) = arybase64(CLng("&#038;o" &#038; Mid(nGroup, 3, 2)) + 1)
    aryCTR = aryCTR + 1
    ary1(aryCTR) = arybase64(CLng("&#038;o" &#038; Mid(nGroup, 5, 2)) + 1)
    aryCTR = aryCTR + 1
    ary1(aryCTR) = arybase64(CLng("&#038;o" &#038; Mid(nGroup, 7, 2)) + 1)

  Next
  ReDim preserve ary1(aryCTR)
  sOut = Join(ary1,"")

  ' Perform fixup
  Select Case L2 Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select

  FasterBase64Encode = sOut

End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/09/03/a-faster-base64-encode/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RC4 Encryption with VBScript</title>
		<link>http://www.effengud.com/index.php/2009/08/04/rc4-encryption-with-vbscript/</link>
		<comments>http://www.effengud.com/index.php/2009/08/04/rc4-encryption-with-vbscript/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 17:23:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[RC4]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=32</guid>
		<description><![CDATA[I recently had a need to query a remote server via HTTP and receive rather sensitive information. SSL was not an option because of the ISP&#8217;s setup. For these reasons (and a few others), I found myself in need of a good, general-purpose encryption module. For the purposes of prototyping and early testing, I whipped [...]]]></description>
			<content:encoded><![CDATA[<p><P>I recently had a need to query a remote server via HTTP and receive rather sensitive information. SSL was not an option because of the ISP&#8217;s setup. For these reasons (and a few others), I found myself in need of a good, general-purpose encryption module. For the purposes of prototyping and early testing, I whipped one up using the old stand-by system of circular-XOR&#8217;s. Geez, that&#8217;s quick and dirty, but it&#8217;s about as secure as a message sent on a postcard in the US Mail. My own home-grown cryptanalysis tools made quick work of cracking the code. </p>
<p><P>Obviously, I was going to need something much stronger than that for the site once it went into general production. I also wanted something that I could code completely in VBscript for ASP. Mainly because I wanted to be able to use the code in any ASP environment, regardless of any ISP&#8217;s component registration policies (or lack thereof), etc. In addition, I wanted something that was publicly proven and recognized as being fairly secure.</p>
<p><P>I chose the streaming-encryption algorithm known as RC4. RC4 is generally regarded as being &#8220;strong&#8221;, and has no known attacks (although a relatively weak class of keys has been identified &#8211; the discussion of which is beyond the scope of this document).</p>
<p><P>Other strengths of this algorithm include decent encryption/decryption speed and relative ease of coding in VBscript. It is also interesting to note the symmetrical nature of the RC4 algorithm. What I mean by &#8217;symmetrical&#8217; is that the same routine is called to do both encryption <b>and</b> decryption. To encrypt data, simply pass the data and the password you choose to the routine to receive encrypted data. To decrypt, pass the encrypted data and the same password. (Note: It is possible to encrpyt the data multiple times, even with different passwords on each iteration. To decrypt, simply reverse the steps you followed during encryption).</p>
<p><P>There&#8217;s an interesting story behind this algorithm. Well, OK, maybe it&#8217;s not interesting if you&#8217;re not a self-proclaimed geek. But here it is anyway. RC4 was invented by RSA Data Security. It is not a patented algorithm, but it is protected under federal law as a trade secret by RSA. In 1994, an anonymous person posted what they called the (do your Dr. Evil air-quotation marks here) &#8220;source code&#8221; to the RC4 algorithm. No one (outside of RSA) knows whether or not the &#8220;source code&#8221; that was posted was the actual RC4 algorithm or not, but it certainly does produce exactly the same output as the RSA product. So, in fact, the &#8220;source code&#8221; presented here can only be said to be &#8220;RC4-like&#8221; in nature&#8230; there&#8217;s no telling if it actually is RC4 as implemented by the RSA company.</p>
<p><P>One word of warning is in order here: If you plan to use this code outside the US, or if you plan to include it in a product that you are going to ship outside the US, please make yourself aware of the legal restrictions of crypto export. Always remember&#8230; guns don&#8217;t kill people, algorithms kill people.</p>
<p><P>I have included a sample test harness (<CODE>rc4test.html</CODE> and <CODE>rc4test.asp</CODE>) to help you see how the  algorithm (contained in <CODE>rc4.inc</CODE>) works. The output of the test looks like this:</p>
<p><P><TABLE WIDTH=95% BORDER=0><br />
<TR><TD WIDTH=100% BGCOLOR=#CCCCCC></p>
<h3>RC4 Test Harness</h3>
<p><b>Plaintext was:</b> &#8220;To be or not to be: that is the question, whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune.&#8221; can be anagrammed to form: &#8220;In one of the Bard&#8217;s best-thought-of tragedies, our insistent hero, Hamlet, queries on two fronts about how life turns rotten.&#8221;<br /><b>Encrypted text:</b> 0E%89%02y%D9%9B%F7%C0%D48%D21%10%BF%0De%1A%7E%F9%C6%BE%B1%B8h4%ED%A6%1D%8B%27%B4O%3DXAk2%3F%88%98%E592s%DE%8C%E6%E1IM%0A%7F%C5f%C7V%3E%EC%19%C7%18%DA%25%B4%C1%2C%12%B8%80c%14%BB%E1h%A7m%E5%E8%E9%F6%21%04%9F%2B%0E%E3%B2%9D%A8%FB%FA%D7T%7B%FAQ%3Dw%E21%E4%29%FA%23%FB%F9%1D%0AT%BF%0E%FF%94%7Dm%B4%2A%C8%3E%01J%AF%C8%7EB%2CF%F0q%F8%AD%9EFB%DAo%17%AF%7C%3A%13T%B3%9E%B5%11%12%7F%94%3D%1C%0C9%21%26%AE%06%E6%E6%F0%0Em%90%EC%12%039%1DG%D7%BA%9C%A1%04%BF%FA%F9%A3%ED%C1l%E8AEM%CB%B4%1Ba%D2%ADT%BCZ%04%C2%1Bvv%F9%8F%DF%B8U%8C%17%8F%BF%A7%D1kV%D2%B2%C6%3F%2E%BFD%C3%E1Ht%2E%EF%A7%C6%0E%FFRFU%92%22%CC%FA%92%5E%DA%FAn%AB5%E1%DB%D9%83%D9%E8%C2i%ADP%8Fk%E7+%1E%A9%2C%1C6%16%8D%27%AF%B6R%C50%81KJ%18%F8%0CQ%2EU%04%C3%5B%9E%3E</p>
<p><b>Hex dump of encrypted string:</b>
<pre>30 45 89 02 79 D9 9B F7 C0 D4 38 D2 31 10 BF 0D 65 1A 7E F9 C6 BE B1 B8 68 34
ED A6 1D 8B 27 B4 4F 3D 58 41 6B 32 3F 88 98 E5 39 32 73 DE 8C E6 E1 49 4D 0A
7F C5 66 C7 56 3E EC 19 C7 18 DA 25 B4 C1 2C 12 B8 80 63 14 BB E1 68 A7 6D E5
E8 E9 F6 21 04 9F 2B 0E E3 B2 9D A8 FB FA D7 54 7B FA 51 3D 77 E2 31 E4 29 FA
23 FB F9 1D 0A 54 BF 0E FF 94 7D 6D B4 2A C8 3E 01 4A AF C8 7E 42 2C 46 F0 71
F8 AD 9E 46 42 DA 6F 17 AF 7C 3A 13 54 B3 9E B5 11 12 7F 94 3D 1C 0C 39 21 26
AE 06 E6 E6 F0 0E 6D 90 EC 12 03 39 1D 47 D7 BA 9C A1 04 BF FA F9 A3 ED C1 6C
E8 41 45 4D CB B4 1B 61 D2 AD 54 BC 5A 04 C2 1B 76 76 F9 8F DF B8 55 8C 17 8F
BF A7 D1 6B 56 D2 B2 C6 3F 2E BF 44 C3 E1 48 74 2E EF A7 C6 0E FF 52 46 55 92
22 CC FA 92 5E DA FA 6E AB 35 E1 DB D9 83 D9 E8 C2 69 AD 50 8F 6B E7 20 1E A9
2C 1C 36 16 8D 27 AF B6 52 C5 30 81 4B 4A 18 F8 0C 51 2E 55 04 C3 5B 9E 3E </pre>
<p><b>Decrypted text:</b><br />&#8220;To be or not to be: that is the question, whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune.&#8221; can be anagrammed to form: &#8220;In one of the Bard&#8217;s best-thought-of tragedies, our insistent hero, Hamlet, queries on two fronts about how life turns rotten.&#8221;
<pre>22 54 6F 20 62 65 20 6F 72 20 6E 6F 74 20 74 6F 20 62 65 3A 20 74 68 61 74 20
69 73 20 74 68 65 20 71 75 65 73 74 69 6F 6E 2C 20 77 68 65 74 68 65 72 20 74
69 73 20 6E 6F 62 6C 65 72 20 69 6E 20 74 68 65 20 6D 69 6E 64 20 74 6F 20 73
75 66 66 65 72 20 74 68 65 20 73 6C 69 6E 67 73 20 61 6E 64 20 61 72 72 6F 77
73 20 6F 66 20 6F 75 74 72 61 67 65 6F 75 73 20 66 6F 72 74 75 6E 65 2E 22 20
63 61 6E 20 62 65 20 61 6E 61 67 72 61 6D 6D 65 64 20 74 6F 20 66 6F 72 6D 3A
20 22 49 6E 20 6F 6E 65 20 6F 66 20 74 68 65 20 42 61 72 64 27 73 20 62 65 73
74 2D 74 68 6F 75 67 68 74 2D 6F 66 20 74 72 61 67 65 64 69 65 73 2C 20 6F 75
72 20 69 6E 73 69 73 74 65 6E 74 20 68 65 72 6F 2C 20 48 61 6D 6C 65 74 2C 20
71 75 65 72 69 65 73 20 6F 6E 20 74 77 6F 20 66 72 6F 6E 74 73 20 61 62 6F 75
74 20 68 6F 77 20 6C 69 66 65 20 74 75 72 6E 73 20 72 6F 74 74 65 6E 2E 22 </pre>
<p><b>Encryption took:</b> 0.0078125 seconds (±55 msec)</p>
<p></TD></TR><br />
</TABLE><br />
<P><I><br />
(Note that the output of the encrypted text is shown in &#8216;urlencoded&#8217; form. This is because it may contain illegal characters for a web browser. IMPORTANT NOTE: Microsoft&#8217;s URLEncode function does NOT work if there are embedded ASCII NULs in the data you&#8217;re encoding&#8230; the function assumes that the ASCII 0 is an end-of-string marker, and thus will not encode past that point)</i></p>
<p><P><B>Overview</B>: This article contains a decent encryption tool that you may find useful. Read up on encryption technology to determine if this algorithm is strong enough for your needs. Don&#8217;t give this code to anyone who doesn&#8217;t love baseball and apple pie.</p>
<p><P></p>
<p><strong>Test the script by <a href="/articles/rc4test.html" target="_blank">clicking here</a></p>
<p>Article collateral materials:</p>
<p>&nbsp;&nbsp;&nbsp;<img src="/elements/effendot.gif" width="7" height="7">&nbsp;<a href=" /articles/rc4.inc.html">View/download RC4.INC in text format</a><br />&nbsp;&nbsp;&nbsp;<img src="/elements/effendot.gif" width="7" height="7">&nbsp;<a href="/articles/rc4test.html.html">View/download RC4TEST.HTML in text format</a><br />&nbsp;&nbsp;&nbsp;<img src="/elements/effendot.gif" width="7" height="7">&nbsp;<a href="/articles/rc4test.asp.html">View/download RC4TEST.ASP in text format</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/08/04/rc4-encryption-with-vbscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Determining Image Properties using ASP</title>
		<link>http://www.effengud.com/index.php/2009/08/01/determining-image-properties-using-asp/</link>
		<comments>http://www.effengud.com/index.php/2009/08/01/determining-image-properties-using-asp/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 16:42:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[dimensions]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=25</guid>
		<description><![CDATA[In a recent small project, I needed a way to display a series of images in a square area (150&#215;150 pixels). The images in question are uploaded by our customers, and we have no real control over what size or color depth (or even type) they are. While it&#8217;s easy to force a resize of [...]]]></description>
			<content:encoded><![CDATA[<p><P>In a recent small project, I needed a way to display a series of images in a square area (150&#215;150 pixels). The images in question are uploaded by our customers, and we have no real control over what size or color depth (or even type) they are. While it&#8217;s easy to force a resize of an image to, say, 150 pixels wide x 150 pixels high, you run the risk of stretching the image incorrectly in one dimension. And if you simply decide to force the width of the image to 150 and leave the height proportional (or vice-versa), then you may exceed the boundaries of 150 pixels.</p>
<p><P>This is a problem.</p>
<p><P>Fortunately, the solution is relatively easy. All we need is a tool that will report the dimensions (and color depth) of any image. I looked out on the web and found several that will do the trick. Of those, some were relatively inexpensive, and some were even free, but all of them were distributed as components. While<br />
there is certainly nothing wrong with that, I wanted something that could run entirely within an ASP page, without needing the ability to register a component on the server (since many people are unable to do so).</p>
<p><P>Therefore, what follows is a set of routines (written in VBScript for your ASP pages) which will determine the height, width, and color depth of any GIF, JPG, BMP or PNG file. Additionally, these routines do not rely on the file extension to determine the graphic file-type. In other words, you can dynamically point this routine to any filename, regardless of name and extension, and determine if is a valid graphic file (actually, there is a very small chance [roughly one in 17 million] that a non-graphic file chosen at random will appear to be a graphics file, so it would be more appropriate to say that this routine determines PROBABLE graphic formats).</p>
<p><P><br />
Here are some possible uses for these routines:<br />
<P><UL><br />
	<LI>intelligent image scaling (as mentioned above)<BR><br />
	<LI>Check user file upload, to see if the graphical image<br />
	  adheres to your requirements (e.g. make your upload function<br />
	  accept only files that are 640&#215;480 or less and 256 colors)<BR><br />
	<LI>perform directory scans and gather information on<br />
	  your graphics files<BR><br />
	<LI>Show graphical directories (using thumbnails) on the web</p>
<p></UL><P><br />
The comments in the routine should be sufficient to help you get started. The only real thing you need to understand is the format of the call to the main routine, <CODE>gfxSpex</CODE>:<br />
<P><TABLE WIDTH=95% BORDER=0><br />
<TR><TD WIDTH=100% BGCOLOR=#CCCCCC><br />
<CODE><br />
	xxx = gfxSpex(strFlnm, lngWidth, lngHeight, lngDepth, strImageType)<br />
</CODE></TD></TR><br />
</TABLE><P><br />
where:<br />
<P><UL><br />
       <LI><CODE>strFlnm</CODE>        => Filespec of file to read<BR></p>
<p>       <LI><CODE>lngWidth</CODE>       => width of image<BR><br />
       <LI><CODE>lngHeight</CODE>      => height of image<BR><br />
       <LI><CODE>lngDepth</CODE>       => color depth (in number of colors)<BR><br />
       <LI><CODE>strImageType</CODE>   => type of image (e.g. GIF, BMP, etc.)</p>
<p></UL></p>
<p><P>The routine returns FALSE if the file was not recognized as a valid image type, and TRUE if it is &#8216;probably&#8217; a valid image file.</p>
<p><P>So there you have it, a complete new tool for your arsenal, with all VBScript source code, and best of all, <B>IT&#8217;S FREE</B>!</p>
<p>Article collateral materials:</p>
<p>&nbsp;&nbsp;&nbsp;<img src="/elements/effendot.gif" width="7" height="7">&nbsp;<a href="/articles/imgsz.html">Download the IMGSZ.ASP source code in text format</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/08/01/determining-image-properties-using-asp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Proportional Image Resizing(within a constrained area)</title>
		<link>http://www.effengud.com/index.php/2009/08/01/proportional-image-resizing/</link>
		<comments>http://www.effengud.com/index.php/2009/08/01/proportional-image-resizing/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 14:59:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=12</guid>
		<description><![CDATA[History
The IMGSZ code presented in my earlier article, Determining Image Properties in ASP, was written in response to my need to be able to fit images proportionally within a given area. My client had asked me to develop a photo gallery application for their site, and the photo gallery was to display photos surrounded by [...]]]></description>
			<content:encoded><![CDATA[<h2>History</h2>
<p>The IMGSZ code presented in my earlier article, <a href="http://www.effengud.com/index.php/2009/08/01/determining-image-properties-using-asp/">Determining Image Properties in ASP</a>, was written in response to my need to be able to fit images proportionally within a given area. My client had asked me to develop a photo gallery application for their site, and the photo gallery was to display photos surrounded by a complex graphical &#8216;photo album&#8217; motif. As a result, all photos (regardless of their original size) needed to &#8216;fit&#8217; within a defined pixelspace.</p>
<p>Many questions ensued: So how would I accomplish that within HTML? Would I need to buy a component? And is  <em>&#8216;pixelspace&#8217;</em> even a real word?  Although I mentioned the possibility of enhancing the IMGSZ routines to allow for proportional resize, I left it up to the readers of that original article to enhance those routines to do the proportional sizing. However, <em>so many</em> of you have asked for the resizing routine that I am pleased (relieved?) to offer it here now.</p>
<h2>Why Do I Need It?</h2>
<p>If you are just learning HTML (or any of its younger variants) you already know that the <code>IMG</code> tag has <code>HEIGHT</code> and <code>WIDTH</code> properties that can be used to force an image to a particular size. However, it has no easy way to do a proportional resize that fits in a desired area. For example, let&#8217;s say you&#8217;re showing images in a table for an on-line product catalog. To make things look nice, you&#8217;re defining the size of the image presented to 100&#215;75 pixels. So you may decide to use <code>WIDTH="100" HEIGHT="75"</code> in your <code>IMG </code>tag for each picture. What will happen is that images may get squashed or stretched in ways that you (and your customers) do not find pleasing. This IMGSZ add-on function will allow you to overcome that problem.</p>
<h2>How Does It Work?</h2>
<p>The <code>ImageResize</code> function is passed an image filename, a maximum width and a maximum height. The function returns either a <code>HEIGHT=xxx</code> or a <code>WIDTH=xxx</code> string that you embed in your IMG tag to provide the proportional resize.</p>
<h2>So How Do I Use It?</h2>
<p>Using the function can be as simple as embedding a function call in your <code>IMG</code> tag. For example, the following code will present an image proportionally in a 100&#215;75 pixel area:</p>
<table border="0" width="95%">
<tbody>
<tr>
<td width="100%" bgcolor="#cccccc"><code><br />
response.write "&lt;img src=""graphic.gif"" " &amp; ImageResize(strImageName, 100, 75) &amp; "&gt;"<br />
</code></td>
</tr>
</tbody>
</table>
<p>The parameters passed to the function are as follows:</p>
<table border="1" cellspacing="1" width="75%" align="center">
<tbody>
<tr>
<th colspan="2"><code>ImageResize</code> Parameters</th>
</tr>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
<tr>
<td><code>strImageName</code></td>
<td>The actual filename (on the server&#8217;s drives)<br />
of the image you will display</td>
</tr>
<tr>
<td><code>intDesiredWidth</code></td>
<td>The width constraint</td>
</tr>
<tr>
<td><code>intDesiredHeight</code></td>
<td>The height constraint</td>
</tr>
</tbody>
</table>
<p>As a further example, let&#8217;s look at some simple test code. This code will look at your server&#8217;s <code>C:\</code> drive, root directory, and display all GIF images that it finds there in a table. The images will be proportionally resized to fit within a 75&#215;45 pixel area. Be sure to <a href="/demos/PropResizeTest.asp">view the live demo</a>!</p>
<table border="0" width="95%">
<tbody>
<tr>
<td width="100%" bgcolor="#cccccc"><code> </code></p>
<pre>&lt;%
   ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   ':::                                                   :::
   ':::  SCRIPT:   testpropresize.asp                     :::
   ':::  AUTHOR:   Mike Shaffer                           :::
   ':::  DATE:     11-Jan-01                              :::
   ':::  PURPOSE:  Test/show the operation of the resize  :::
   ':::            (proportional) function                :::
   ':::                                                   :::
   ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%&gt;
  &lt;!--#INCLUDE FILE='imgsz.asp'--&gt;
  &lt;!--#INCLUDE FILE='propresize.asp'--&gt;
&lt;%
' To test, we'll just try to show all files with a .GIF
' extension in the root of C: by fitting them to a common
' area (75 pixels x 45 pixels)

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

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF = objFSO.GetFolder("c:\")
Set objFC = objF.Files

response.write "&lt;table border=""1"" cellpadding=""5""&gt;"

For Each f1 in objFC
  if instr(ucase(f1.Name), ".GIF") then
     response.write "&lt;tr&gt;&lt;td&gt;" &amp; f1.name &amp; "&lt;/td&gt;&lt;td&gt;" &amp; _
         f1.DateCreated &amp; "&lt;/td&gt;&lt;td&gt;" &amp; f1.Size &amp; "&lt;/td&gt;&lt;td&gt;"

     if gfxSpex(f1.Path, w, h, c, strType) = true then
        response.write w &amp; " x " &amp; h &amp; " " &amp; c &amp; " colors&lt;/td&gt;"
        response.write "&lt;td&gt;&lt;img src=""" &amp; f1.Path &amp; """ " &amp; _
              ImageResize(f1.Path, 75, 45) &amp; " border=""1""&gt;&lt;/td&gt;"
     else
        response.write " &lt;/td&gt;&lt;td align=""center""&gt;bad image&lt;/td&gt;"
     end if

     response.write "&lt;/tr&gt;"

  end if
Next

response.write "&lt;/table&gt;"

set objFC = nothing
set objF = nothing
set objFSO = nothing
%&gt;</pre>
</td>
</tr>
</tbody>
</table>
<p>[<a href="/demos/PropResizeTest.asp">View the live demo!</a>]</p>
<h2>Enough Yakking Already, Show Me The Code!</h2>
<p>The code for the function (below) is quite simple, really. Here is a description of the steps it follows to do its magic:</p>
<ul>
<li>First, call the <code>gfxSpex</code> function (contained in <code>IMGSZ.ASP</code>) to determine<br />
the height and width (and color depth and validity!) of the image.</li>
<li>Determine the target image ratio and the file&#8217;s image ratio<br />
(width divided by depth)</li>
<li>If the file&#8217;s image ratio is greater-than the target image ratio,<br />
then we know that we have to size proportionally based on <code>WIDTH</code>,<br />
otherwise we size proportionally based on <code>HEIGHT</code> (and if it was<br />
not a valid image, return an empty resize string)</li>
</ul>
<p>Oops! I&#8217;m <em>yakking</em> again, here&#8217;s the code, already!</p>
<table border="0" width="95%">
<tbody>
<tr>
<td width="100%" bgcolor="#cccccc"><code> </code></p>
<pre>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 &gt; TargetRatio then  ' We'll scale height
         strResize = "width=""" &amp; intDesiredWidth &amp; """"
      else
         ' We'll scale width
         strResize = "height=""" &amp; intDesiredHeight &amp; """"
      end if
   else
      strResize = ""
   end if

   ImageResize = strResize
end Function</pre>
</td>
</tr>
</tbody>
</table>
<h2>Caveats</h2>
<p>You can integrate the <code>PROPRESIZE.ASP</code> contents into the <code>IMGSZ.ASP</code> code if you like. Note that this does NOT perform an actual resize of the graphical image file, only the visual representation of the image. What this means is that if you have an image that is 800&#215;600x24 bits which is 1 megabyte in size, and you ask the routine to scale the image to a 100&#215;75 pixel area, you will STILL be downloading that entire 1 megabyte of picture information from the server. If you want to do TRUE thumbnail file creation (on disk) you will need to obtain any one of the graphical components available today that offer this feature.</p>
<p><b>Article collateral materials:</b></p>
<p><img src="/elements/effendot.gif" alt="" width="7" height="7" /> <a href="/articles/propresize.html">Download the PROPRESIZE.ASP source code in text format</a><br />
<img src="/elements/effendot.gif" alt="" width="7" height="7" /> <a href="/articles/imgsz.html">Download the IMGSZ.ASP source code in text format</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/08/01/proportional-image-resizing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
