Advertisement
Guest User

foliogallery.php Version 2.0

a guest
Apr 12th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.41 KB | None | 0 0
  1. <!--
  2.     folioGallery v2.0 - 2015-01-30
  3.     (c) 2014 Harry Ghazanian - foliopages.com/php-jquery-ajax-photo-gallery-no-database
  4.     This content is released under the http://www.opensource.org/licenses/mit-license.php MIT License.
  5. -->
  6. <script type="application/javascript">
  7. $(document).ready(function() { // load colorbox settings
  8.     $('.folioGallery').each(function() {
  9.         var targetDiv = (this.id); // id of div to load albums 
  10.         $('#'+targetDiv+' .albumpix').colorbox({rel:targetDiv, maxWidth:'96%', maxHeight:'96%', slideshow:true, slideshowSpeed:3500, slideshowAuto:false});
  11.         $('#'+targetDiv+' .vid').colorbox({rel:targetDiv, iframe:true, width:"80%", height:"96%"});
  12.     });
  13. });
  14. </script>
  15. <?php
  16. /***** gallery settings *****/
  17. $mainFolder          = 'albums'; // main folder that holds albums - this folder resides on root directory of your domain
  18. $album_page_url      = $_SERVER['PHP_SELF']; // url of page where gallery/albums are located
  19. $no_thumb            = 'foliogallery/noimg.png';  // show this when no thumbnail exists
  20. $extensions          = array("jpg","png","gif","JPG","PNG","GIF"); // allowed extensions in photo gallery
  21. $itemsPerPage        = '12';    // number of images per page if not already specified in ajax mode
  22. $thumb_width         = '150';   // width of thumbnails in pixels
  23. $sort_albums_by_date = TRUE;    // TRUE will sort albums by creation date (most recent first), FALSE will sort albums by name
  24. $sort_images_by_date = TRUE;    // TRUE will sort thumbs by creation date (most recent first), FALSE will sort images by name
  25. $random_thumbs       = TRUE;    // TRUE will display random thumbnails, FALSE will display the first image from thumbs folders
  26. $show_captions       = TRUE;    // TRUE will display file names as captions on thumbs inside albums, FALSE will display no captions
  27. $num_captions_chars  = '20';    // number of characters displayed in album and thumb captions
  28. /***** end gallery settings *****/
  29.  
  30. $numPerPage = (!empty($_REQUEST['numperpage']) ? (int)$_REQUEST['numperpage'] : $itemsPerPage);
  31. $fullAlbum  = (!empty($_REQUEST['fullalbum']) ? 1 : 0);
  32.  
  33. // sanitize string
  34. function sanitize($string)
  35. {
  36.     $string = htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
  37.     return $string;
  38. }
  39.  
  40. // encode string to
  41. function encodeto($string)
  42. {
  43.     $string = mb_convert_encoding(trim($string), 'UTF-8', 'HTML-ENTITIES');
  44.     return $string;
  45. }
  46.  
  47. // function to create thumbnails from images
  48. function make_thumb($folder,$file,$dest,$thumb_width) {
  49.  
  50.     $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  51.    
  52.     switch($ext)
  53.     {
  54.         case "jpg":
  55.         $source_image = imagecreatefromjpeg($folder.'/'.$file);
  56.         break;
  57.        
  58.         case "jpeg":
  59.         $source_image = imagecreatefromjpeg($folder.'/'.$file);
  60.         break;
  61.        
  62.         case "png":
  63.         $source_image = imagecreatefrompng($folder.'/'.$file);
  64.         break;
  65.        
  66.         case "gif":
  67.         $source_image = imagecreatefromgif($folder.'/'.$file);
  68.         break;
  69.     }  
  70.    
  71.     $width = imagesx($source_image);
  72.     $height = imagesy($source_image);
  73.    
  74.     if($width < $thumb_width) // if original image is smaller don't resize it
  75.     {
  76.         $thumb_width = $width;
  77.         $thumb_height = $height;
  78.     }
  79.     else
  80.     {
  81.         $thumb_height = floor($height*($thumb_width/$width));
  82.     }
  83.    
  84.     $virtual_image = imagecreatetruecolor($thumb_width,$thumb_height);
  85.    
  86.     if($ext == "gif" or $ext == "png") // preserve transparency
  87.     {
  88.         imagecolortransparent($virtual_image, imagecolorallocatealpha($virtual_image, 0, 0, 0, 127));
  89.         imagealphablending($virtual_image, false);
  90.         imagesavealpha($virtual_image, true);
  91.     }
  92.    
  93.     imagecopyresampled($virtual_image,$source_image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
  94.    
  95.     switch($ext)
  96.     {
  97.         case 'jpg': imagejpeg($virtual_image, $dest,80); break;
  98.         case 'jpeg': imagejpeg($virtual_image, $dest,80); break;
  99.         case 'gif': imagegif($virtual_image, $dest); break;
  100.         case 'png': imagepng($virtual_image, $dest); break;
  101.     }
  102.  
  103.     imagedestroy($virtual_image);
  104.     imagedestroy($source_image);
  105.    
  106. }
  107.  
  108. // return array sorted by date or name
  109. function sort_array(&$array,$dir,$sort_by_date) { // array argument must be passed as reference
  110.    
  111.     if($sort_by_date)
  112.     {
  113.         foreach ($array as $key=>$item)
  114.         {
  115.             $stat_items = stat($dir .'/'. $item);
  116.             $item_time[$key] = $stat_items['ctime'];
  117.         }
  118.         return array_multisort($item_time, SORT_DESC, $array);
  119.     }  
  120.     else
  121.     {
  122.         return usort($array, 'strnatcasecmp');
  123.     }  
  124.  
  125. }
  126.  
  127. // get album and image descriptions
  128. function itemDescription($album, $file='')
  129. {
  130.     if(file_exists($album.'/descriptions.txt'))
  131.     {
  132.         $lines_array = file($album.'/descriptions.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  133.         if($lines_array)
  134.         {
  135.             if($file == '')
  136.             {
  137.                 $album_line = explode(';', $lines_array[0]);
  138.                 return (!empty($album_line[0]) && $album_line[0] == 'album' ? $album_line[1] : '');
  139.             }
  140.             else
  141.             {
  142.                 foreach($lines_array as $img_line)
  143.                 {  
  144.                     if(!empty($img_line)) {
  145.                         $img_desc = explode(';', $img_line);   
  146.                         if($img_desc[0] == $file) { return $img_desc[1]; }
  147.                     }
  148.                 }
  149.             }  
  150.         }
  151.         else
  152.         {
  153.             return '';
  154.         }
  155.     }  
  156. }
  157.  
  158.  
  159. // display pagination
  160. function paginate_array($numPages,$urlVars,$alb,$currentPage) {
  161.        
  162.    $html = '';
  163.    
  164.    if ($numPages > 1)
  165.    {
  166.    
  167.        if ($currentPage > 1)
  168.        {
  169.            $prevPage = $currentPage - 1;
  170.            $html .= '<a class="pag prev" rel="'.$alb.'" rev="'.$prevPage.'" href="?'.$urlVars.'p='.$prevPage.'"></a> ';
  171.        }       
  172.        
  173.        for( $i=0; $i < $numPages; $i++ )
  174.        {
  175.            $p = $i + 1;
  176.            $class = ($p==$currentPage ? 'current-paginate' : 'paginate');
  177.            $html .= '<a rel="'.$alb.'" rev="'.$p.'" class="'.$class.' pag" href="?'.$urlVars.'p='.$p.'"></a>';   
  178.        }
  179.        
  180.        if ($currentPage != $numPages)
  181.        {
  182.            $nextPage = $currentPage + 1;   
  183.            $html .= ' <a class="pag next" rel="'.$alb.'" rev="'.$nextPage.'" href="?'.$urlVars.'p='.$nextPage.'"></a>';
  184.        }         
  185.    
  186.    }
  187.    
  188.    return $html;
  189.  
  190. }
  191. ?>
  192.  
  193. <div class="fg">
  194.  
  195. <?php
  196. if (empty($_REQUEST['album'])) // if no album requested, show all albums
  197. {      
  198.    
  199.     $albums = array_diff(scandir($mainFolder), array('..', '.'));
  200.     $numAlbums = count($albums);
  201.      
  202.     if($numAlbums == 0)
  203.     { ?>
  204.        
  205.         <div class="titlebar"><p>No albums posted</p></div>
  206.    
  207.     <?php
  208.     }
  209.     else
  210.     {
  211.         sort_array($albums,$mainFolder,$sort_albums_by_date); // rearrange array either by date or name
  212.         $numPages = ceil( $numAlbums / $numPerPage );
  213.        
  214.         if(isset($_REQUEST['p']))
  215.          {
  216.             $currentPage = ((int)$_REQUEST['p'] > $numPages ? $numPages : (int)$_REQUEST['p']);
  217.          }
  218.          else
  219.          {
  220.             $currentPage=1;
  221.          }
  222.        
  223.         $start = ($currentPage * $numPerPage) - $numPerPage; ?>
  224.          
  225.         <div class="p10-lr">
  226.             <span class="title">Photo Gallery</span> - <?php echo $numAlbums; ?> albums
  227.         </div>
  228.      
  229.         <div class="clear"></div>
  230.          
  231.         <?php            
  232.         for( $i=$start; $i<$start + $numPerPage; $i++ )
  233.         {
  234.                            
  235.             if(isset($albums[$i]))
  236.             {  
  237.                 $thumb_pool = glob($mainFolder.'/'.$albums[$i].'/thumbs/*{.'.implode(",", $extensions).'}', GLOB_BRACE);
  238.                
  239.                 if (count($thumb_pool) == 0)
  240.                 {
  241.                     $album_thumb = $no_thumb;
  242.                 }
  243.                 else
  244.                 {  
  245.                     $album_thumb = ($random_thumbs ? $thumb_pool[array_rand($thumb_pool)] : $thumb_pool[0]); // display a random thumb or the 1st thumb                
  246.                 } ?>
  247.                                  
  248.                 <div class="thumb-wrapper">
  249.                     <div class="thumb">
  250.                        <a class="showAlb" rel="<?php echo $albums[$i]; ?>" href="<?php echo $_SERVER['PHP_SELF']; ?>?album=<?php echo urlencode($albums[$i]); ?>">
  251.                          <img src="<?php echo $album_thumb; ?>" alt="<?php echo $albums[$i]; ?>" />
  252.                        </a>
  253.                     </div>
  254.                     <div class="caption"><?php echo substr($albums[$i],0,$num_captions_chars); ?></div>
  255.                 </div>
  256.    
  257.             <?php
  258.             }
  259.        
  260.         }
  261.         ?>
  262.          
  263.          <div class="clear"></div>
  264.  
  265.          <div align="center" class="paginate-wrapper">
  266.             <?php
  267.             $urlVars = "";
  268.             $alb = "";
  269.             echo paginate_array($numPages,$urlVars,$alb,$currentPage);
  270.             ?>
  271.          </div>  
  272.     <?php
  273.     }
  274.  
  275. }
  276. else //display photos in album
  277. {
  278.  
  279.     $albums_in_maindir = scandir($mainFolder); 
  280.     $requested_album = sanitize($_REQUEST['album']); // xss prevention
  281.    
  282.     // check requested album against directory traverse
  283.     if (!in_array($requested_album, $albums_in_maindir)) { ?>
  284.         <span class="title">Photo Gallery &raquo; <a href="<?php echo $album_page_url; ?>" class="refresh">Index</a></span>
  285.         <p></p>
  286.         <?php die('Invalid Request');
  287.     }
  288.    
  289.     $album = $mainFolder.'/'.$requested_album;
  290.     $scanned_album = scandir($album);
  291.    
  292.     $files = array_diff($scanned_album, array('..', '.','thumbs','descriptions.txt'));
  293.     $numFiles = count($files); ?>
  294.      
  295.     <div class="p10-lr">
  296.         <?php if($fullAlbum==1) { ?>
  297.             <span class="title"><a href="<?php echo $album_page_url; ?>" class="refresh">Index</a></span>
  298.             <span class="title">&raquo;</span>
  299.         <?php } ?>
  300.         <span class="title"><?php echo $requested_album; ?></span> - <?php echo $numFiles; ?> images
  301.     </div>  
  302.        
  303.     <div class="clear"></div>
  304.    
  305.     <?php
  306.     if($numFiles == 0)
  307.     { ?>
  308.        
  309.          <div class="p10-lr"><p>There are no images in this album.</p></div>
  310.    
  311.     <?php
  312.     }
  313.     else   
  314.     {          
  315.         sort_array($files,$album,$sort_images_by_date); // rearrange array either by date or name
  316.         $numPages = ceil( $numFiles / $numPerPage );
  317.        
  318.         if(isset($_REQUEST['p']))
  319.         {
  320.             $currentPage = ((int)$_REQUEST['p'] > $numPages ? $numPages : (int)$_REQUEST['p']);
  321.         }
  322.          else
  323.         {
  324.             $currentPage=1;
  325.         }
  326.                      
  327.         $start = ($currentPage * $numPerPage) - $numPerPage;
  328.        
  329.         if (!is_dir($album.'/thumbs'))
  330.         {
  331.             mkdir($album.'/thumbs');
  332.             chmod($album.'/thumbs', 0777);
  333.             //chown($album.'/thumbs', 'apache');
  334.         }      
  335.  
  336.         for( $i=0; $i <= $numFiles; $i++ )
  337.         {  
  338.             if(isset($files[$i]) && is_file($album .'/'. $files[$i]))
  339.             {              
  340.                 $thumb_url_end = '</a>';
  341.                 $ext = strtolower(pathinfo($files[$i], PATHINFO_EXTENSION));
  342.                 $prefix = substr($files[$i], 0, -(strlen($ext)+1));
  343.                 $prefix_begin = mb_substr($prefix, 0, 6); // 1st 6 characters of caption           
  344.                 $full_caption = (itemDescription($album, $files[$i]) ? itemDescription($album, $files[$i]) : $prefix); // image captions
  345.                 $num_chars = strlen($full_caption);
  346.                 $caption = ($num_chars > $num_captions_chars ? substr($full_caption,0,$num_captions_chars).'...' : $full_caption);
  347.                 $caption = encodeto($caption);
  348.                    
  349.                 switch($prefix_begin)
  350.                 {
  351.                     case "utube-":
  352.                     $video_id = explode('utube-', $prefix);
  353.                     $video_id = $video_id[1];
  354.                     $thumb_url_start = '<a href="http://www.youtube.com/embed/'.$video_id.'?rel=0&amp;wmode=transparent" title="'.$caption.'" class="albumpix vid">';
  355.                     break;
  356.                    
  357.                     case "vimeo-":
  358.                     $video_id = explode('vimeo-', $prefix);
  359.                     $video_id = $video_id[1];
  360.                     $thumb_url_start = '<a href="http://player.vimeo.com/video/'.$video_id.'" title="'.$caption.'" class="albumpix vid">';
  361.                     break;
  362.                    
  363.                     default:
  364.                     $thumb_url_start = '<a href="'.$album.'/'.$files[$i].'" title="'.$caption.'" class="albumpix">';
  365.                     break;
  366.                 }
  367.                                    
  368.                 if(in_array($ext, $extensions))
  369.                 {                                      
  370.                     $thumb = $album.'/thumbs/'.$files[$i];
  371.                     if (!file_exists($thumb))
  372.                     {
  373.                         make_thumb($album,$files[$i],$thumb,$thumb_width);
  374.                     }      
  375.                    
  376.                     if($i<$start || $i>=$start + $numPerPage) { ?><div style="display:none;"><?php } ?>
  377.                     <div class="thumb-wrapper">
  378.                         <div class="thumb">
  379.                             <?php echo $thumb_url_start; ?><img src="<?php echo $thumb; ?>" alt="<?php echo $files[$i]; ?>" /><?php echo $thumb_url_end; ?>
  380.                         </div>
  381.                         <?php if($show_captions) { ?>
  382.                             <div class="caption">
  383.                                 <?php if($num_chars > $num_captions_chars) { ?>
  384.                                     <div class="tooltip-container"><?php echo $caption; ?><span class="tooltip"><?php echo $full_caption; ?></span></div>
  385.                                 <?php } else { echo $caption; } ?>
  386.                             </div>
  387.                         <?php } ?>
  388.                     </div>
  389.                     <?php if($i<$start || $i>=$start + $numPerPage) { ?></div><?php }
  390.                 }
  391.            
  392.             }
  393.            
  394.         }
  395.        
  396.         ?>
  397.    
  398.         <div class="clear"></div>
  399.          
  400.         <div align="center" class="paginate-wrapper">
  401.             <?php    
  402.             $urlVars = "album=".urlencode($requested_album)."&amp;";
  403.             echo paginate_array($numPages,$urlVars,$requested_album,$currentPage);
  404.             ?>
  405.         </div>
  406.        
  407.         <?php echo (file_exists($album.'/descriptions.txt') ? '<div class="description-wrapper">'.encodeto(itemDescription($album)).'</div>' : ''); //display album description ?>
  408.      
  409.     <?php    
  410.     } // end if numFiles not 0   
  411.  
  412. }
  413. ?>
  414. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement