<?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>Fancybox Archives - Michael Soriano</title>
	<atom:link href="https://michaelsoriano.com/tag/fancybox/feed/" rel="self" type="application/rss+xml" />
	<link>https://michaelsoriano.com/tag/fancybox/</link>
	<description>I turn code into captivating user experiences for the web</description>
	<lastBuildDate>Thu, 13 Jan 2011 18:41:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.4</generator>
	<item>
		<title>Create an Awesome Photo Gallery with FancyBox and TimThumb</title>
		<link>https://michaelsoriano.com/create-an-awesome-photo-gallery-with-fancybox-and-timthumb/</link>
					<comments>https://michaelsoriano.com/create-an-awesome-photo-gallery-with-fancybox-and-timthumb/#comments</comments>
		
		<dc:creator><![CDATA[Michael Soriano]]></dc:creator>
		<pubDate>Thu, 13 Jan 2011 18:41:46 +0000</pubDate>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Fancybox]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">http://fearlessflyer.com/?p=1978</guid>

					<description><![CDATA[<p>Time for a little tutorial eh? Let’s make a Photo Gallery – one that is a combination of already made scripts and plugins; one that is loaded automatically; and of course – one that looks awesome. We are using PHP, FancyBox jQuery plugin and TimThumb. Important: You should use the latest version of TimThumb due [&#8230;]</p>
<p>The post <a href="https://michaelsoriano.com/create-an-awesome-photo-gallery-with-fancybox-and-timthumb/">Create an Awesome Photo Gallery with FancyBox and TimThumb</a> appeared first on <a href="https://michaelsoriano.com">Michael Soriano</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Time for a little tutorial eh? Let’s make a Photo Gallery – one that is a combination of already made scripts and plugins; one that is loaded automatically; and of course – one that looks awesome. We are using PHP, FancyBox jQuery plugin and TimThumb.<br />
Important: You should use the latest version of TimThumb due to vulnerability issues with old code. Make sure you keep updated to keep your site secure.<br />
Ready to get started? Let’s begin:</p>
<h3>Set up the Environment:</h3>
<p>In order for our PHP scripts to run, you need to have a web server. A good example is XAMPP, which is free and can be downloaded from <a href="http://www.apachefriends.org/en/xampp.html">http://www.apachefriends.org/en/xampp.html</a>.  Create a root folder – name it anything, and inside it, create the following folders:</p>
<ul>
<li><strong>Images </strong> &#8211; this is where the image of the gallery will go. </li>
<li><strong>Scripts </strong> – this is where our scripts will go</li>
</ul>
<p>Download <strong>Timthumb </strong>script: <a href="http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/">http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/</a>. Timthumb is an image resizer script that is written in PHP.  Save <strong>timthumb.php</strong> inside scripts.<br />
Let’s create a new HMTL file inside the root, name it<strong> index.php</strong>. Add this line of code:<br />
<script src="https://gist.github.com/michaelsoriano/284b9bd79a3d49ea5aff.js"></script><br />
The above code creates 2 variables:</p>
<ul>
<li><strong>$path</strong> – is the full image path of our images</li>
<li><strong>$files</strong> – is an array of filenames from a specified folder that we’ve scanned through the<strong> scandir()</strong> function</li>
</ul>
<p>Then add the code below:<br />
<script src="https://gist.github.com/michaelsoriano/6acc208b7569fd5bb333.js"></script><br />
This loops through each item in the array $files – and assign it to another variable $file. For each one found, we’re printing a list item with an anchor and image tags. Within these tags you’ll notice the variables we’ve created earlier. The timthumb script is inserted at the beginning of the src attribute of our thumbnails. The width and the height parameters are hard coded in there as well. Note that we are going to randomize this section a little bit later.<br />
Assuming that you already have photos in your image folder &#8211; fire up your web browser and navigate to index.php. You should have something like the screenshot below:<br />
<img fetchpriority="high" decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot1.jpg" alt="" title="screenshot1" width="576" height="500" class="alignnone size-full wp-image-1979" /><br />
As you can see – we’re swiftly on our way. Our thumbnails are resizing right. Once clicked, the larger image appears. Though there are mysterious images that are returning empty (<em>file name is shows as “.” and “..”</em>). This may not be the case with yours &#8211; but with me, I just decided to fix this by wrapping our list items inside an ‘if &#8211; else’ statement:<br />
<script src="https://gist.github.com/michaelsoriano/b4058033e75c0fbc260a.js"></script><br />
Now we move on to the Fancy Box script:</p>
<h3>Add Fancy Box</h3>
<p>I almost always use jQuery for all my projects. It is one of the most versatile and powerful javascript libraries to date. Download the latest version from <a href=" http://jquery.com/ "> http://jquery.com/ </a> and include it in your document.<br />
Download the Fancybox files from <a href="http://fancybox.net/">http://fancybox.net/</a>. Once again, include the script right under the jQuery one. Note that Fancy box comes with its own image folder and css file. You need to include that css as well:<br />
Now let’s add a “<strong>rel</strong>” attribute to our anchor tag, with a value of “<strong>lightbox</strong>”. This makes it easier for our script to target which elements to add an effect on.<br />
Finally, create a new script tag and add our line of code:<br />
<script src="https://gist.github.com/michaelsoriano/64a2dcb02f55f6f6e1c5.js"></script><br />
The above code states that when the document is ready – grab all the anchor tags with an attribute value of “lightbox” and hook it up with the fancybox function. I’ve chosen the effect  ‘Elastic’  for the transitions – you can replace this with your own liking.<br />
Now go back to the browser and hit F5 to refresh. Click on a thumbnail – you should see Fancy Box in action with smooth and eye popping animation added to your photo gallery.<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot2.jpg" alt="" title="screenshot2" width="576" height="500" class="alignnone size-full wp-image-1980" /><br />
Next up – some styling with <strong>CSS</strong>.</p>
<h3>Pretty &#8216;er up.</h3>
<p>Now it is always good practice to separate your formatting and markup. Create a new file named <strong>style.css</strong>, go back to index.php and link the stylesheet we just created.<br />
In the last screenshot you notice that I marked the little buttons. That’s because of two things – if you’ve configured Fancybox correctly – those icons should be visible. The other thing is, I’ve created an icon that is very identical to those of Fancybox’s – and we’ll include in our thumbnails.  Take a look at the couple of images below:<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot3.jpg" alt="" title="screenshot3" width="576" height="127" class="alignnone size-full wp-image-1981" /><br />
These images (available in the download) I’ve saved inside the root folder. In your style.css – add these lines of code:<br />
<script src="https://gist.github.com/michaelsoriano/c20a35ee70d4d3ae712f.js"></script><br />
The code above adds a nice texture to our gallery background. Next, it floats each item to the left – aligning them all side by side. Then it adds a nice white background to each image, along with proper paddings and margins:<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot4.jpg" alt="" title="screenshot4" width="576" height="250" class="alignnone size-full wp-image-1982" /><br />
Remember our custom script tag just above our closing head tag? – add, this line of code just in between the <strong>$(document).ready()</strong> function:<br />
<script src="https://gist.github.com/michaelsoriano/a2e82a37d43a3cc2cebb.js"></script><br />
This will insert an empty span tag inside each of our list items (this is for the small magnifying glass icon). If you’re thinking – why not just add an inline span tag in our “for each” loop inside index.php – that’s an excellent question. This is because for users  without javascript enabled – they won’t see this magnifying button.<br />
Add these lines of code inside our style.css:<br />
<script src="https://gist.github.com/michaelsoriano/237981be4ce6ae4a0818.js"></script><br />
The above styles adds the subtle details that will make our thumbnails beautiful. This includes – of course our magnify.png, CSS3 box shadows (box-shadow) and rounded corners (border-radius).<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot5.jpg" alt="" title="screenshot5" width="576" height="250" class="alignnone size-full wp-image-1983" /><br />
Our gallery looks really good so far, but not good enough. Let’s add some embellishments.</p>
<h3>Randomize the Images and Orientation:</h3>
<p>Let’s make the thumbnail images be mixed orientation – some tall and some wide. Remember our thumbnail dimensions declared right after the timthumb script? All we need to do is switch the height and width values. So let’s create a PHP function. Append this code on top of index.php (right before the <strong>html</strong> tag):<br />
<script src="https://gist.github.com/michaelsoriano/c6945c58624acfd74bd0.js"></script><br />
The code above randomizes the two strings (‘<strong>&#038;h=194&#038;w=224</strong>&#8216; and &#8216;<strong>&#038;h=224&#038;w=194</strong>’). Then we call this function where our image tag is:<br />
<script src="https://gist.github.com/michaelsoriano/2c1a3e235e2ab1ebb707.js"></script><br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot6.jpg" alt="" title="screenshot6" width="576" height="350" class="alignnone size-full wp-image-1984" /><br />
Now our thumbnails have different orientation each time we refresh. But as you can see, we need to fix the float – some thumbnails get stuck where portrait ends. We also need to fix the magnify icon positioning.<br />
We need a way to tell what the current thumbnail orientation is, and somehow apply a class to that specific list item. This way we can fix the padding and margins accordingly. Add this line of code inside our “else” statement:<br />
<script src="https://gist.github.com/michaelsoriano/dca21d8d1fcc5fd8cf62.js"></script><br />
This grabs the current orientation and assigns it to a variable. Now, inside our li tag – paste this line of code:<br />
<script src="https://gist.github.com/michaelsoriano/840e8d274126f9c9a0b7.js"></script><br />
This adds a class name of “tall” for each list item – if the $current_o variable equals to the portrait dimension. We also need to replace our image thumbnail script to this new code:<br />
<script src="https://gist.github.com/michaelsoriano/ba0a75b34026a75a7a9e.js"></script><br />
Refresh your browser and if we inspect our code – you should see list items with a class of “tall” for images that have 224 x 194 dimensions:<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot7.jpg" alt="" title="screenshot7" width="576" height="200" class="alignnone size-full wp-image-1985" /><br />
Also, add these lines of code to our style.css file:<br />
<script src="https://gist.github.com/michaelsoriano/267bea0ce089711febbf.js"></script><br />
The above code overrides the default styling for the list items.<br />
Note that you also have to delete some previous styling for our list items (see the comments in the download). If you’re following this tutorial – I apologize for the replacements. As you see, I’m developing this as I go.<br />
Lastly, add this code inside index.php:<br />
<script src="https://gist.github.com/michaelsoriano/9f60bc3e00804428271f.js"></script><br />
This will randomize the images. So now we’ve fixed the list items, magnify icon – as well as randomized the images:<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot8.jpg" alt="" title="screenshot8" width="576" height="350" class="alignnone size-full wp-image-1986" /></p>
<h3>Final Touches:</h3>
<p>So our gallery is so done. Except I like to add those final details that make it really good. Let’s add a pre-loader image. This is when you see a “<strong>loading</strong>” message for each thumbnail before they appear. To do this, we go back to jQuery. Inside our document ready event, add this line of code:<br />
<script src="https://gist.github.com/michaelsoriano/a7bc92bbcfabd8d8ce24.js"></script><br />
The above code delays our images just a tad bit &#8211; just enough to see a preloader image. Save your “<strong>loading.png</strong>” image in the root directory. In your styles.css, add these lines of code:<br />
<script src="https://gist.github.com/michaelsoriano/70c66b7d23c96635913f.js"></script><br />
This just sets a background image to our list items. So next time you load the page – you should see our preloaders:<br />
<img decoding="async" src="https://michaelsoriano.com/wp-content/uploads/2011/01/screenshot9.jpg" alt="" title="screenshot9" width="576" height="197" class="alignnone size-full wp-image-1987" /><br />
Finally, download the fancy box <strong>mousewheel.js</strong> pack inside your scripts folder and include it in your document. You should now be able to view next and previous images once you scroll your mouse wheel.</p>
<h3>Conclusion:</h3>
<p>Well now, I think our gallery is ready for prime time. The good thing about this is you never have to re-size a thumbnail again. All you need to do is add the large image inside the &#8220;images&#8221; folder and presto! Another good thing is that your gallery doesn&#8217;t look the same all the time. With our randomized order and orientation, the gallery looks fresh at each refresh. <strong>So go ahead and grab the <a href="https://app.box.com/s/wtuwv6xn2bwvbezrmvou">download</a> files</strong>. Play around with it &#8211; and maybe even improve the code. Be sure to include your comments below:</p>
<p>The post <a href="https://michaelsoriano.com/create-an-awesome-photo-gallery-with-fancybox-and-timthumb/">Create an Awesome Photo Gallery with FancyBox and TimThumb</a> appeared first on <a href="https://michaelsoriano.com">Michael Soriano</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://michaelsoriano.com/create-an-awesome-photo-gallery-with-fancybox-and-timthumb/feed/</wfw:commentRss>
			<slash:comments>64</slash:comments>
		
		
			</item>
	</channel>
</rss>
