<?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>Ask Owen &#187; PHP</title>
	<atom:link href="http://askowen.info/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://askowen.info</link>
	<description>Your friendly neighbourhood geek</description>
	<lastBuildDate>Mon, 06 Feb 2012 22:14:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How can I upload an automatically resize an image on my website?</title>
		<link>http://askowen.info/2008/06/how-can-i-upload-an-automatically-resize-an-image-on-my-website/</link>
		<comments>http://askowen.info/2008/06/how-can-i-upload-an-automatically-resize-an-image-on-my-website/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 13:59:18 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[GD library]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://askowen.info/?p=186</guid>
		<description><![CDATA[TweetI often need to upload images to my website, but I&#8217;d like them to be automatically resized when they&#8217;re uploaded. How can I do this?? I got this question from from a colleague of mine who was answering some questions for a client. His client&#8217;s requirements were a bit more complex than just this question, [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton186" class="tw_button" style="float:right;margin-left:10px;"><a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Ftwitter.com%2Fshare%3Furl%3Dhttp%253A%252F%252Faskowen.info%252F2008%252F06%252Fhow-can-i-upload-an-automatically-resize-an-image-on-my-website%252F%26amp%3Btext%3DHow%2520can%2520I%2520upload%2520an%2520automatically%2520resize%2520an%2520image%2520on%2520my%2520website%253F%2520-%2520Ask%2520Owen%26amp%3Brelated%3DOwenC%26amp%3Blang%3Den%26amp%3Bcount%3Dhorizontal%26amp%3Bcounturl%3Dhttp%253A%252F%252Faskowen.info%252F2008%252F06%252Fhow-can-i-upload-an-automatically-resize-an-image-on-my-website%252F&sref=rss" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://askowen.info/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p class="question">I often need to upload images to my website, but I&#8217;d like them to be automatically resized when they&#8217;re uploaded. How can I do this??</p>
<p><span id="more-186"></span><br />
I got this question from from a colleague of mine who was answering some questions for a client. His client&#8217;s requirements were a bit more complex than just this question, but I thought this would be a good one to put up on <a href="http://askowen.info">AskOwen</a> and use it as a tutorial for some interesting PHP techniques. Here&#8217;s some <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPHP&sref=rss">PHP</a> code that will accomplish the request:</p>
<p><code><br />
&lt;?php<br />
// Image uploaded and resizer<br />
// from http://AskOwen.info<br />
?&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Image Uploader and Resizer&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php<br />
if (array_key_exists('btn', $_POST)) {<br />
$uploadedfile = $_FILES['uploadfile']['tmp_name'];<br />
$src = imagecreatefromjpeg($uploadedfile);<br />
list($width,$height)=getimagesize($uploadedfile);<br />
$newwidth=600;<br />
$newheight=($height/$width)*600;<br />
$tmp=imagecreatetruecolor($newwidth,$newheight);<br />
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);<br />
$filename = "images/". $_FILES['uploadfile']['name'];<br />
imagejpeg($tmp,$filename,100);<br />
imagedestroy($src);<br />
imagedestroy($tmp);<br />
}<br />
?&gt;<br />
&lt;form action="upload.php" method="post" enctype="multipart/form-data" &gt;<br />
&lt;input type="file" name="uploadfile"/&gt;<br />
&lt;input id="btn" name="btn" type="submit"/&gt;<br />
&lt;/body&gt;<br />
</code></p>
<p>Here&#8217;s a quick run through the algorithm:</p>
<ol>
<li>Pick up the file that has been uploaded by the form</li>
<li>Get the images width and height</li>
<li>Calculate the correct height to maintain the aspect ratio of the image</li>
<li>Create a memory space for the new image</li>
<li>Use <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fus2.php.net%2Fimagecopyresampled&sref=rss">imagecopyresampled</a>, a PHP function that resizes the image for you, to generate the new image</li>
<li>Write the file to disk</li>
<li>Clean up memory space</li>
</ol>
<p>It&#8217;s quite straight forward once you know that PHP has the graphical functions you need to accomplish this.  It&#8217;s worth nothing that there&#8217;s another PHP function that can resize an image for you also, called <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fus2.php.net%2Fmanual%2Fen%2Ffunction.imagecopyresized.php&sref=rss">imagecopyresized</a>, however this function only uses 255 colours to generate the target image and so doesn&#8217;t maintain the fidelity of the image so well. This may be fine if you have some line art you&#8217;re converting, but if you wanted to resize that beautiful photo you took last time you went on holiday and stayed at one of the <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fwww.destinationvacationhhi.com%2F&sref=rss">Hilton Head rentals</a> you hear so much about, well, you really want as clear an image as possible.</p>
<p>As I said above, this is just the bare bones of an image resizing script, but it illustrates some of the graphical functions that <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fus.php.net%2F&sref=rss">PHP</a> has available (through the <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fwww.boutell.com%2Fgd%2F&sref=rss">GD library</a>) that can help you produce some really useful scripts.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Faskowen.info%2F2008%2F06%2Fhow-can-i-upload-an-automatically-resize-an-image-on-my-website%2F';
  addthis_title  = 'How+can+I+upload+an+automatically+resize+an+image+on+my+website%3F';
  addthis_pub    = 'owencutajar';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://askowen.info/2008/06/how-can-i-upload-an-automatically-resize-an-image-on-my-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How can I generate images with PHP?</title>
		<link>http://askowen.info/2008/03/how-can-i-generate-images-with-php/</link>
		<comments>http://askowen.info/2008/03/how-can-i-generate-images-with-php/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 10:24:29 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://askowen.info/?p=84</guid>
		<description><![CDATA[TweetI&#8217;d like to have an image on my website that gets updated with different text based on the current user. My website is built in PHP. Can I do this? I got a question from a friend who works for a web development company and was building a website for an insurance firm. He was [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton84" class="tw_button" style="float:right;margin-left:10px;"><a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Ftwitter.com%2Fshare%3Furl%3Dhttp%253A%252F%252Faskowen.info%252F2008%252F03%252Fhow-can-i-generate-images-with-php%252F%26amp%3Btext%3DHow%2520can%2520I%2520generate%2520images%2520with%2520PHP%253F%2520-%2520Ask%2520Owen%26amp%3Brelated%3DOwenC%26amp%3Blang%3Den%26amp%3Bcount%3Dhorizontal%26amp%3Bcounturl%3Dhttp%253A%252F%252Faskowen.info%252F2008%252F03%252Fhow-can-i-generate-images-with-php%252F&sref=rss" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://askowen.info/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p class="question">I&#8217;d like to have an image on my website that gets updated with different text based on the current user. My website is built in PHP. Can I do this?</p>
<p><span id="more-84"></span><br />
I got a question from a friend who works for a <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fwww.u-g-h.com&sref=rss">web development</a> company and was building a website for an insurance firm. He was preparing a page that showed off <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fwww.insure.com%2F&sref=rss">car insurance</a> and wanted to have an image with a dynamically generated value on it showing different savings based on user input.</p>
<p>Producing an image with <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fphp.org&sref=rss">PHP</a> isn&#8217;t all that hard. Here&#8217;s some sample code that we&#8217;ll use as reference:<br />
<code>&lt; ?php<br />
header("Content-type: image/png");<br />
$print_value = "Save 15%";  // this can be changed as needed<br />
$img = imagecreatefrompng("backdrop.png");<br />
$orange = imagecolorallocate($img, 220, 210, 60);<br />
$new_pic = (imagesx($img) - 7.5 * strlen($print_value)) / 2;<br />
imagestring($img, 3, $new_pic, 9, $print_value, $orange);<br />
imagepng($img);<br />
imagedestroy($img);<br />
?&gt;</code></p>
<p>That&#8217;s not too many lines of code, is it? And calling it is just as easy. Let&#8217;s say the code above is saved in a file called make_image.php. All you have to do is add the tag<strong> &lt;img src=â€make_image.phpâ€/&gt;</strong> to your HTML code, and you&#8217;re done!</p>
<p>Here&#8217;s a quick explanation of what the code does. The first line sets your <a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fwww.w3.org%2FProtocols%2Frfc2616%2Frfc2616-sec14.html&sref=rss">HTTP header</a> so that a browser will interpret the data coming from the PHP file as an image (.<a href="http://redirectingat.com?id=8352X670472&xs=1&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPortable_Network_Graphics&sref=rss">PNG</a> to be exact). It then proceeds to build the image based on a background image and some text that is specified in the code. This is then output to the browser and memory used for the image cleared at the end of the process. It&#8217;s quite easy to adapt this to your needs once you understand what the code is doing.</p>
<p>Hope this helps!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Faskowen.info%2F2008%2F03%2Fhow-can-i-generate-images-with-php%2F';
  addthis_title  = 'How+can+I+generate+images+with+PHP%3F';
  addthis_pub    = 'owencutajar';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://askowen.info/2008/03/how-can-i-generate-images-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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

