<?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>Moki Systems Blog &#187; JavaScript</title>
	<atom:link href="http://www.mokisystems.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mokisystems.com/blog</link>
	<description>Addressing such topics as: web programming, design, ruby on rails, cake php, postgresql, linux, seo</description>
	<lastBuildDate>Thu, 19 Aug 2010 00:18:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zebra Striping with jQuery</title>
		<link>http://www.mokisystems.com/blog/zebra-striping-with-jquery/</link>
		<comments>http://www.mokisystems.com/blog/zebra-striping-with-jquery/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 17:09:02 +0000</pubDate>
		<dc:creator>Wes Bangerter</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.mokisystems.com/blog/?p=43</guid>
		<description><![CDATA[Zebra Striping a table with jQuery is ridiculously easy. This code will add even and odd classes to every row in all tables:

$&#40;document&#41;.ready&#40;function&#40;&#41;&#123;
  // Zebra stripe our tables
  $&#40;&#34;table tr:odd&#34;&#41;.addClass&#40;&#34;odd&#34;&#41;;
  $&#40;&#34;table tr:even&#34;&#41;.addClass&#40;&#34;even&#34;&#41;;
&#125;&#41;;

You can make it more selective by changing the table part in $("table tr:odd") to something like table.stripe, so it will [...]]]></description>
			<content:encoded><![CDATA[<p>Zebra Striping a table with <a href="http://jquery.com/">jQuery</a> is ridiculously easy. This code will add even and odd classes to every row in all tables:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Zebra stripe our tables</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table tr:odd&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;odd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table tr:even&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;even&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can make it more selective by changing the <code>table</code> part in <code>$("table tr:odd")</code> to something like <code>table.stripe</code>, so it will only apply to tables with a <code>stripe</code> class.</p>
<p>Of course you&#8217;ll have to throw in some CSS so the even and odd classes actually do something:</p>

<div class="wp_syntax"><div class="code"><pre class="css">table tr<span style="color: #6666ff;">.odd</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #66cc66;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
table tr<span style="color: #6666ff;">.even</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #66cc66;">:</span> <span style="color: #cc00cc;">#f3f3f3</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mokisystems.com/blog/zebra-striping-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Markers to a Google Map With Ruby on Rails and JSON</title>
		<link>http://www.mokisystems.com/blog/add-markers-to-a-google-map-with-ruby-on-rails-and-json/</link>
		<comments>http://www.mokisystems.com/blog/add-markers-to-a-google-map-with-ruby-on-rails-and-json/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 21:23:45 +0000</pubDate>
		<dc:creator>Wes Bangerter</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.mokisystems.com/blog/?p=27</guid>
		<description><![CDATA[This tutorial will guide you through creating a map using the Google Maps API that will be dynamically populated with markers as the user zooms or scrolls around the map.
For this example, we&#8217;re going to create and use a generic Location model.
Geocoding Your Addresses
Geocoding will translate an address into its approximate latitude and longitude.
We&#8217;ll use [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will guide you through creating a map using the Google Maps API that will be dynamically populated with markers as the user zooms or scrolls around the map.</p>
<p>For this example, we&#8217;re going to create and use a generic <code>Location</code> model.</p>
<h3>Geocoding Your Addresses</h3>
<p>Geocoding will translate an address into its approximate latitude and longitude.</p>
<p>We&#8217;ll use <a href="http://geokit.rubyforge.org/">GeoKit</a>, a Ruby on Rails plugin to geocode our addresses. Install it with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">script<span style="color: #000000; font-weight: bold;">/</span>plugin <span style="color: #c20cb9; font-weight: bold;">install</span> svn:<span style="color: #000000; font-weight: bold;">//</span>rubyforge.org<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>geokit<span style="color: #000000; font-weight: bold;">/</span>trunk</pre></div></div>

<p>Follow the instructions to obtain and install your own Google API key.</p>
<p>Generate the model, controller and views for our map locations:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">script<span style="color: #000000; font-weight: bold;">/</span>generate scaffold location name:string address:string city:string \
state:string <span style="color: #c20cb9; font-weight: bold;">zip</span>:string</pre></div></div>

<p>We also need to edit the migration file for this model and add fields for the location&#8217;s latitude and longitude:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">t.<span style="color:#9900CC;">decimal</span> <span style="color:#ff3333; font-weight:bold;">:lat</span>, <span style="color:#ff3333; font-weight:bold;">:precision</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">15</span>, <span style="color:#ff3333; font-weight:bold;">:scale</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">12</span>
t.<span style="color:#9900CC;">decimal</span> <span style="color:#ff3333; font-weight:bold;">:lng</span>, <span style="color:#ff3333; font-weight:bold;">:precision</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">15</span>, <span style="color:#ff3333; font-weight:bold;">:scale</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">12</span></pre></div></div>

<p>Replace the code in <code>app/models/location.rb</code> with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Location <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  acts_as_mappable
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:address</span>, <span style="color:#ff3333; font-weight:bold;">:city</span>, <span style="color:#ff3333; font-weight:bold;">:state</span>, <span style="color:#ff3333; font-weight:bold;">:zip</span>, <span style="color:#ff3333; font-weight:bold;">:lat</span>, <span style="color:#ff3333; font-weight:bold;">:lng</span>
  before_validation_on_create <span style="color:#ff3333; font-weight:bold;">:geocode_address</span>
&nbsp;
  private
    <span style="color:#9966CC; font-weight:bold;">def</span> geocode_address
      geo=<span style="color:#6666ff; font-weight:bold;">GeoKit::Geocoders::MultiGeocoder</span>.<span style="color:#9900CC;">geocode</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{address} #{city} #{state} #{zip}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:address</span>, <span style="color:#996600;">&quot;Could not Geocode address&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !geo.<span style="color:#9900CC;">success</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">lat</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">lng</span> = geo.<span style="color:#9900CC;">lat</span>,geo.<span style="color:#9900CC;">lng</span> <span style="color:#9966CC; font-weight:bold;">if</span> geo.<span style="color:#9900CC;">success</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That&#8217;s all there is to geocoding, now any time we create a Location it will automatically be assigned a latitude and longitude.</p>
<h3>Adding the Google Map</h3>
<p>In <code>app/views/locations/index.html.erb</code> add:</p>

<div class="wp_syntax"><div class="code"><pre class="css">&lt;div id=<span style="color: #ff0000;">&quot;map&quot;</span> style=<span style="color: #ff0000;">&quot;width: 890px; height: 600px;&quot;</span>&gt;&lt;/div&gt;</pre></div></div>

<p>And in <code>app/views/controllers/locations_controller.rb</code>, change the index action to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;"># GET /locations</span>
<span style="color:#008000; font-style:italic;"># GET /locations.xml</span>
<span style="color:#008000; font-style:italic;"># GET /locations.js</span>
<span style="color:#9966CC; font-weight:bold;">def</span> index
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@locations</span> = Location.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@locations</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">js</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      ne = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:ne</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span>e.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#125;</span>  
      sw = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sw</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span>e.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#0066ff; font-weight:bold;">@locations</span> = Location.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span>, <span style="color:#ff3333; font-weight:bold;">:bounds</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>sw, ne<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@locations</span>.<span style="color:#9900CC;">to_json</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The index action will now respond to javascript requests with a JSON object containing the first 100 Locations inside of the map boundaries.</p>
<p>In your layout file, add this code inside of the <code>&lt;head&gt;</code> tag:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@locations</span>.<span style="color:#9900CC;">blank</span>? <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;</span>script
    src=<span style="color:#996600;">&quot;http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=&lt;%= GeoKit::Geocoders::google -%&gt;&quot;</span>
    type=<span style="color:#996600;">&quot;text/javascript&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>script<span style="color:#006600; font-weight:bold;">&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= javascript_include_tag <span style="color:#996600;">'prototype'</span>, <span style="color:#996600;">'maps'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>And finally, create a <code>public/javascripts/maps.js</code> file with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">window.<span style="color: #000066;">onunload</span> <span style="color: #339933;">=</span> GUnload<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> map<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> markers <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Event.<span style="color: #006600;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Center the map on the US</span>
    map.<span style="color: #006600;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">37.731145</span><span style="color: #339933;">,</span><span style="color: #CC0000;">-97.326092</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    GEvent.<span style="color: #006600;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;moveend&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>updateMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    map.<span style="color: #006600;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    map.<span style="color: #006600;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GMapTypeControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    updateMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> updateMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> map.<span style="color: #006600;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> southWest <span style="color: #339933;">=</span> bounds.<span style="color: #006600;">getSouthWest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> northEast <span style="color: #339933;">=</span> bounds.<span style="color: #006600;">getNorthEast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Send an AJAX request for our locations</span>
  <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/locations.js'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    method<span style="color: #339933;">:</span><span style="color: #3366CC;">'get'</span><span style="color: #339933;">,</span>
    parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>sw<span style="color: #339933;">:</span> southWest.<span style="color: #006600;">toUrlValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ne<span style="color: #339933;">:</span> northEast.<span style="color: #006600;">toUrlValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>transport<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// Remove markers outside of our maps boundaries.</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>markers.<span style="color: #006600;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        removeMarkersOutsideOfMapBounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Add our new markers to the map (unless they are already on the map.)</span>
      <span style="color: #003366; font-weight: bold;">var</span> json <span style="color: #339933;">=</span> transport.<span style="color: #006600;">responseText</span>.<span style="color: #006600;">evalJSON</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      json.<span style="color: #006600;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        id <span style="color: #339933;">=</span> i.<span style="color: #006600;">location</span>.<span style="color: #006600;">id</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// Marker doesnt exist, add it.</span>
          markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createMarker<span style="color: #009900;">&#40;</span>i.<span style="color: #006600;">location</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #006600;">addOverlay</span><span style="color: #009900;">&#40;</span>markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> createMarkerClickHandler<span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    marker.<span style="color: #006600;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>
      <span style="color: #3366CC;">'&lt;div&gt;&lt;strong&gt;'</span> <span style="color: #339933;">+</span> location.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/strong&gt;&lt;br/&gt; '</span> <span style="color: #339933;">+</span>
      location.<span style="color: #006600;">address</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;br/&gt;'</span> <span style="color: #339933;">+</span> location.<span style="color: #006600;">city</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">', '</span> <span style="color: #339933;">+</span>
      location.<span style="color: #006600;">state</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> location.<span style="color: #006600;">zip</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> createMarker<span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> latlng <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>location.<span style="color: #006600;">lat</span><span style="color: #339933;">,</span> location.<span style="color: #006600;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>latlng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> createMarkerClickHandler<span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> location<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  GEvent.<span style="color: #006600;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> marker<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> removeMarkersOutsideOfMapBounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> markers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>map.<span style="color: #006600;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">containsLatLng</span><span style="color: #009900;">&#40;</span>markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">getLatLng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      map.<span style="color: #006600;">removeOverlay</span><span style="color: #009900;">&#40;</span>markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>updateMap()</code> function is run after the page initially loads and each time the user moves or zooms the map. It sends an AJAX request to the server with the maps boundaries, and the server returns a JSON object of the locations within those boundaries. After it receives the JSON object, it will add new locations to the map (it skips locations that have already been mapped) and removes locations that are no longer within the visible map boundaries.</p>
<p>A sample app containing all of the code can be downloaded here: <a href="/blog/wp-content/uploads/2008/09/map-sample-code.zip">map-sample-code.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mokisystems.com/blog/add-markers-to-a-google-map-with-ruby-on-rails-and-json/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ajax multi file upload with php, iframe, &amp; javascript</title>
		<link>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/</link>
		<comments>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 22:08:35 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javaxcript]]></category>
		<category><![CDATA[multi-file upload]]></category>

		<guid isPermaLink="false">http://www.mokisystems.com/blog/?p=28</guid>
		<description><![CDATA[I found several solutions after browsing around google trying to find a multi-file uploader built around php.  Most of these solutions for multi-file uploading fit into one of these three categories.

Use Flash to upload the images.  (The browser only supports one image at a time) &#8211; SWFUpload
Use Javascript to add multiple file type [...]]]></description>
			<content:encoded><![CDATA[<p>I found several solutions after browsing around google trying to find a multi-file uploader built around php.  Most of these solutions for multi-file uploading fit into one of these three categories.</p>
<ol>
<li>Use Flash to upload the images.  (The browser only supports one image at a time) &#8211; <a href="http://swfupload.org/">SWFUpload</a></li>
<li>Use Javascript to add multiple file type fields, while hidding previous fields &#8211; <a href="http://the-stickman.com/web-development/javascript/multiple-file-uploader-mootools-version/">the-stickman</a></li>
<li>Use Javascript and hidden iframes embedded on the same page to upload the images. &#8211; <a href="http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html">ajaxf1</a></li>
</ol>
<p>I decided to take pieces of the mentioned methods and come up with a solution that worked for me.</p>
<p>Here is the idea behind what we are going to be doing.</p>
<ol>
<li>Create a HTML form which has a file upload field</li>
<li>Set the target of this HTML form to an iFrame which is on the same page</li>
<li>Have Javascript submit the form everytime the &#8220;file&#8221; field changes</li>
<li>Javascript hides the form when submitted, and displays an &#8220;animation&#8221;</li>
<li>PHP takes the uploaded file, thumbnails it, and stores the image data</li>
<li>Javascript unhides the form, and re-hides the animation when upload is complete</li>
<li>Javascript and php show the thumbnail on the same page right after it was uploaded</li>
</ol>
<p>This makes this little script appear like an ajax application.  It is NOT actually AJAX, but uses css, iframes, php sessions, and javascript to make it appear so.</p>
<p>I&#8217;ll try to explain what is happening at each step, but keep in mind, I&#8217;m not an expert programmer like my fellow co-workers, and I borrowed most of the work from the mentioned sources above.  If you just want to download all the files and take the easy way out, skip to the bottom of the post.</p>
<p><strong>Steps 1 &#038; 2 &#038; 3 &#8211; Make Our Form</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript"><span style="color: #339933;">&lt;</span>form <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;pictureform&quot;</span> action<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload.php&quot;</span> method<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;post&quot;</span> enctype<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;multipart/form-data&quot;</span> target<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> <span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>p id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;f1_upload_process&quot;</span><span style="color: #339933;">&gt;</span>Loading...<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;loader.gif&quot;</span> <span style="color: #339933;">/&gt;&lt;</span>br<span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>p id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;f1_upload_form&quot;</span> align<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
         <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Add Pictures<span style="color: #339933;">:</span>  
              <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfile&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;file&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span> onchange<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;startUpload();document.pictureform.submit();&quot;</span> <span style="color: #339933;">/&gt;</span>
         <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>					 
     <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>iframe id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;width:0;height:0;border:0px solid #fff;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>iframe<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><span id="more-28"></span><br />
Explanation of the mentioned code:<br />
<code>enctype="multipart/form-data"</code><br />
Tells the browser you are submitting files, or images with your form.</p>
<p><code>onchange="startUpload();document.pictureform.submit();</code><br />
The onchange event of the input field calls the function startUpload(), which will hide the form, and show the animation while uploading.  The document.pictureform.submit(); Submits the form to our &#8220;action&#8221; which we have as &#8220;upload.php&#8221;</p>
<p><strong>Step 4 &#8211; Make the JavaScript functions for hiding and showing</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> startUpload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'visible'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Explanation of the mentioned code:<br />
By giving our HTML elements id tags,such as <code>id=f1_upload_process</code><br />
we make them available for manipulation by JavaScript via the DOM.</p>
<p><code>document.getElementById('f1_upload_process').style.visibility = 'visible';</code><br />
says: &#8220;get the element in the document that has an id of &#8220;f1_upload_process&#8221; and make it visible.&#8221;</p>
<p>This then turns on the visibility of our &#8220;animation&#8221;
<p> block which we have set &#8220;invisible&#8221; by default in our CSS file.  The opposite occurs for the form block, we make it &#8220;invisible&#8221; or &#8220;hidden.&#8221;</p>
<p>Step 5 &#8211; Create our &#8220;upload.php&#8221; script to handle thumbnail and copy image to filesystem</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// keep our session</span>
	<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Check the upload file type</span>
&nbsp;
	<span style="color: #000033;">$filetype</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$gd_function_suffix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
 		<span style="color: #0000ff;">'image/pjpeg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'JPEG'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/jpeg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'JPEG'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/gif'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GIF'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/bmp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'WBMP'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'image/png'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PNG'</span><span style="color: #339933;">,</span>
 		<span style="color: #0000ff;">'image/x-png'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PNG'</span> 
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$function_suffix</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$gd_function_suffix</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$filetype</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Build Function name for ImageCreateFromSUFFIX </span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$function_suffix</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Unsupported file type, must be JPG|PNG|GIF|BMP&quot;</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: #000033;">$function_to_read</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ImageCreateFrom'</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$function_suffix</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the image and create a thumbnail</span>
	<span style="color: #000033;">$img</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$function_to_read</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;could not create image handle&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$width</span> <span style="color: #339933;">=</span> imageSX<span style="color: #009900;">&#40;</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Get the image width</span>
	<span style="color: #000033;">$height</span> <span style="color: #339933;">=</span> imageSY<span style="color: #009900;">&#40;</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the image height</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$width</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000033;">$height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;No Width or Height&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Build the thumbnail</span>
	<span style="color: #000033;">$target_width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Thumnail width</span>
	<span style="color: #000033;">$target_height</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Thumbnail height</span>
	<span style="color: #000033;">$target_ratio</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//New target ratio</span>
&nbsp;
	<span style="color: #000033;">$img_ratio</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$height</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Original ratio</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_ratio</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$img_ratio</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$new_width</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$img_ratio</span> <span style="color: #339933;">*</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Tall Image</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$img_ratio</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Wide Image</span>
		<span style="color: #000033;">$new_width</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</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: #000033;">$new_height</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$target_height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_width</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$target_width</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$new_img</span> <span style="color: #339933;">=</span> ImageCreateTrueColor<span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Create a new image handle for thumbnail</span>
	<span style="color: #000033;">$white</span> <span style="color: #339933;">=</span> imagecolorallocate<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Allocate the color white</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span>imagefilledrectangle<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</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: #000033;">$target_width</span><span style="color: #cc66cc;">-1</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_height</span><span style="color: #cc66cc;">-1</span><span style="color: #339933;">,</span> <span style="color: #000033;">$white</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;">// Fill the image white</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Couldn't fill image&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// Copy the uploaded image to the newly created image</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span>imagecopyresampled<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</span> <span style="color: #000033;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_width</span><span style="color: #339933;">-</span><span style="color: #000033;">$new_width</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: #009900;">&#40;</span><span style="color: #000033;">$target_height</span><span style="color: #339933;">-</span><span style="color: #000033;">$new_height</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: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$new_width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$new_height</span><span style="color: #339933;">,</span> <span style="color: #000033;">$width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;couldn't copy new image&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Create php Session array for image information</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Use a output buffering to load the image into a variable</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>
	imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$imagevariable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Create a unique id or name for the image</span>
	<span style="color: #000033;">$file_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myfile&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$new_name</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.$function_suffix&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$temp_name</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myfile&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// assign upload directory, and target path for image</span>
	<span style="color: #000033;">$uploaddir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #000033;">$target_path</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$uploaddir</span><span style="color: #339933;">.</span>DIRECTORY_SEPARATOR<span style="color: #339933;">.</span><span style="color: #000033;">$new_name</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">// Move uploaded image to uploaddir location</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$temp_name</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Couldn't Copy File to Filesystem&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// Create a Session array with name, type, and image object for use in displaying thumbnails</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$file_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$file_id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>										<span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$new_name</span><span style="color: #339933;">,</span> 								<span style="color: #0000ff;">&quot;type&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$function_suffix</span><span style="color: #339933;">,</span> 
						<span style="color: #0000ff;">&quot;obj&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$imagevariable</span>								<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
   <span style="color: #666666; font-style: italic;">// If no error, then pass good result and file id to Javascript for generating the thumbnail.</span>
   <span style="color: #666666; font-style: italic;">// Pass our $err varialbe along</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$err</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'1','&quot;</span><span style="color: #339933;">.</span><span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</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: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'.$err.','&quot;</span><span style="color: #339933;">.</span><span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
   	<span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// in case of really fast upload, sleep script to show loading animation for a bit longer</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Javascript calls the &quot;stopUpload&quot; function with its paramaters</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;script language</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>window<span style="color: #339933;">.</span>top<span style="color: #339933;">.</span>window<span style="color: #339933;">.</span>stopUpload<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000033;">$result</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Explanation of Code:<br />
These php functions make use of the GD library.  You must have that enabled in PHP in order to use this.  Also make sure your current directory is writeable.</p>
<p>I won&#8217;t explain to many things here, as the code has inline comments.  You could make this code a function and have it thumbnail and save multiple sizes to the filesytem.  Currently it simply thumbnails a 100  X 100 and copies the original to the current directory the script is running from.  It will do GIF, JPEG, BMP, or PNG.  Error checking and Session handling could be improved.</p>
<p><strong>Step 6 &#8211; Create JavaScript function to show form, and hide animation when upload is finished</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> stopUpload<span style="color: #009900;">&#40;</span>success<span style="color: #339933;">,</span>fileid<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>success <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         result <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;span class=&quot;msg&quot;&gt;The file was uploaded successfully!&lt;<span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span>
		 addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnail.php?id=&quot;</span> <span style="color: #339933;">+</span> fileid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         result <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;span class=&quot;emsg&quot;&gt;There was an error during file upload!'</span> <span style="color: #339933;">+</span> success <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;<span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">innerHTML</span> <span style="color: #339933;">=</span> result <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;label&gt;File: &lt;input name=&quot;myfile&quot; type=&quot;file&quot; size=&quot;30&quot;  onchange=&quot;startUpload();document.pictureform.submit();&quot; /&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>label&gt;'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'visible'</span><span style="color: #339933;">;</span>      
&nbsp;
	  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>   
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Explanation of Code:<br />
From our upload.php script, we passed two varialbes to this JavaScript function, Success and File Id.<br />
If we had a successful file upload, we throw up a message saying &#8220;success&#8221; and call another function called <code> addImage("thumbnail.php?id=" + fileid);</code> <- More on this later...</p>
<p>If we didn't have a successful upload, we return a message saying so.</p>
<p>The last few lines of the function hide the "animation" bar, and recreate our form input element so we can upload another image, and do the whole thing over again.  All without leaving the page!</p>
<p><strong>Step 7 &#8211; Our Thumbnail.php script and our addImage() Javascript function</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// This script accepts an ID and looks in the user's session for stored thumbnail data.</span>
	<span style="color: #666666; font-style: italic;">// It then streams the data to the browser as an image	</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000033;">$image_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the id from the URL</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_id</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">exit</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: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>obj<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</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: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Code Explanation:<br />
This bit of code simply gets the image id from the URL and pulls the image thumbnail data we saved into our session in our upload.php script, and outputs the image to the browser.</p>
<p><strong>addImage() Javascript Function</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> addImage<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> newImg <span style="color: #339933;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">margin</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5px&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnails&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">appendChild</span><span style="color: #009900;">&#40;</span>newImg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newImg.<span style="color: #006600;">filters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			newImg.<span style="color: #006600;">filters</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DXImageTransform.Microsoft.Alpha&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.</span>
			newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">filter</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'progid:DXImageTransform.Microsoft.Alpha(opacity='</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	newImg.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fadeIn<span style="color: #009900;">&#40;</span>newImg<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	newImg.<span style="color: #006600;">src</span> <span style="color: #339933;">=</span> src<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> fadeIn<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> opacity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> reduceOpacityBy <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> rate <span style="color: #339933;">=</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">// 15 fps</span>
&nbsp;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		opacity <span style="color: #339933;">+=</span> reduceOpacityBy<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			opacity <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #006600;">filters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				element.<span style="color: #006600;">filters</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DXImageTransform.Microsoft.Alpha&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> opacity<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.</span>
				element.<span style="color: #006600;">style</span>.<span style="color: #006600;">filter</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'progid:DXImageTransform.Microsoft.Alpha(opacity='</span> <span style="color: #339933;">+</span> opacity <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			element.<span style="color: #006600;">style</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> opacity <span style="color: #339933;">/</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			fadeIn<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> opacity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> rate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Code Explanation:<br />
I borrowed most of this code from the SWFUpload project.  The basic idea is that we create a new image tag via javascript, give it a src of &#8220;thumbnail.php?id=XXXXX&#8221; XXXXX being our image name, and call a &#8220;fadeIn&#8221; function which does just that, fade the image in for a lovely effect.  I have this javascript included in my index.php page via</p>

<div class="wp_syntax"><div class="code"><pre>&lt;script type=&quot;text/javascript&quot; src=&quot;handlers.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Thats it!  Hope you learned as much as I did from this tutorial.</p>
<p><em>Important Notes:</em></p>
<p>-This script does not check file size, or limit the number of files that can be uploaded.<br />
-You will need to add in appropriate error checking on the uploaded files for security.<br />
-You will want to clean out the data from the session when done with the uploading.<br />
-Make sure you have write permission the directory where images are being saved<br />
-You must have GD library installed for this script to work</p>
<p>All the files you need to be up and running are found here -> <a href='http://www.mokisystems.com/blog/wp-content/uploads/2008/07/ajax-file-upload-mokisystems.zip'>ajax-file-upload-mokisystems</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
