<?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>Martin's Blog &#187; class</title>
	<atom:link href="http://www.martienus.com/tag/class/feed" rel="self" type="application/rss+xml" />
	<link>http://www.martienus.com</link>
	<description>Logic to Art</description>
	<lastBuildDate>Fri, 12 Feb 2010 19:07:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP: Image Resizer Class</title>
		<link>http://www.martienus.com/code/php-image-resizer-class.html</link>
		<comments>http://www.martienus.com/code/php-image-resizer-class.html#comments</comments>
		<pubDate>Thu, 19 Jun 2008 16:43:46 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=15</guid>
		<description><![CDATA[Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the GD library. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the [...]]]></description>
			<content:encoded><![CDATA[<p>Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the <a href="http://www.php.net/manual/en/book.image.php" target="_blank">GD library</a>. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the site on which they are shown.</p>
<p>Thus we want the image to be resized to sane proportions and dimensions so that we can be sure they fit nicely into our websites&#8217; layout.</p>
<p>Therefore I made an image resizing class myself. It actually resamples the images for better results. Here are some of the features:</p>
<ul>
<li>Handles JPG, GIF, PNG and BMP image files</li>
<li>Can make multiple resized copies</li>
<li>Can simply duplicate the original</li>
<li>Can crop images to a certain aspect ratio, maintaining proportions</li>
<li>Can save to JPG, GIF, PNG and BMP</li>
<li>Can return the image as a string (for i.e. database storage)</li>
<li>Can return the image to the browser</li>
<li>Throws exceptions for unexpected behaviour</li>
</ul>
<p>Here is some code that shows how to utilize the class in your PHP application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;imageresizer.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Increase the allowed memory size for the bigger images</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mem_size'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> imageResizer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'original.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a smaller version of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result.gif'</span><span style="color: #339933;">,</span> GIF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a thumbnail of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thumb_result.png'</span><span style="color: #339933;">,</span> PNG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Retrieve the thumbnail as a string as BMP and show it in the browser as JPG</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getString</span><span style="color: #009900;">&#40;</span>BMP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span>JPG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Catch and display any exceptional behaviour</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy object (executes the destructor) and more importantly, frees up memory</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The resize method can have four values passed to it, namely: maximum width, maximum height, minimum width and minimum height. Either the width or the height should be the same when you use this functionality. Suppose we have the width at 400 pixels. The height of the result should be between 300 and 100 pixels. If we can&#8217;t fit the original in this aspect ratio constraint it will crop off the edges until it fits. This way the image is not stretched or compressed and we have control over both proportions and dimensions.</p>
<p><a href="http://www.martienus.com/wp-content/uploads/imageresizer.txt" target="_blank">Download the imageResizer class here!</a></p>
<p>If you come up with any improvements or bugs please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/php-image-resizer-class.html/feed</wfw:commentRss>
		<slash:comments>8463</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.814 seconds -->

