<?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>Random Renegade Bits &#187; php</title>
	<atom:link href="http://rrbits.com/epb/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://rrbits.com/epb</link>
	<description>Whatever comes to mind</description>
	<lastBuildDate>Mon, 30 Jan 2012 08:42:13 +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>PHP: Get zipcode for an Address</title>
		<link>http://rrbits.com/epb/2011/10/13/zipcode-for-address-php-googlemaps/</link>
		<comments>http://rrbits.com/epb/2011/10/13/zipcode-for-address-php-googlemaps/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 02:10:01 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=342</guid>
		<description><![CDATA[I needed a small php function that could take an address (which lacked a zip code) and fetch a best guess postal code for it. I had a database full of addresses with city and state, but no zip code. Thankfully, if you have enough information in your address to locate it with the Google [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a small php function that could take an address (which lacked a zip code) and fetch a best guess postal code for it.  I had a database full of addresses with city and state, but no zip code.  Thankfully, if you have enough information in your address to locate it with the Google maps geocoding service, you have enough information to get the zip code.  If not the correct zip code, it should pull one that&#8217;s relatively close assuming you&#8217;ve specified the correct city and state in your address.</p>
<p>This is possible because the geocoding service will return matches with full address data available.  If we assume that the first match is most likely the correct match, we can just extract it&#8217;s postal code. I&#8217;ve only tested this with US addresses, but it may work in other countries where Google can geocode an address.</p>
<p><span id="more-342"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Uses Google Maps Geocoder (V3) to get postal code for an address.  Uses first address match.
 * @param String Address - Make sure to include at least the street address, city and state.
 * @return String - Zip code on success, blank if a zip code is failed to be retrieved.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> zip_code_from_address<span style="color: #009900;">&#40;</span><span style="color: #000088;">$address</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$ura</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$rurl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&amp;address=<span style="color: #006699; font-weight: bold;">$ura</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadXML</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rurl</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'OK'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//If status is OK, then at least one result should be here.</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$postalcode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'address_component'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$comp</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
               <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'postal_code'</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                  <span style="color: #000088;">$postalcode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$comp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'short_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$postalcode</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">else</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2011/10/13/zipcode-for-address-php-googlemaps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I feel cryptastic</title>
		<link>http://rrbits.com/epb/2011/04/17/i-feel-cryptastic/</link>
		<comments>http://rrbits.com/epb/2011/04/17/i-feel-cryptastic/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 19:30:50 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[mcrypt]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=338</guid>
		<description><![CDATA[Looking for a quick and easy to use encryption class for PHP?  I stumbled upon Cryptastic in my searches for example code to use with mcrypt for a personal project.  I must say it&#8217;s quite handy. It implements a message integrity check and handles the initialization vector stuff for you making encrypting data as easy [...]]]></description>
			<content:encoded><![CDATA[<p>Looking for a quick and easy to use encryption class for PHP?  I stumbled upon <a href="http://www.itnewb.com/v/PHP-Encryption-Decryption-Using-the-MCrypt-Library-libmcrypt">Cryptastic </a>in my searches for example code to use with mcrypt for a personal project.  I must say it&#8217;s quite handy. It implements a message integrity check and handles the initialization vector stuff for you making encrypting data as easy as a single call.</p>
]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2011/04/17/i-feel-cryptastic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get the Bounding Box of an SVG Path</title>
		<link>http://rrbits.com/epb/2011/02/06/get-the-bounding-box-of-an-svg-path/</link>
		<comments>http://rrbits.com/epb/2011/02/06/get-the-bounding-box-of-an-svg-path/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 00:39:04 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[hopefully_that_made_sense]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=325</guid>
		<description><![CDATA[There may be times you need to find the bounding box of an SVG path. One way to find it would be to parse the path and then apply any group transformation on it to find the range of X and Y coordinates the object occupies. However that&#8217;s a bit of code, and I&#8217;m quite [...]]]></description>
			<content:encoded><![CDATA[<p>There may be times you need to find the bounding box of an SVG path.  One way to find it would be to parse the path and then apply any group transformation on it to find the range of X and Y coordinates the object occupies.  However that&#8217;s a bit of code, and I&#8217;m quite a bit lazy, so I decided to try another approach: rasterize the path object by itself and find where in the image it was drawn.<br />
<span id="more-325"></span><br />
To do this, basically, all you need to do is erase all the other content from the document, leaving only the path you want to find the bounding box of and then trim the sides off with ImageMagick and have it tell you the info instead of generating an image.</p>
<p>Here&#8217;s an example ImageMagick convert command line:</p>
<pre>bash:~&gt;convert -density 200 pumpkinsheet.svg -trim info:-
pumpkinsheet.svg SVG 1813x2244 2066x2922+207+576 16-bit DirectClass 0.070u 0:00.029</pre>
<p>First you have the file name, type, resulting crop size, original canvas size and the offsets where the crop starts.  Respectively: pumpkinsheet.svg; SVG; 1813 x 2244; 2066&#215;2922; and 207,586.</p>
<p>You can then find the bounding offsets of the drawn object:</p>
<p>Top left X: 207 (horizontal offset)<br />
Top left Y: 576 (vertical offset)<br />
Bottom right X: 207 + 1813 (horizontal offset + cropped width)<br />
Bottom right Y: 576 + 2244 (vertical offset + cropped height)</p>
<p>Unless you use the default density (72) these canvas sizes won&#8217;t match up to what you likely have in your SVG set as the width and height, so convert them to percentages by dividing them by the original width (for the Xs) and the original height (for the Ys).</p>
<p>Once you have the percentages, multiply the width and height of your SVG if you need it in absolute points of the SVG instead of percentages.</p>
<p>A couple of caveats with this method:</p>
<ul>
<li>If you have borders, they will add to the bounding box.</li>
<li>This will clip any path that falls outside the canvas unless you expand the canvas of the single-node object.</li>
<li>You&#8217;ll need to preserve patterns and gradients if you want those patterns and gradients to play a role in your bounding calculation.</li>
<li>Using higher densities will get you more sub-pixel accurate results, but will consume more memory in the process.</li>
</ul>
<p>I have some rough code I&#8217;m using in my own project below.  I just use the percentages for my purposes, as such the discover_bounding_box only returns the bounding coordinates as percentages.  The create_single_object_svg function could function differently as well.  Instead of making a copy of the SVG, it could just loop through the SVG&#8217;s DOM and remove any node that would result in drawing except our desired object.  This would make it fairly easy to preserve patterns and gradients.  It uses proc_open and DO NOT run this on a server with autoregister globals on.  That is a recipe for disaster.  The code below is provided for demonstration purposes only and should not necessarily be expected to work as you expect without modification.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Determines the bounding box of a specific path, rectangle, polygon, or ellipses
 * in an SVG.  Accepts the SVG's DOM, and the node specifically desired to be checked.
 * Assumes convert is at /usr/bin/convert unless a global called $convertpath is set.
 *
 * @param DOMDocument $svg SVG DOMDocument to use to find this item's bounding box.
 * @param DOMDocument $object Node that we want to find the bounding box for.
 * @param Float $precision Precision desired, 1 is rougly pixel-accurate, between 0.1 and 1 is less than pixel accurate
 *   and anything above 1 should be sub-pixel accurate.  (Max is 3)
 * @return Mixed Boolean false on failure, Array with topleft-x, topleft-y, bottomright-x, bottomright-y in percentages
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> discover_bounding_box<span style="color: #009900;">&#40;</span><span style="color: #000088;">$svg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$convertpath</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$convert</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$convertpath</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$convertpath</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'/usr/bin/convert'</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// We assume density 72 is the pixel-size accurate </span>
   <span style="color: #000088;">$density</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$precision</span> <span style="color: #339933;">&gt;=</span> <span style="color:#800080;">0.1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$precision</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$density</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">72</span><span style="color: #339933;">*</span><span style="color: #000088;">$precision</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000088;">$singlesvg</span> <span style="color: #339933;">=</span> create_single_object_svg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$svg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
   <span style="color: #666666; font-style: italic;">//Now that we've got the SVG in hand, let's pass it on to process image.</span>
   <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   process_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$singlesvg</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-trim'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'svg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'info'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;-density <span style="color: #006699; font-weight: bold;">$density</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ob_get_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Tokenize the fragments of text from our friend, ImageMagick.</span>
   <span style="color: #000088;">$fragments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trimmed_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$trimmed_height</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">//If both intval'd are not 0, then we can assume that all when well.</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$trimmed_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trimmed_width</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$trimmed_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trimmed_height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//Now get the original width, original height, clip offsetx and clip offsety</span>
      <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$original_height</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clip_offsetx</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clip_offsety</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> 
         <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_intval'</span><span style="color: #339933;">,</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'+'</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'+'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$bounding_top_left_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$clip_offsetx</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$bounding_top_left_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$clip_offsety</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_height</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$bounding_bottom_right_x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$clip_offsetx</span><span style="color: #339933;">+</span><span style="color: #000088;">$trimmed_width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$bounding_bottom_right_y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$clip_offsety</span><span style="color: #339933;">+</span><span style="color: #000088;">$trimmed_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_height</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bounding_top_left_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bounding_top_left_y</span><span style="color: #339933;">,</span> 
         <span style="color: #000088;">$bounding_bottom_right_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bounding_bottom_right_y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Creates an extract from an SVG based on an input DOM and desired object.
 * @param DOMDocument $svg A fully loaded DOMDocument for the SVG.
 * @param DOMNode $object Target node to extract.
 * @return String Returns an SVG file containing only the desired object.  At present
 * this function also removes patterns and gradients which may be a notable problem in 
 * cases where you want to find the visible bounding box of an object, not necessarily
 * it's exact box.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> create_single_object_svg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$svg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//Get the width and height of the current document.</span>
   <span style="color: #000088;">$root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$svg</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'svg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//It's best if you have a solid color set in the style or fill attribute</span>
   <span style="color: #666666; font-style: italic;">//but it shouldn't be that big a deal.   </span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Get an transforms from group nodes.</span>
   <span style="color: #000088;">$transforms</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$object</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentNode</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//Continue climbing until there's no where else to climb to.</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentNode</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'g'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$transforms</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'transform'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Run stuff through image_magick uses the global convert path settings.  Outputs image on success (and sets header)
 * @param String $imgdata Raw image file data to pipe to image magick.
 * @param String $processing_instruction Command line options to pass to image magick's convert program.
 * @param String $inputtype Default: jpg Input image format.
 * @param String $outputtype Default: jpg Output image format.
 * @param String $preload_instructions Add instructions that go before the file to load.
 * @return void
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> process_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$imgdata</span><span style="color: #339933;">,</span> <span style="color: #000088;">$processing_instruction</span><span style="color: #339933;">,</span> <span style="color: #000088;">$inputtype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'jpg'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$outputtype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'jpg'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preload_instructions</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$convertpath</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'proc_open'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//Let's just pipe to imagemagick to handle our graphics transform shall we?</span>
      <span style="color: #000088;">$descriptors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
         <span style="color: #0000ff;">'0'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pipe&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
         <span style="color: #0000ff;">'1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pipe&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
         <span style="color: #0000ff;">'2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/dev/null&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$pipes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$process</span> <span style="color: #339933;">=</span> <span style="color: #990000;">proc_open</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$convertpath</span> <span style="color: #006699; font-weight: bold;">$preload_instructions</span> <span style="color: #006699; font-weight: bold;">{$inputtype}</span>:- <span style="color: #006699; font-weight: bold;">$processing_instruction</span> <span style="color: #006699; font-weight: bold;">{$outputtype}</span>:-&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$descriptors</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pipes</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'binary_pipes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//die($command);</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$process</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pipes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$imgdata</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #666666; font-style: italic;">//fwrite($pipes[0], EOF);</span>
         <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pipes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pipes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #990000;">proc_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$process</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">50</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$outputtype</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'info'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">else</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 500 Internal Error'</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;">'content-type: text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Failed to process image.'</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">//Poor process died.  Should we do something?</span>
         <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 500 Internal Error'</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;">'content-type: text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Failed to process image.'</span><span style="color: #339933;">;</span>               
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2011/02/06/get-the-bounding-box-of-an-svg-path/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Resize and Crop to a specific width x height with a crop suggestion point.</title>
		<link>http://rrbits.com/epb/2010/07/18/resize-and-crop/</link>
		<comments>http://rrbits.com/epb/2010/07/18/resize-and-crop/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 03:15:10 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=190</guid>
		<description><![CDATA[I really don&#8217;t have a shorter description for this.  I was working on a personal project the other day and realized that this might be a nifty function.  Basically, you know all those snippets to make cropped thumbnails (squares and the like), and how most of them either center or crop to the top of [...]]]></description>
			<content:encoded><![CDATA[<p>I really don&#8217;t have a shorter description for this.  I was working on a personal project the other day and realized that this might be a nifty function.  Basically, you know all those snippets to make cropped thumbnails (squares and the like), and how most of them either center or crop to the top of an image.  I wanted a new function that would crop to most any size I desired and any position on the image I desired without the need to change any code, only parameters.  And thus img_resize_crop was born.</p>
<p><span id="more-190"></span>Function code is below, try out a demo at <a href="http://rrbits.com/resizer/">http://rrbits.com/resizer/</a> (Demo code is available here: <a href="http://rrbits.com/resizer.zip">http://rrbits.com/resizer.zip</a> )</p>
<p>Right now it&#8217;s pure GD (uses a sharpening convolution matrix to get over GD&#8217;s resizing blurryness), though could probably be converted to ImageMagick by someone who knows more about ImageMagick than I.  (Plus more patience, no desire to port it right now.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * img_resize_crop
 * Resizes an image to a specific size, cropping if necessary, allows center-weighting.
 *
 * @param Resource $src - GD Resource Image to use.
 * @param Integer $twidth - Thumbnail width
 * @param Integer $theight - Thumbnail height
 * @param Integer $center_x - The x value of the target center location, leaving it at -1 autocalculates. [Default: -1]
 * @param Integer $center_y - The y value of the target center location, leaving it at -1 autocalculates. [Default: -1]
 * @param String $method - Method to use for the resize.  [Default:suggest]
 *   'suggest' (provided center coordinate is only guaranteed to be in the picture.)
 *   'force' (The provided coordinate is forced into being the center, additional data is trimmed to ensure this.)
 *   'onlycrop' (Not sure why you would use this, but only performs cropping.)
 * @param Boolean $sharpen - Specify whether or not to sharpen the resultant thumbnail [Default:true]
 * @param Int $evil_threshold - Maximum thumbnail width/height to prevent attempts at evil. [Default:1024]
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> img_resize_crop<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #339933;">,</span> <span style="color: #000088;">$twidth</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theight</span><span style="color: #339933;">,</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> 
    <span style="color: #000088;">$center_y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'suggest'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sharpen</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$evil_threshold</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">/*Set defaults if they are not provided.*/</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'force'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$center_x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'force'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$center_y</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</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;">/* Prevent evil */</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twidth</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$evil_threshold</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$twidth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$evil_threshold</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theight</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$evil_threshold</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$theight</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$evil_threshold</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
   <span style="color: #666666; font-style: italic;">/*  The ratio of the thumbnail and the ratio 
    of the original image.*/</span>
   <span style="color: #000088;">$t_ratio</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$twidth</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$theight</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$o_ratio</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$new</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twidth</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theight</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/*Ideally, we'll want to grab as much of 
     the source image as possible.
&nbsp;
     So we need to figure out how much to grab based on 
     the ratios of the desired thumbnail and the original
     image.*/</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t_ratio</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$o_ratio</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$t_ratio</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$t_ratio</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;">/*Calculate the upper-left corner of the 
     source image grab area.  These will be 
     adjusted differently based on the method selected.*/</span>
   <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
   <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'onlycrop'</span><span style="color: #339933;">:</span>  
         <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$twidth</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$twidth</span> <span style="color: #339933;">:</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$theight</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$theight</span> <span style="color: #339933;">:</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
&nbsp;
         <span style="color: #666666; font-style: italic;">/*Make sure we're inside the left and top boundaries.*/</span>
         <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$s_x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_x</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$s_y</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_y</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">/*Make sure we're inside the bottom and right boundaries.*/</span>
         <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">+</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i_width</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_x</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">+</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i_height</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_y</span><span style="color: #339933;">;</span>  
&nbsp;
         <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>      
      <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'suggest'</span><span style="color: #339933;">:</span>
         <span style="color: #666666; font-style: italic;">/*Condition: Crop is thinner (ratio-wise) than the original image.*/</span>
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t_ratio</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$o_ratio</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$s_x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_x</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">+</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i_width</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_x</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">/*Condition: Crop is wider (ratio-wise) than the original image.*/</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$s_y</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_y</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">+</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i_height</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_y</span><span style="color: #339933;">;</span>   
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'force'</span><span style="color: #339933;">:</span>
         <span style="color: #666666; font-style: italic;">/*Condition: Crop is thinner (ratio-wise) than the original image.*/</span>
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t_ratio</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$o_ratio</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/*Find the maximum possible height with the requested center_y.*/</span>
            <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$center_y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$t_ratio</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/*Height for said width.*/</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/*If that puts the source box out of bounds, we'll need to proportionally scale down.*/</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">+</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">/*Minimum width.*/</span>
               <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$center_x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$t_ratio</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">/*Recalculate source x and y*/</span>
            <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>            
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">/*Condition: Crop is wider (ratio-wise) than the original image.*/</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/*Find the maximum possible width with the requested center_x.*/</span>
            <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_x</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$center_x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$t_ratio</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/*Height for said width.*/</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/*If that puts the source box out of bounds, we'll need to proportionally scale down.*/</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">+</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">/*Minimum height.*/</span>
               <span style="color: #000088;">$i_height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center_y</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$center_y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$i_width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i_height</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$t_ratio</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">/*Recalculate source x and y*/</span>
            <span style="color: #000088;">$s_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_x</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_width</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$s_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$center_y</span> <span style="color: #339933;">-</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_height</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src</span><span style="color: #339933;">,</span> 
     <span style="color: #666666; font-style: italic;">/*Destination Coords*/</span>
     <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
     <span style="color: #666666; font-style: italic;">/*Source Coords*/</span>
     <span style="color: #000088;">$s_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$s_y</span><span style="color: #339933;">,</span>
     <span style="color: #666666; font-style: italic;">/*Destination Geometry*/</span>
     <span style="color: #000088;">$twidth</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theight</span><span style="color: #339933;">,</span>
     <span style="color: #666666; font-style: italic;">/*Source Geometry*/</span>
     <span style="color: #000088;">$i_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i_height</span>
     <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sharpen</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$sharpen_matrix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$divisor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #990000;">imageconvolution</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sharpen_matrix</span><span style="color: #339933;">,</span> <span style="color: #000088;">$divisor</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$new</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2010/07/18/resize-and-crop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embed Album Art into an MP3 with PHP</title>
		<link>http://rrbits.com/epb/2010/05/31/embed-album-art-into-an-mp3-with-php/</link>
		<comments>http://rrbits.com/epb/2010/05/31/embed-album-art-into-an-mp3-with-php/#comments</comments>
		<pubDate>Mon, 31 May 2010 10:55:11 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=178</guid>
		<description><![CDATA[An odd quest perhaps: but adding album art to an MP3 (in ID3 tags) is readily doable with the KTaglib extension for PHP.  Once you have it, you can use a script like this to add artwork to an MP3: $mp3 = new KTaglib_MPEG_File('2.mp3'); $idv2 = $mp3-&#62;getID3v2Tag(true); $album = new KTaglib_ID3v2_AttachedPictureFrame(); $album-&#62;setType(KTaglib_ID3v2_AttachedPictureFrame::FrontCover); $album-&#62;setPicture('testalbum.jpg'); $album-&#62;setMimeType('image/jpeg'); $idv2-&#62;addFrame($album); [...]]]></description>
			<content:encoded><![CDATA[<p>An odd quest perhaps: but adding album art to an MP3 (in ID3 tags) is readily doable with the KTaglib extension for PHP.  Once you have it, you can use a script like this to add artwork to an MP3:</p>
<pre>$mp3 = new KTaglib_MPEG_File('2.mp3');
$idv2 = $mp3-&gt;<a href="http://www.php.net/manual/en/mpegfile.getId3v2Tag.php">getID3v2Tag</a>(true);
$album = new KTaglib_ID3v2_AttachedPictureFrame();
$album-&gt;setType(KTaglib_ID3v2_AttachedPictureFrame::FrontCover);
$album-&gt;setPicture('testalbum.jpg');
$album-&gt;setMimeType('image/jpeg');
$idv2-&gt;addFrame($album);
$mp3-&gt;save();</pre>
<p>Assuming no exceptions are thrown, this should allow you to set an image of your choosing as a FrontCover image  (of course replacing my example values with your own real values), consult the <a href="http://www.php.net/manual/en/book.ktaglib.php">KTaglib documentation</a> for more details and check &#8220;Predefined Constants&#8221; for the other image types.</p>
]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2010/05/31/embed-album-art-into-an-mp3-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SVG to online coloring-sheet SWF</title>
		<link>http://rrbits.com/epb/2010/03/12/svg-to-online-coloring-sheet-swf/</link>
		<comments>http://rrbits.com/epb/2010/03/12/svg-to-online-coloring-sheet-swf/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 02:11:07 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=163</guid>
		<description><![CDATA[You can do quite a bit of unorthodox stuff with PHP.  If you head over to epbsoft.com, you&#8217;ll find several flash coloring sheets I made with PHP, not Flash.  (Sort of anyway.)  I make them by first creating specially formatted SVG (Scalable Vector Graphics) files and feeding them to a PHP script that writes ActionScript [...]]]></description>
			<content:encoded><![CDATA[<p>You can do quite a bit of unorthodox stuff with PHP.  If you head over to epbsoft.com, you&#8217;ll find several <a href="http://epbsoft.com/coloring-sheets/">flash coloring sheets</a> I made with PHP, not Flash.  (Sort of anyway.)  I make them by first creating specially formatted SVG (Scalable Vector Graphics) files and feeding them to a PHP script that writes ActionScript files that are then compiled by MotionTwin ActionScript compiler.  As far as capabilities, it&#8217;s not all that capable.  I have to jump through a couple of hoops to make an SVG that my ActionScript generator can actually parse properly, but when it works, I like the results.</p>
<p><a href="http://rrbits.com/epb/files/2010/03/svg-to-coloring.php_.zip">svg-to-coloring.php.zip</a> &lt;&#8211; The source code is available if you want to take a look at how it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2010/03/12/svg-to-online-coloring-sheet-swf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB looks Interesting</title>
		<link>http://rrbits.com/epb/2010/03/03/mongodb-looks-interesting/</link>
		<comments>http://rrbits.com/epb/2010/03/03/mongodb-looks-interesting/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 21:12:02 +0000</pubDate>
		<dc:creator>epb</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rambling]]></category>

		<guid isPermaLink="false">http://rrbits.com/epb/?p=160</guid>
		<description><![CDATA[I might take a look at installing MongoDB on my server.  I like the idea of a document-driven, NoSQL database, especially after working with relational SQL databases for years, and MongoDB looks interesting.  You can read more about it in Developing scalable PHP Applications using Mongo DB. What caught my interest is how flexible using [...]]]></description>
			<content:encoded><![CDATA[<p>I might take a look at installing MongoDB on my server.  I like the idea of a document-driven, NoSQL database, especially after working with relational SQL databases for years, and MongoDB looks interesting.  You can read more about it in <a href="http://www.phpclasses.org/blog/post/118-Developing-scalable-PHP-applications-using-MongoDB.html"><em>Developing scalable PHP Applications using Mongo DB</em></a>. What caught my interest is how flexible using it appears to be from the linked article.  Plus, using something other than a SQL database could be a nice change of pace.  I HATE adding fields to a SQL database table, and it looks nearly painless in these examples.</p>
<p>Another thing that seemed as though it were worth looking into is it&#8217;s apparently designed with storing whole files in mind.  I&#8217;ve stored files in MySQL databases before, but MySQL really isn&#8217;t designed with that in mind, but at the same time, if I&#8217;m going to be making a lot of small binary files, I don&#8217;t like them cluttering up my directories.</p>
]]></content:encoded>
			<wfw:commentRss>http://rrbits.com/epb/2010/03/03/mongodb-looks-interesting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

