Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.07 KB | None | 0 0
  1. <?php
  2.  
  3. $imgbrdr     = 1;
  4. $columns     = 5;
  5. $thmb_width  = 80;
  6. $thmb_height = 80;
  7.  
  8. function resizeImage($originalImage,$toWidth,$toHeight){
  9.    
  10.     // Get the original geometry and calculate scales
  11.     list($width, $height) = getimagesize($originalImage);
  12.     $xscale=$width/$toWidth;
  13.     $yscale=$height/$toHeight;
  14.    
  15.     // Recalculate new size with default ratio
  16.     if ($yscale>$xscale){
  17.         $new_width = round($width * (1/$yscale));
  18.         $new_height = round($height * (1/$yscale));
  19.     }
  20.     else {
  21.         $new_width = round($width * (1/$xscale));
  22.         $new_height = round($height * (1/$xscale));
  23.     }
  24.     // Resize the original image
  25.     $imageResized = imagecreatetruecolor($new_width, $new_height);
  26.     $imageTmp     = imagecreatefromjpeg ($originalImage);
  27.     imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0,
  28.                        $new_width, $new_height, $width, $height);
  29.  
  30.     return $imageResized;
  31. }
  32.  
  33. function generateThumbnails(){
  34.     global $thmb_width,$thmb_height;
  35.    
  36.     // Open the actual directory
  37.     if ($handle = opendir(".")) {
  38.         // Read all file from the actual directory
  39.         while ($file = readdir($handle))  {
  40.             // Check whether tha actual item is a valid file
  41.             if (is_file($file)){
  42.                 // Check whether the actual image is a thumbnail
  43.                   if (strpos($file,"_th.jpg")){
  44.                       $isThumb = true;
  45.                   } else {
  46.                       $isThumb = false;
  47.                   }
  48.              
  49.                   if (!$isThumb) {
  50.                       // Process the file string
  51.                       $dirName  = substr($file,0,strpos($file,basename($file)));
  52.                       if (strlen($dirName) < 1) $dirName = ".";
  53.                       $fileName = basename($file);
  54.                       $fileMain = substr($fileName,0,strrpos($fileName,"."));
  55.                       $extName  = substr($fileName,strrpos($fileName,"."),
  56.                                           strlen($fileName)-strrpos($fileName,"."));
  57.                      
  58.                       // Check if the actual file is a jpeg image
  59.                       if (($extName == ".jpg") || ($extName == ".jpeg")){
  60.                         $thmbFile = $dirName."/".$fileMain."_th.jpg";
  61.                         // If a thumbnail dosn't exists tahn create a new one
  62.                         if (!file_exists($thmbFile)){
  63.                             imagejpeg(resizeImage($file,$thmb_width,$thmb_height)
  64.                                      ,$thmbFile,80);
  65.                         }
  66.                     }
  67.                   }
  68.                }
  69.            }
  70.     }
  71.    
  72. }
  73.  
  74. function getNormalImage($file){
  75.     $base = substr($file,0,strrpos($file,"_th.jpg"));
  76.     if (file_exists($base.".jpg")) return $base.".jpg";
  77.     elseif (file_exists($base.".jpeg")) return $base.".jpeg";
  78.     else return "";
  79. }
  80.  
  81. function displayPhotos(){
  82.     global $columns;
  83.    
  84.     generateThumbnails();
  85.     $count = 0;
  86.     // Open the actual directory
  87.     if ($handle = opendir(".")) {
  88.         // Read all file from the actual directory
  89.         while ($file = readdir($handle))  {
  90.             // Check whether tha actual item is a valid file
  91.             if (is_file($file)){
  92.                 // Check whether the actual image is a thumbnail
  93.                   if (strpos($file,"_th.jpg")){
  94.                     $count++;
  95.                     if ($count > $columns) {
  96.                         echo "\n</tr><tr>\n<td class=\"photo\">\n<a href=\"".getNormalImage($file)."\"><img src=\"".$file."\" alt=\"".$file."\" border=\"".$imgbrdr."\"/></a>\n</td>";    
  97.                         $count = 1;
  98.                     } else {
  99.                         echo "\n<td class=\"photo\">\n<a href=\"".getNormalImage($file)."\"><img src=\"".$file."\" alt=\"".$file."\" border=\"".$imgbrdr."\"/></a>\n</td>";    
  100.                     }
  101.                      
  102.                   }
  103.               }
  104.         }
  105.     }    
  106. }
  107.  
  108. ?>
  109. <center><h1>Photo Gallery</h1></center>
  110. <table align="center">
  111. <tr>
  112. <?php displayPhotos(); ?>
  113. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement