Can some one check this gallery code

have found this bit of code which is suppose to be a simple gallery code
but i cant seem to get it to work
on my server i have a folder called pics where all jpgs are and called the php code test.php
if some one could have a look for me
thanks
Doug

<body>
<?php

$columns     = 5;
$thmb_width  = 120;
$thmb_height = 80;

function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }
    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, 
                       $new_width, $new_height, $width, $height);

    return $imageResized;
} 

function generateThumbnails(){
    global $thmb_width,$thmb_height;
    
    // Open the actual directory
    if ($handle = opendir("./pics/")) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                // Check whether the actual image is a thumbnail
                  if (strpos($file,'_th.jpg')){
                      $isThumb = true;
                  } else {
                      $isThumb = false;
                  }
              
                  if (!$isThumb) {
                      // Process the file string
                      $dirName  = substr($file,0,strpos($file,basename($file)));
                      if (strlen($dirName) < 1) $dirName = '.';
                      $fileName = basename($file);
                      $fileMain = substr($fileName,0,strrpos($fileName,'.'));
                      $extName  = substr($fileName,strrpos($fileName,'.'),
                                          strlen($fileName)-strrpos($fileName,'.'));
                      
                      // Check if the actual file is a jpeg image
                      if (($extName == '.jpg') || ($extName == '.jpeg')){
                        $thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
                        // If a thumbnail dosn't exists tahn create a new one
                        if (!file_exists($thmbFile)){
                            imagejpeg(resizeImage($file,$thmb_width,$thmb_height)
                                     ,$thmbFile,80);
                        }
                    }
                  } 
               }
           }
    }
    
}

function getNormalImage($file){
    $base = substr($file,0,strrpos($file,'_th.jpg'));
    if (file_exists($base.'.jpg')) return $base.'.jpg';
    elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
    else return "";
}

function displayPhotos(){
    global $columns;
    
    generateThumbnails();
    $act = 0;
    // Open the actual directory
    if ($handle = opendir("./pics/")) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                // Check whether the actual image is a thumbnail
                  if (strpos($file,'_th.jpg')){
                    ++$act;
                    if ($act > $columns) {
                        echo '</tr><tr>
                           <td class="photo"><a href="'.getNormalImage($file).'">
                               <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                        $act = 1;
                    } else {
                        echo '<td class="photo"><a href="'.getNormalImage($file).'">
                           <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                    }
                      
                  }
              }
        }
    }    
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Photo Gallery</title>
</head>
<body>
  <center><h3>Photo Gallery</h3></center>
  <table align="center"><tr>     
    <?php displayPhotos(); ?>
  </table>        
</body>   

</html>

Does it have a specific error? Or nothing gets outputted at all?
I also would advice you to check glob:
http://php.net/manual/en/function.glob.php

Then you could do something like:


function displayPhotos($columns){
    
  generateThumbnails();
  $act = 0;
  $imageFiles = glob('./pics/*_th.jpg');

  foreach ($imageFiles as $file) {
    $act++;
    if ($act > $columns) {
      echo '</tr><tr>
              <td class="photo"><a href="'.getNormalImage($file).'">
              <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
      $act = 1;
    } else {
       echo '<td class="photo"><a href="'.getNormalImage($file).'">
               <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
    }
  }
}

It is better practice in my opinion to avoid the global keyword and just pass it to the function for you $columns variable.

Have you tried turning on error output?
error_reporting(E_ALL | E_STRICT);

But only do this during development. During the actual use of the api turn it off:
error_reporting(0);

Are the thumbnails being created in your pics folder? have you checked?
It’s difficult to pin point the problem without a bit more of info :slight_smile:

thanks for quick reply
all that is happening is its displaying the gallery page but nothing in it
also i have put all this code in 1 file is that correct
cheers
Doug
newbie

Can you go physically to the pics folder? and check if there are any thumbnails in there? If not, then the problem is in the generation of the thumbs. Otherwise there is a problem in displaying them.

Putting all code into 1 file is fine for small projects.
But the bigger things get, the better it might be to split up the code in more files and include them when needed. One step further will be OOP and classes. But that is running, better to first walk :slight_smile:
If you want to use multiple files you can check the following functions:
require_once:
http://php.net/manual/en/function.require-once.php

but for learning it’s good to have everything in 1 file, it’s a better overview

Edit
I just Thought of something, if you are testing this on a websrver, maybe the file permissions of the pics folder aren’t correct.
PHP needs to be able to access it to create the thumbs. the permissions should 777.

thanks for all your help
have found a script that works to a fashion it uploads and creates thumbnails
cheers
Doug

Hello,

I want to rezise original image to.

original image and thum image in this script.
please.