PHP Thumbnail Gallery

December 16, 2006

I had gone again to the office to work on the PM box artwork and its completed now. Later I saw a great movie with my wife, Kabul Express. It was a very very good movie! I generally dont like watching any of the new Bollywood movies.

This week started with first 3 days of the watering hole routine and then Thursday onwards I worked on the IF project with its CMS and last night I created the thumbnails page to dynamically resize images from a database. I m saying this as if I have created a non existing function in the language that deals with this problem. But for a non programmer this is an achievement!

I have gotten into PHP and am very happy about it. I really love the way one can make thumbnails on the fly. Its really surprising that ASP didn’t have that. I m sure there is a whole lot to PHP than just making thumbnails. I have managed to get 25 books on PHP! I think that should be enough to start learning PHP. My approach to learning anything has always been, keep trying the damn thing till its done. No deadlines, no goals, just to keep it going. There is one more crucial thing to the success of this method. Whatever that I am learning must have some commercial importance. For example I cannot just yet start taking 3D very seriously as there is no commercially viable project that can happen in the near or far future at this moment. The same applies to music.

OK, the PHP image gallery. There wasnt much problem as there is the GD Library and innumerable tutorials online for achieving the thumbnailing procedure. The one which turned out the most useful for me was by Michael Bailey and here is a nice pdf of it:
www.codewalkers.com/tutorialpdfs/tutorial42.pdf

If you are an absolute newcomer to PHP or any kind of programming like me, then you might stumble upon the confusion due to the header tag and where does the link for the thumbnailed image actually come? From the above tutorial, make one php file with the script for the thumbnail without any other tags, save it and close it. Then in another page using the HTML <IMG> tag and assign this thumbnail.php to the SRC attribute of the IMG tage like this
<img border=’0′ src=’thumbnail.php?”.$row['pimg'].”‘ alt=’Click to enlarge this product!’>

The PHP query string value is retrieved from MySQL and an example would be ‘the_image.jpg’. It is basically the full version of the image we want to make a thumbnail of. thumbnail.php does all the necessary processing and returns an appropriate thumbnail.

Another issue I faced was, how to create a grid. I had a layout of 2 columns and many rows to display a bunch of images pulled from the database. This is what I did. I had a loop running to collect all the image paths of the selected category of images. This loop obviously incremented a counter called $num to see how many images we have pulled. At every loop point I simply divided the counter variable by 2 and saw if it returned ‘zero’ as the remainder by using the modulo function. If it did then it meant that it needed to be in the second column in the grid. If it didnt then it needed to come in the first column. Here is the script to demonstrate this:

$result = mysql_query(“SELECT * FROM if_products WHERE pcategory=’”.$cat.”‘”);
echo “<table border=’0′ cellpadding=’10′ cellspacing=’10′>”;
while($row = mysql_fetch_array($result))
{
$num=$num+1;
if($num%2==0){
echo “<td valign=’top’ width=’50%’>”;

echo “<table class=’product_display’>”;
echo “<tr><td>”;
echo “<a href=’if_product_details.php?id=”.$row['id'].”‘><img border=’0′ src=’if_thumbnail.php?”.$row['pimg'].”‘ alt=’Click to enlarge this product!’></a>”;
echo “</td></tr>”;
echo “</table>”;

echo “</td></tr>”;

}else{
echo “<tr><td valign=’top’ width=’50%’>”;
echo “<table class=’product_display’>”;
echo “<tr><td>”;
echo “</td></tr>”;
echo “</table>”;

echo “</td>”;
}
}
if($num%2 != 0){
echo “</td></tr>”;
}
echo “</table>”;

Apart from that I created the new year ad option for A and the concept is approved, just the artwork had some serious layout issues. Its best to look at a whole lot of good layouts before starting any artwork. Also the problem is to suddenly shift from web dev to hi res print artwork. The entire process changes. Firstly the machine reacts slower due to hi res and then there is no code to achieve something faster.

I have ordered for a new machine and it will be here alongside in a week or so. Its cost me 39 k which comes from my wife’s office as a perk! All machines I ve ever bought have come from my wife.

The new machine is a core 2 duo 2.13 ghz machine with a gigabyte of ram and a 256mb graphics card. Its sufficient for my kinda work. Create artwork, splice it and bind it with the database content with resuable code.