<?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>Perplexed Labs &#187; images</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/images/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tumblelogs, Image Manipulation, and You</title>
		<link>http://blog.perplexedlabs.com/2009/02/12/tumblelogs-image-manipulation-and-you/</link>
		<comments>http://blog.perplexedlabs.com/2009/02/12/tumblelogs-image-manipulation-and-you/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 19:49:17 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[tumbelog]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=185</guid>
		<description><![CDATA[After trying for many years to maintain a personal blog, with many starts and stop, I have given up. I realized I don't *want* to write personal thoughts on the web. Instead, I've been messing around with my own custom tumblelog scripts. I use delicious, Flickr, last.fm, and other services that I want to aggregate [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/04/21/37signals-and-php/' rel='bookmark' title='Permanent Link: 37Signals and PHP?'>37Signals and PHP?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>After trying for many years to maintain a personal blog, with many starts and stop, I have given up.  I realized I don't *want* to write personal thoughts on the web.  Instead, I've been messing around with my own custom tumblelog scripts.  I use delicious, Flickr, last.fm, and other services that I want to aggregate in one place.  I know there are services out there that do this already, but I thought rolling my own would be a good excuse to dust off my PHP skills.</p>
<p>One feature I wanted to have was the ability to grab images from other sites, re-size them, and copy them to my local server.    Nothing special, but very useful.</p>
<pre class="brush: php;">$remote_image = &quot;some_img_url&quot;;

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $remote_image);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);

$file_as_string = curl_exec($ch);
$new_file_name =  substr(strrchr($remote_image, &quot;/&quot;), 1);
$path_to_img = '/path/to/image/folder/'.$new_file_name;

resizeImage($file_as_string, 600, $path_to_img);

curl_close($ch);

function resizeImage($img_as_string, $max_width, $dst){
   $image = imagecreatefromstring($img_as_string);
   $orig_width = imagesx($image);
   $orig_height = imagesy($image);

   $width = $orig_width;
   $height = $orig_height;

   if ($orig_width &gt; $max_width){
      $height = ($orig_height * $max_width) / $orig_width;
      $width = $max_width;
   }			

   $image_p = imagecreatetruecolor($width, $height);

   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);

   imagejpeg($image_p, $dst, 100);
}</pre>
<p>What this script does is:</p>
<ul>
<li>using curl, grab the image remotely (as a string)</li>
<li>parse the name of the image, create a string that represents the server path to copy the image to</li>
<li>proportionally resize the image if it's wider than 600 pixels</li>
<li>save it</li>
</ul>
<p>This code can be easily modified to resize the image if it's taller than x pixels.  I made a very simple form, so now I can supply the script with the image url, and it takes care of the rest.</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/04/21/37signals-and-php/' rel='bookmark' title='Permanent Link: 37Signals and PHP?'>37Signals and PHP?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/02/12/tumblelogs-image-manipulation-and-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
