Skip to content

PHP – Auto Generate a Photo Gallery from a Directory with pagination

Three year ago I wrote a post on generating image gallery from a given directory. Some of my readers are requested to add pagination to gallery so I have decided to take his tutorial a step further by showing you how to generate pagination for the gallery using PHP.

Creating an photo gallery from a folder or directory of images using PHP is easy. I have added inline comments for each code block, just go through them for better understanding.

isFile() && in_array($fileinfo->getExtension(),array('jpg','png'))) { 
      array_push($allImages,$image_dir.$fileinfo->getBasename());
    }
  }

// total number of images
$total_pages = count($allImages);
	
//how many items to show per page
$page = isset($_GET['page']) ? $_GET['page'] : 1;

//  if no page var is given, set start to 0
$start = $page ?  (($page - 1) * $limit) : 0; 

	
$images = array_slice( $allImages, $start, $limit );;

/* Setup page vars for display. */
if ($page == 0) $page = 1;					//if no page var is given, default to 1.
$prev = $page - 1;							//previous page is page - 1
$next = $page + 1;							//next page is page + 1
$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;						//last page minus 1
	
$pagination = "";
if($lastpage > 1)
{	
  $pagination .= "
"; //previous button if ($page > 1) $pagination.= "Previous"; else $pagination.= "Previous"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //close to end; only hide early pages else { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } } //next button if ($page < $counter - 1) $pagination.= "Next"; else $pagination.= "Next"; $pagination.= "
\n"; } if(count($images) > 0) { foreach($images as $image) { ?>
0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments