<?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>Kevin Dubois &#187; slideshow</title>
	<atom:link href="http://kevindubois.com/blog/tag/slideshow/feed/" rel="self" type="application/rss+xml" />
	<link>http://kevindubois.com/blog</link>
	<description>A LAMP consultant in the greater Salt Lake City Area</description>
	<lastBuildDate>Mon, 24 Oct 2011 16:32:22 +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>Rotating images using jQuery Ajax</title>
		<link>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/</link>
		<comments>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 23:20:21 +0000</pubDate>
		<dc:creator>Kevin Dubois</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[slideshow]]></category>

		<guid isPermaLink="false">http://kevindubois.com/blog/?p=13</guid>
		<description><![CDATA[Tweet There are many ways to rotate images on a website.  In my case, I needed a way to rotate all images from a particular folder, and I wanted it so that I just have to dump images in the &#8230; <a href="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fkevindubois.com%2Fblog%2F2009%2F03%2F03%2Frotating-images-jquery-ajax%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" style="border:none; overflow:hidden; width=85px; height:21px;"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"  data-text="Rotating images using jQuery Ajax" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>There are many ways to rotate images on a website.  In my case, I needed a way to rotate all images from a particular folder, and I wanted it so that I just have to dump images in the folder in order for the slideshow to pick them up.  ** NOTE: if you want a more advanced setup with database driven slides visit my <a title="Database driven jquery rotating slideshow demo" href="http://kevindubois.com/blog/2009/07/29/database-driven-jquery-rotating-slide-show-with-text-navigation-and-links-using-the-cycle-plugin/" target="_blank">post about database driven slideshows</a></p>
<p>For this tutorial I&#039;m going to use jQuery&#039;s framework and crossSlide, a plugin for jQuery which will allow smooth transitions between the images.  If you don&#039;t have it already, you can get jQuery at <a title="www.jquery.com" href="http://www.jquery.com" target="_blank">www.jquery.com</a> and the crossSlide plugin at <a title="www.gruppo4.com/~tobia/cross-slide.shtml" href="http://www.gruppo4.com/~tobia/cross-slide.shtml" target="_blank">www.gruppo4.com/~tobia/cross-slide.shtml</a> .  You&#039;ll also have to create a folder with images.  You&#039;ll get the best result if all images are identical in size.</p>
<p>A working example can be found at <a title="duboistechnologies.com" href="http://www.duboistechnologies.com">duboistechnologies.com</a> .</p>
<p>Download the code <a title="rotate images zip" href="http://kevindubois.com/blog/attachments/rotate_images.zip" target="_self">here</a></p>
<p>Ok, first things first: we&#039;ll need a placeholder for the images.  We&#039;ll need to specify a fixed height for this placeholder too, otherwise the content below will be jumping up and down every time a new image is loaded:</p>
<blockquote><p>&lt;div id=&#034;imgcontainer&#034; style=&#034;margin:0; width:46em; height:20em;&#034;&gt;<br />
&lt;img src=&#034;img/ajax-loader.gif&#034; style=&#034;margin-top:20%&#034; alt=&#034;loading&#8230;&#034; id=&#034;mainImg&#034; /&gt;<br />
&lt;/div&gt;</p></blockquote>
<p>I put an image in the div that will show an icon while the images are loaded&#8230;</p>
<p>Next step is to create a php page with some code to get all images from the folder.   The getfiles function will get all files and return a string that will be formatted so it can be parsed as separate objects by the javascript eval() function&#8230; but that&#039;s for later.  We just echo this string so our ajax call will pick it up.  The only thing you&#039;ll need to customize is the &#039;imagefolder&#039;&#8230; change this to whatever the name or path is to your folder:</p>
<blockquote><p>&lt;?php</p>
<p>echo getfiles(&#039;imagefolder&#039;);</p>
<p>function getfiles($folder=&#034;, $extensions=&#039;.*&#039;)<br />
{</p>
<p>// path:<br />
$folder = trim($folder);<br />
$folder = ($folder == &#034;) ? &#039;./&#039; : $folder;</p>
<p>// validate folder<br />
if (!is_dir($folder)){ die(&#039;invalid folder given!&#039;); }</p>
<p>// create files array<br />
$files = array();</p>
<p>// open directory<br />
if ($dir = @opendir($folder)){</p>
<p>// get all files:<br />
while($file = readdir($dir)){</p>
<p>if (!preg_match(&#039;/^\.+$/&#039;, $file) and<br />
preg_match(&#039;/\.(&#039;.$extensions.&#039;)$/&#039;, $file)){</p>
<p>// feed the array:<br />
$files[] = $file;<br />
}<br />
}<br />
// close directory<br />
closedir($dir);<br />
}<br />
else {<br />
die(&#039;Could not open the folder &#034;&#039;.$folder.&#039;&#034;&#039;);<br />
}</p>
<p>if (count($files) == 0){<br />
die(&#039;No files where found <img src='http://kevindubois.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> &#039;);<br />
}<br />
// shuffle the array so we have a random sequence of images<br />
shuffle($files);</p>
<p>$return = &#034;;<br />
foreach($files as $file){<br />
// format the string so we can later parse it into objects in javascript<br />
$return .= &#034;{&#039;src&#039;:'{$folder}/{$file}&#039;},&#034;;<br />
}<br />
$return = &#034;[".substr($return, 0,-1)."]&#034;;</p>
<p>// return the string:<br />
return $return;</p>
<p>}</p>
<p>?&gt;</p></blockquote>
<p>What we need to do now is set up an ajax call that gets the string of images the above function found in my images folder.  We&#039;ll also have to include the jquery and crossSlide packages:</p>
<blockquote><p>&lt;script type=&#034;text/javascript&#034; src=&#034;js/jquery.js&#034;&gt;&lt;/script&gt;<br />
&lt;script type=&#034;text/javascript&#034; src=&#034;js/jquery.cross-slide.js&#034;&gt;&lt;/script&gt;</p>
<p>&lt;script type=&#034;text/javascript&#034;&gt;<br />
$(document).ready(function(){loadImage();})</p>
<p>function loadImage(){<br />
// getimage.php is the php file that gets my images from the images folder<br />
$.post(&#034;getimage.php&#034;, function(data){<br />
if(data.length &gt;0) {<br />
// evaluate the string to a group of objects<br />
myObject = eval(data);<br />
// create a slideshow with the image objects and drop them in the placeholder<br />
$(&#039;#imgcontainer&#039;).crossSlide({sleep:5,fade:2},myObject);<br />
}<br />
});</p>
<p>}<br />
&lt;/script&gt;</p></blockquote>
<p>That&#039;s all there is to it.  This is a great alternative to flash and is much less cpu intensive!</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="kevin.dubois@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Rotating images using jQuery Ajax" /><input type="hidden" name="currency_code" value="USD" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://kevindubois.com/blog/wp-content/plugins/buy-me-beer/icon_beer.gif" align="left" alt="" title="" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=kevin.dubois@gmail.com&amp;currency_code=USD&amp;amount=1&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Rotating+images+using+jQuery+Ajax" target="paypal">Was this post helpful? Buy me a drink :-)</a></p>]]></content:encoded>
			<wfw:commentRss>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

