Jump to content

image gallery script


xevean

Recommended Posts

 

hi, i have a php script that automatically gets all the .jpg/.jpeg pictures from x directory and makes thumbnails and links them to the original image. however whenever i set the directory path to something besides the current directory, the images don't generate.

 

the directory that the images are in is called "images"

 

 

 

<?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("./images/")) {
        // 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("./images/")) {
        // 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 '<a class ="ximg" href="'.getNormalImage($file).'" rel="photoslice@samples"><img src="'.$file.'"  alt="" hspace="10" vspace="10" class="ximg"/></a>';    
                        $act = 1;
                    } else {
echo '<a class ="ximg" href="'.getNormalImage($file).'" rel="photoslice@samples"><img src="'.$file.'"  alt="" hspace="10" vspace="10" class="ximg"/></a>';    
                    }
                      
                  }
              }
        }
    }    
}



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
  <link rel="stylesheet" type="text/css" href="final.css" />
<script type="text/javascript" src="functions.js"></script>
   <title>Photo Gallery</title>
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="Pcontent">
      <!--DWLayoutTable-->
      <tr>
        <td width="29" height="23"> </td>
        <td width="703"> </td>
        <td width="24"> </td>
      </tr>
      <tr>
        <td height="434"> </td>
        <td valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" id="Cborder">
          <!--DWLayoutTable-->
          <tr id="PtopicTop">
            <td width="703" height="28" valign="top" id="PtopicTop">Image Galery</td>
          </tr>
          <tr id="TopicBody">
            <td height="381" valign="top" id="TopicBody"><?php displayPhotos(); ?></td>
          </tr>
          <tr id="PtopicBot">
            <td height="25" valign="top" id="PtopicBot"></td>
          </tr>
        </table>        </td>
        <td> </td>
      </tr>
      <tr>
        <td height="19"> </td>
        <td> </td>
        <td> </td>
      </tr>
    </table>
     
</body>   
</html>

 

any help with this would be greatly appreciated.

 

xevean

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.