<?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>Web Cash &#187; resize</title>
	<atom:link href="http://www.earn-web-cash.com/tag/resize/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Enable Client-Side Caching of Resized Images</title>
		<link>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</link>
		<comments>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 00:37:44 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</guid>
		<description><![CDATA[About a month ago, I wrote a tutorial on dynamically resizing images in PHP. Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created. However, one reader asked if it was possible to enable a client-side cache of a dynamically resized [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I wrote a tutorial on <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">dynamically resizing images in PHP</a>.  Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created.</p>
<p>However, one reader asked if it was possible to enable a client-side cache of a dynamically resized image.  It surely is, and it&#8217;s very simple.<br />
<span id="more-166"></span></p>
<h4>Dynamic Image Resizing: Revisited</h4>
<p>If you&#8217;ve got no clue how to resize an image in PHP, then you ought to go back and re-read the <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">original article</a>.  For the rest of you, here&#8217;s a quick refresher on what happens.</p>
<p>Our script takes a filename as a $_GET parameter.  If the file exists, the script loads it in as an image resource.  Using the GD library, we create a new image resource that is the proper size.</p>
<p>Finally, we use a content-type header to tell the browser to interpret the data as an image and we output the image data.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Enabling a Client Side Cache</h4>
<p>In order to enable a client-side cache, all we need to do is add an extra header to the mix.</p>
<p>There is a set of headers in HTTP to handle this sort of need &#8211; Cache-Control.  You can use this to enable caching, disable caching, and set some restrictions on what can be cached for how long.</p>
<p>The option we&#8217;re most interested in is max-age=[seconds].  This response header is sent along with the data to tell the browser that it can re-use a cached copy as long as it is no older than the specified age.</p>
<p>To enable this with PHP, add another header() call before the imagepng() call, and set a Cache-Control header with an appropriate max-age.  Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: max-age=3600&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Keep Request Queries Consistent</h4>
<p>With this system, you could set a pretty long max-age for the cache.  Assuming that the original image never changes, the user could cache a resized copy for months and your script would be happy.</p>
<p>However, you need to keep your query strings consistent for this to work.  Since your request includes a query string, that full string is included in the location that is cached.</p>
<p>According to your browser, it&#8217;s not the image that is cached, it&#8217;s the location&#8230;</p>
<pre>http://www.earn-web-cash.com/scripts/resize-image.php?image=http://www.earn-web-cash.com/wp-content/uploads/2008/01/dzone-screenshot.png</pre>
<p>If you were to change the query string in any way &#8211; leaving out the www, for example &#8211; the browser will think it is fetching a new resource and it won&#8217;t consult the cache.</p>
<p>This is especially important if you include size parameters in the query string to dynamically choose the new size.  You would need to be careful to keep the parameters in the same order every time for the cache to work properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

