By Sergey Skudaev
I will show you how I display images gallery on my web page. First, I created a main images directory in my root directory. Then I create a directory for thumbnail images. Then I use PHP script to resize each main image, create a thumbnail and save it in thumbnail images directory with the same name as main image.
Then I display all thumnail images on one page in a table. Each thumbnail image has link to main image.
When user click a thumbnail, a corresponding single main image is displayed on the next page
<?php function resize($original_image, $thumb_width, $resized_image, $thumbdir, $orig_image_dir) { $max_width=$thumb_width; //Check if GD extension is loaded if (!extension_loaded('gd') && !extension_loaded('gd2')) { trigger_error("GD is not loaded", E_USER_WARNING); return false; } //Get Image size info list($width_orig, $height_orig, $image_type) = getimagesize($orig_image_dir."/".$original_image); switch ($image_type) { case 1: $im = imagecreatefromgif($orig_image_dir."/".$original_image); break; case 2: $im = imagecreatefromjpeg($orig_image_dir."/".$original_image); break; case 3: $im = imagecreatefrompng($orig_image_dir."/".$original_image); break; default: trigger_error('Unsupported filetype!', E_USER_WARNING); break; } //calculate the height/width ratio $h_to_w_ratio = (float) $height_orig / $width_orig; //calulate the thumbnail width based on the height $thumb_height = round($thumb_width * $h_to_w_ratio); while($thumb_height>$max_width) { $thumb_width-=10; $thumb_height = round($thumb_width * $h_to_w_ratio); } $newImg = imagecreatetruecolor($thumb_width, $thumb_height); //Check if this image is PNG or GIF, then set if Transparent*/ if(($image_type == 1) OR ($image_type==3)) { imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent); } imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig); //Generate the file, and rename it to $resized_image switch ($image_type) { case 1: imagegif($newImg,$thumbdir."/".$resized_image); break; case 2: imagejpeg($newImg,$thumbdir."/".$resized_image); break; case 3: imagepng($newImg,$thumbdir."/".$resized_image); break; default: trigger_error('Failed resize image!', E_USER_WARNING); break; } return $resized_image; } ?>
Let say you have main images in the images directory and you want to create a thumbnail image in the thumb directory. Then you place directory names in the function arguments.
$files=array(); $count=0; $files=scandir(dirname(__FILE__)."/images/"); $count=count($files); for($i=2; $i < $count; $i++) { echo resize($files[$i], 170, $files[$i],'thumb','images'); } ?>
Now when your created thumbnail images in the thumb directory, you may display these images on your web page. Again, I used scandir function to get image names.
$files=array(); $images=array(); $count=0; $files=scandir(dirname(__FILE__)."/".$thumb."/"); $count=sizeof($files); //two first file name are not valid images for some reason and you have to skip them. for($i=0; $i<$count; $i++) { if($i >1) $images[]=$files[$i]; } $count=sizeof($images); print('<table><tr>'); for($i=0; $i<$count; $i++) { //pass image name to the main image directory to display large image on the next page. print('<td><a href="largeimage.php?path='.$orig_image_dir.'/'.$images[$i].'"> <img src="'.$thumb.'/'.$images[$i].'"></a></td>'); if(($i+1) % 6==0) { print('</tr><tr>'); } } print('</tr></table>'); ?>
On the largeimage.php page display the main image
$imagefile=""; if(isset($_GET['path'])) $imagefile=$_GET['path']; print('<table align="center" border="7"><tr><td>'); print('<img src="'.$imagefile.'" width="593" alt="an image caption"/>'); print('</td></tr></table>');
Our dog needs urgent surgery, and the cost is overwhelming.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!
Oscar Story.
Oscar wasn’t just any puppy—he was a gift from a mother who trusted us with her smallest one.
For five years, my wife worked at the Indian Medical Center in Arizona, deep in Navajo Nation. Near her clinic, she often saw a homeless dog wandering the area. Over time, she began feeding her, and the dog grew fond of her. Then, one day, that same dog brought her newborn puppies to my wife—as if proudly showing them off.
Among them was the smallest, most delicate pup. My wife couldn’t resist. She brought him home, and we named him Oscar.
Oscar thrived in the house provided by the medical center, enjoying the big backyard where he lived. I built him a sturdy wooden doghouse, and we often took him on walks along the Window Rock Trail. He became our adventure companion, making the vast desert feel like home.
After my wife’s contract ended, we moved back to Florida, bringing Oscar with us. He adjusted to his new surroundings, but he never lost his adventurous spirit.
Now, Oscar faces a tough challenge—he needs urgent surgery, and the cost is overwhelming. We want to give him the best care possible, just as he’s given us years of joy and loyalty.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!