Jump to content

[SOLVED] ThumbNail Generator Code Problem with directory


Recommended Posts

Thank you to F1FAN for helping me with that ARRAY problem before.

 

Now i have another NEW problem

 

I have  this script i was  learning from an on line php tutorial.

 

The code works fine if the code is in the SAME folder as the  images.

 

But I want to call this script from the main folder where the main index.php is.  The images to make the thumnnails are in seperate sub folders  like 

"/mainimages/gallery2008/"

"/mainimages/gallery2007/"

etc etc

 

 

I noticed that the code checks for $handle=opendir(".")

 

Thats why it works when the code is in EACH of the seperate image folders.

 

So i move the code to the main  index folder and then  i added this:

 

$photodir = './mainimages/gallery2007/';

 

and those two lines in the main code , that have that opendir(".")  to  opendir($photodir)

 

And then the page displays nothing. except the text "photo gallery" on top

 

No error at all.

 

 

 

 

here is the code

 

 


<?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(".")) {
        // 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(".")) {
        // 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>   




ok leaving the code stock  so it works when  loading it from each individual image folder.  As it works that way, so i am trying another way to go about this.

 

When you load the main index there is a simple link to that  php code in them folders.. and it STILl shows up nothing on the page.

 

 

 

 

man this is really frustrating.. i tried every combinatin possible to try to get this to work.. and  it doesnt show anything.

 

II wanna call it from a TPL file.... i put the thumbnail code in  one of the image folders:

 

'mainimages/gallery2007/'

 

I called the file 'thumbs.php'

 

Then in my template folder for the file that loads up . I have it include the thumbs.php from that gallery folder.

 

The file IS INdeed loaded.. cause the title/text "photo gallery"  shows up on top.. as the page is tryign to generate..  but nothign else.. i added a DIE in ther to see where ti is killed and it seems like the folder isnt found even though the php file is RIGHT in that image folder. and that is FOUND!

 

This is really starting to bug the crap out of me!

 

Thanks in advance for your help!!

 

Sincerely

Dog

 

 

 

 

 

 

 

 

 

try using this function maybe that's help  ;)

 

<?php
/*------------------------------------------------------------------------------------------
     Micro Image Manipulation Pack

     ©PhpToys 2006
     http://www.phptoys.com

     Released under the terms and conditions of the
     GNU General Public License (http://gnu.org).

     $Revision: 1.0 $
     $Date: 2006/07/03 $
     $Author: PhpToys $
     
     USAGE:
          This package has 3 function to manipulta jpeg images with php code.
          - resizeImage : needs a filename and a width, height values.
          - dropShadow  : needs an image as image resource and not as filename, and you can define 
                          the shadow size.
          - createBorder: needs an image as image resource and not as filename, and the border 
                          width and height values.
--------------------------------------------------------------------------------------------*/
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 createBorder($img,$x,$y){
    
    // Create image base 
    $image           = imagecreatetruecolor($x,$y);
    $backgroundColor = imagecolorallocate($image,255,255,255);
    $borderColor     = imagecolorallocate($image,50,50,50);
    
    imagefill($image,0,0,$backgroundColor);
    imagerectangle($image,0,0,$x-1,$y-1, $borderColor);

    $width  = imagesx($img);
    $height = imagesy($img);
    
    imagecopymerge($image,$img,($x-$width)/2,($y-$height)/2,0,0,$width,$height,100);

    return $image;
}


function dropShadow($img,$shadowSize=5){

  // Set the new image size  
  $width  = imagesx($img)+$shadowSize;
  $height = imagesy($img)+$shadowSize;
  
  $image = imagecreatetruecolor(imagesx($img)+$shadowSize, imagesy($img)+$shadowSize);

  for ($i = 0; $i < 10; $i++){
    $colors[$i] = imagecolorallocate($image,255-($i*25),255-($i*25),255-($i*25));
  }

  // Create a new image
  imagefilledrectangle($image, 0,0, $width, $height, $colors[0]);

  // Add the shadow effect
  for ($i = 0; $i < count($colors); $i++) {
    imagefilledrectangle($image, $shadowSize, $shadowSize, $width--, $height--, $colors[$i]);
  }

  // Merge with the original image
  imagecopymerge($image, $img, 0,0, 0,0, imagesx($img), imagesy($img), 100);

  return $image;
  
}

?>

 

<?php 
    //Resize the image
    $myimg1 = resizeImage('test.jpg',180,120);
    // Creat the new file
    imagejpeg($myimg1,'result1.jpg',100);
    //Display the new image
    echo '<img src="result1.jpg" border="0">';
    
    echo "<br/>Now add a shadow effect to the image:<br/>";
    //Drop shadow
    $myimg2 = dropShadow($myimg1);
    // Creat the new file
    imagejpeg($myimg2,'result2.jpg',100);
    //Display the new image
    echo '<img src="result2.jpg" border="0">';

    echo "<br/>And at least add a border to it:<br/>";
    //Add border
    $myimg3 = createBorder($myimg2,220,220);
    // Creat the new file
    imagejpeg($myimg3,'result3.jpg',100);
    //Display the new image
    echo '<img src="result3.jpg" border="0">';
    
?>

Hope that's help

 

I think you mistunderstanding the problem. 

 

In the code that i pasted. It does generate the thumbnails and it works fine when i call the php file  from image folder itself.  The thumbs are displayed and everything works fine. So i know the code works...

 

But when i call the code up from a different location like a link on the main page thats in a different  location on site,  the code starts .. prints the text photogallery"  and then halts!

 

its a problem with the actual location of the code from the main site itself that i cant seem to get worked out.

 

 

Medhi,

 

Your code quote isnt doing what i need,  in fact it is doing the oposite.

It is defeating the pupose of what I need to do.

 

I will to try to explain this again. 

 

I need the  code to read a WHOLE directory of image files, and then make thumnails in a an html table that specifys number of columns. Like I have been saying,  the code I pasted WORKS when it is called up from the specific image folder as a STAND ALONE php file.

 

It generates the thumbnails etc etc.  SO I know it is working code.

 

 

But the problem is when I call the code up from a template file from another location on the site it does nothing except prints the text PHOTO GALLERY then stops.  Thats where my problem is trying to get the code to work when it is called from another location.

 

I even bypassed the template files and tried to call the code up from the MAIN ROOT still as STAND ALONE file,  and adding the $photodir arable changes as mentioned above.  And it still doesnt work.. just printsd the text PHOTO GALLERY as if it os trying to run, but doesnt.  So it isnt the template giving it a problem. 

 

It is a problem with the code being called from another location!

 

This is totally buggin me i been on this thing all night trying every which way i can think of!

 

:(

 

 

 

 

 

  • 2 weeks later...

Man I forgot about this post!..  here is my followup.

 

Anyways,  for me to fix my problem I wrote my own code from scratch  and  it calls up fine from the folder i wanted it to in the first place.  I dont knwo what the heck why it was doing witht hat other code. but it works better now since wrote my own from scratch..

 

Hey thats the learning process  )

 

I will mark this solved!

 

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.