<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Musings of a Code Monkey &#187; php</title>
	<atom:link href="http://www.tinyblueplanet.com/code/?feed=rss2&#038;tag=php-2" rel="self" type="application/rss+xml" />
	<link>http://www.tinyblueplanet.com/code</link>
	<description>Little bits of technology all broken down</description>
	<lastBuildDate>Wed, 07 Dec 2011 08:29:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Preview: A very simple php &amp; javascript image viewer</title>
		<link>http://www.tinyblueplanet.com/code/?p=5</link>
		<comments>http://www.tinyblueplanet.com/code/?p=5#comments</comments>
		<pubDate>Sun, 11 Sep 2011 13:35:58 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[gallery]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.tinyblueplanet.com/code/?p=5</guid>
		<description><![CDATA[I wanted to create a very basic (and I mean basic) image gallery to use on a webpage. It didn&#8217;t have to be flash or with fantastic animation. Its purpose was just a little extra page flair and so simplicity &#8230;<p class="read-more"><a href="http://www.tinyblueplanet.com/code/?p=5">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[            <script type="text/javascript" src="http://www.tinyblueplanet.com/code/wp-content/plugins/wordpress-code-snippet/scripts/shBrush.js"></script>
<p>I wanted to create a very basic (and I mean basic) image gallery to use on a webpage. It didn&#8217;t have to be flash or with fantastic animation. Its purpose was just a little extra page flair and so simplicity was the main requirement.</p>
<p>I looked at a number of existing libraries, searched for examples, read a few blogs and just didn&#8217;t see anything particularly simple or lightweight (Lightbox2 for example was pulling 100Kbs of js data in; granted its lovely to see in action but a little overkill for what I wanted).</p>
<p>So I decided to create something as minimal as possible but that was still flexible using PHP to construct the HTML, Javascript to swap images and some simple CSS styling. The solution, nominally called Preview is nothing special, however it does provide a nice simple solution that you might find educational.</p>
<p>The basic features:</p>
<ul>
<li>Somewhat flexible on image sizes.</li>
<li>Use a simple folder based structure, making it easy to auto-generate gallery images (not covered here but easy to do with PHP) or for someone to do it quickly by hand (under 5mins).</li>
<li>Displays clickable thumbnails;  with the number displayed being configurable.</li>
<li>Displays a single main image, that can be swapped out when the thumbnails are clicked.</li>
<li>Provide support for titles and alt text association with images.</li>
<li>Uses as little browser-side code as possible.</li>
<li>Supports CSS styling.</li>
</ul>
<h2>Javascript and OnClick</h2>
<p>The dynamic swapping of images is handled using a really simple Javascript function, which is small enough to be embedded in the header of a page and a little CSS<br />
<pre class="brush: "></pre></p>
<p>&nbsp;</p>
<p>This function is attached to an <em>onClick</em> action placed on an image of our choice. In Preview, when the user clicks on a thumbnail image, this swapImage function is triggered. We change the main image being displayed by updating where its src should point to (the image is simply identified by the CSS id <strong>image-main</strong>). We then attach the attributes from our thumbnail image to the new main image.</p>
<p>The page then shows a new main image with the correct alt and title attributes. You could obviously pass different alt, title and other attributes into this function as well as trigger animations and other visual wizardry. Thats up to you.</p>
<p>Preview places Javascript <em>onClick</em> events on each thumbnail images being displayed, so when the user clicks on one then the <em>imageSwap</em> function is called:</p>
<pre>&lt;img class='thumbnail' src='gallery/ke_75.jpg' alt='[Photo:Lake Victoria]' title='Lake Victoria' height='75' width='75' onclick='imageSwap(event, "gallery/ke.jpg")'&gt;</pre>
<p>The final trick is to change the cursor when rolling over a thumbnail, so that the user will &#8216;see&#8217; the thumbnail as a clickable link. This requires a only tiny piece of CSS via the <strong>cursor</strong> property ie:</p>
<pre>#preview img.thumbnail {
	cursor: pointer;	/* Show 'link' cursor */
}</pre>
<p>Really thats the core of the solution in a nutshell.</p>
<h2>Putting it together with PHP</h2>
<p>Now we have the basic mechanism ready for use, we can use PHP to tie everything together so one simple call will show a nice looking gallery, such as this one:</p>
<p><img class="aligncenter" title="An example Preview Gallery" src="http://www.tinyblueplanet.com/code/examples/preview/screenshot.jpg" alt="" width="328" height="500" /></p>
<p>Preview provides a single PHP call, which takes the location of the gallery folder and how many images to show:</p>
<pre>show_preview_gallery('gallery', 6);</pre>
<p>First we need to decide on some basic image size rules, which really helps to constrain the design requirements. You will need to set different image sizes in the preview.php and preview.css files if you want to use image sizes different than this example.</p>
<h2>Image Sizes</h2>
<p>The first thing to decide is on a size for our Preview main image, in this example I&#8217;m going to use images which are 240px by 300px (again sticking with idea of small page decoration) so it can be easily be floated into a sidebar or margin.</p>
<p>Next lets decide on thumbnail sizes, in this example thumnail previews are going to be 75px by 75px.</p>
<p>I use PHP to crop, resize and prepare uploaded images for galleries like this, but we can look at that on another day. We need for each image to display:</p>
<ul>
<li>The main image version, 240&#215;300 (ie image.jpg)</li>
<li>A thumbnail preview version, 75&#215;75 (ie image_75.jpg)</li>
<li>A piece of description text, that will be used for the alt text and tooltip title</li>
</ul>
<p>The pair of images are placed flat in our gallery folder and the description text is written into a very simple descriptions.txt file at this level, which is simply a tab separated format.</p>
<h2>Folder Structure</h2>
<p>All the image files and description file reside in a single folder, like so:</p>
<p><img class="aligncenter" title="Folder structure" src="http://www.tinyblueplanet.com/code/examples/preview/folder.png" alt="" width="251" height="287" />Each thumbnail file has the same name as its parent (thumbnails are identified by a suffix showing their image dimensions &#8211; so in this case xxxx_75.jpg). The Preview code should support .gif, .png files as well.</p>
<h2>Using PHP to scan a folder</h2>
<p>We use PHP to scan the given gallery folder to find all the thumbnail images of the size we want (in this case all 75&#215;75), using the <em>glob</em> function, which finds pathnames matching a simple pattern:<br />
<pre class="brush: "></pre></p>
<p>You could use nice directory iterators to do the same, but as we have a flat filesystem currently then <em>glob</em> works fine. To add a bit of spice we use <em>shuffle</em> to randomize things and then we <em>slice</em> off how many images we want.</p>
<p>At this stage we now have an array of all the thumbnail images.</p>
<p>The rest of the code is pretty simple. We strip off the thumbnail size designation to get the name of the main image file its related to. We construct our IMGs and hook in the <em>imageSwap onClick</em> event and we attach the long descriptions from our flat description text file.</p>
<h2>Use CSS to style Preview</h2>
<p>Preview is set-up to use id based selection for its main view. Unfortunately theres no simple way of passing our thumbnail and image sizes in, so if you using something bigger/smaller than adjust the widths accordingly in the CSS (remembering to account for any padding and margins being set).</p>
<p>The css file is there mostly to provide a very simple starting point for experimentation by the key components. The only real constraint is that the <em>imageSwap </em>function relies on the CSS id of the main image.</p>
<h2>Example</h2>
<p>You can see a very basic example of <a title="Preview in action" href="http://www.tinyblueplanet.com/code/examples/preview">Preview</a> here. Things are pretty basic to support experiementation.</p>
<h2>Download</h2>
<p>You can download all the code and a working example <a href="http://www.tinyblueplanet.com/code/examples/preview/preview.zip">here</a>. Feel free to modify, hack, lambast and destroy this code. Its deliberately simple and limited so you can see how things work. There is no support provided for this code, though am happy to answer questions and the code is obviously provided as is, with no warranty or guarantee of its functionality.</p>
<h2>Too Simple?</h2>
<p>There are literally thousands of Javascript image galleries and cool lightboxes out there. Preview is just here to show how to begin to construct your own, its not meant to be used seriously.</p>
<p>If you want something to make really good-looking images displays then start with one of these fine libraries:</p>
<p><a href="http://www.lokeshdhakar.com/projects/lightbox2/">LightBox2</a></p>
<p><a href="http://colorpowered.com/colorbox/">Colorbox</a></p>
<p><a href="http://sorgalla.com/jcarousel/">JCarousel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinyblueplanet.com/code/?feed=rss2&#038;p=5</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
