Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: stupid error

  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post

    stupid error

    Hi everyone:
    I have this script and I wanna modify the path (the script reads the images file in a dir and create thmbnails for them then view them) it was written to read the images from the same dir that has this script but I dont want that so here is the script :
    PHP 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$imageTmp0000$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(".")) { 

    //im trying here to change the path to /home/mysite/public_html/test/
            

    // 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>Micro Photo Gallery</title>
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
      <div id="main">
        <div class="caption">Micro Photo Gallery</div>
          <table align="center"><tr>     
                <?php displayPhotos(); ?>
          </table>        
                
        <div id="source">Micro Photo Gallery 1.0</div>
      </div>
    </body>
    im trying to change the path in displayPhotos() to /home/mysite/public_html/test/ but it doesnt work
    can anybody help me cuz i still noop in php

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    function generateThumbnails(){
        global 
    $thmb_width,$thmb_height;
        
        
    // Open the actual directory
       
    if ($handle opendir("."
    Replace "." with your relative path
    I don't read all the code and I don't test it, but I beleave that's work.

    best regards
    Last edited by oesxyl; 12-19-2007 at 07:28 PM.

  • #3
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    I did it before but it didnt work can u check the path (trailing slashes or spmething)

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skmd View Post
    I did it before but it didnt work can u check the path (trailing slashes or spmething)
    PHP Code:
          while ($file readdir($handle))  {
                
    // Check whether tha actual item is a valid file
                
    if (is_file($file)){
                    
    // where we are
                    
    echo $file;
                    
    // Check whether the actual image is a thumbnail 
    comment the line after you see where you are until the script work and then you could remove it.

    PHP Code:
    function generateThumbnails(){
        global 
    $thmb_width,$thmb_height;
        
        
    // Open the actual directory
       
    if ($handle opendir("./test/"
    I presume that your script is in public_html and your pictures in public_html/test/

    best regards
    Last edited by oesxyl; 12-19-2007 at 07:53 PM.

  • #5
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    I dont have a problem with generateThumbnails() cuz im not gonna use it
    the problem is in displayPhotos()

  • #6
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    it didnt work too

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skmd View Post
    it didnt work too
    the thumbnail must be generatated before otherwise displayPhoto could not find any.

    you must call generateThumbnaill somewere once, could be for example just before html section of the file at the end of php section..

    The path must be modified in both function.

    best regards

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    <?php

     $columns     
    5;
     
    $thmb_width  120;
     
    $thmb_height 80;
     
    $photodir "./test";

    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$imageTmp0000$new_width$new_height$width$height);

        return 
    $imageResized;


    function 
    generateThumbnails(){
        global 
    $thmb_width,$thmb_height;
        
        
    // Open the actual directory
        
    if ($handle opendir($photodir)) 
    {
            
    // 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 $photodir;
                          
    $fileName basename($file);
                          
    $fileMain substr($fileName,0,strrpos($fileName,$photodir));
                          
    $extName  substr($fileName,strrpos($fileName,$photodir),
                                              
    strlen($fileName)-strrpos($fileName,$photodir));
                          
                          
    // 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($photodir)) { 

    //im trying here to change the path to /home/mysite/public_html/test/
            

    // 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>';    
                        }
                          
                      }
                  }
            }
        }    
    }

    generateThumbnails();

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html>
    <head>
       <title>Micro Photo Gallery</title>
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
      <div id="main">
        <div class="caption">Micro Photo Gallery</div>
          <table align="center"><tr>     
                <?php displayPhotos(); ?>
          </table>        
                
        <div id="source">Micro Photo Gallery 1.0</div>
      </div>
    </body>
    Try this, I don't test it. I replace all occurence of "." related with directory path with $photodir, which is declared at the begining of the file. Replaceing the path in photodir must propagate along the code.
    Also I call generateThumbnails() at th end of php section.

    I hope that's work.

    best regards

  • #9
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    the thumbnail must be generatated before otherwise displayPhoto could not find any.
    I know but i didnt need it cuz I already generate them before

    second, It didnt work it displayed nothing

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skmd View Post
    I know but i didnt need it cuz I already generate them before

    second, It didnt work it displayed nothing
    use same method, echo $file;, in displayPhoto() to see if find it some picture, and adjust the path in $photodir.

    Check the html code generated, I tink is incorect:

    Code:
    <table align="center"><tr>     
                <?php displayPhotos(); ?>
          </table>
    PHP Code:
     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>';    
                        } 
    check if tr tags ar proprely closed.

  • #11
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    I didnt get it, noop
    can u please write it !!

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skmd View Post
    I didnt get it, noop
    can u please write it !!
    Ok, I be back when I finish and test.

    best regards

  • #13
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thank u so much
    Last edited by skmd; 12-20-2007 at 01:09 AM.

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    <?php
    
    $columns     = 5;
    $thmb_width  = 120;
    $thmb_height = 80;
    $imgdir = "test/"; // add a '/' before 'test' if DOCUMENT_ROOT don't end in '/'
    $photodir = $_SERVER['DOCUMENT_ROOT'].$imgdir;
    
    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,$photodir;
        
      // Open the actual directory
      $handle = opendir($photodir);
      if ($handle) {
    	 // Read all file from the actual directory
    	 while ($file = readdir($handle))  {
    		// full path
    		$filepath = $photodir . $file;
    		// Check whether tha actual item is a valid file
    		if (is_file($filepath)){
    		  // Check whether the actual image is a thumbnail
    		  if(!preg_match("/_th\.jpg$/",$filepath)){
    			 $thumb = preg_replace("/\.jpg$/","_th.jpg",$filepath);
    			 if(!file_exists($thumb)){
    				$img = imagejpeg(resizeImage($filepath,$thmb_width,$thmb_height),$thumb,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, $photodir, $imgdir;
        
      $act = 0;
      // Open the actual directory
      $handle = opendir($photodir);
      if ($handle) { 
    
    	 //im trying here to change the path to /home/mysite/public_html/test/
    
    	 // Read all file from the actual directory
    	 while ($file = readdir($handle))  {
    		// Check whether tha actual item is a valid file
    		$filepath = $photodir . $file;
    		if (is_file($filepath)){
    		  // Check whether the actual image is a thumbnail
    		  if(preg_match("/_th.jpg$/",$filepath)){
    			 $nimg = preg_replace("/_th\.jpg$/",".jpg",$file);
    			 ++$act;
    			 if($act > $columns){
    				echo "</tr><tr><td>";
    				$act = 1;
    				echo '<div class="photo">';
    				if($nimg !== $file){
    				  echo '<a href="'.$imgdir . $nimg.'">';
    				}
    				echo '<img src="'.$imgdir . $file.'" alt="'.$file.'"/>';
    				if($nimg !== $file){
    				  echo '</a>';
    				}
    				echo '</div>';
    			 } else {
    				echo '<div class="photo">';
    				if($nimg !== $file){
    				  echo '<a href="'.$imgdir . $nimg.'">';
    				}
    				echo '<img src="'.$imgdir . $file.'" alt="'.$file.'"/>';
    				if($nimg !== $file){
    				  echo '</a>';
    				}
    				echo '</div>';
    			 }
    			 
    		  }
    		}
    	 }
      }    
    }
    
    generateThumbnails();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html>
    <head>
       <title>Micro Photo Gallery</title>
    
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
      <div id="main">
        <div class="caption">Micro Photo Gallery</div>
          <table align="center"><tr><td>
                <?php displayPhotos(); ?>
          </table>        
                
        <div id="source">Micro Photo Gallery 1.0</div>
    
      </div>
    </body>
    </html>
    It work, I test it, you must ajust the path in $imgdir and clean the code.

    best regards

  • #15
    New Coder
    Join Date
    Dec 2007
    Posts
    96
    Thanks
    8
    Thanked 1 Time in 1 Post
    yes it worked man and thank u soooo much but the pictures are showing in in the same columns although the column veriable is set to 5??


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •