Please help me rewrite this script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chuspy
Forum Newbie
Posts: 2
Joined: Sat Jun 07, 2008 1:14 pm

Please help me rewrite this script

Post by chuspy »

Moved to PHP - Code by moderator.
I have one scrit and need little modification for it. Script takes all files from folder and makes thumbs from them with links to original size image, and works very well. Now I want to make little modification, that is that images have names. Name should be filename without .jog and _ should be replaced with space (for example image_one.jpg should have ALT name image one). Script is:

Code: Select all

<?php
 
 $columns     = 3;
 $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 
        $files = array(); 
        // ovdje sve fajlove iz dira spremas u jedan array 
        while (false !== ($file = readdir($handle))) { 
            $info = stat($file); 
            $files[$info['mtime']] = $file; 
        } 
        if (!empty($files)) { 
            // ovdje taj array sortiras prema datumu 
            ksort($files); 
            // te ovdje odvrtis ispis prema tom sortiranom arrayu 
            foreach ($files as $file) { 
                // 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>';     
                        } 
                    } 
                } 
            } 
        } 
    }     
}  
 
?>
How to make this modification? Thanks in advance.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: Please help me rewrite this script

Post by Bruno De Barros »

I would do:

Code: Select all

 
$alt = str_ireplace('.jpg','', $filename); # str_ireplace because it can either be JPG or jpg, it happens sometimes.
$alt = str_replace('_',' ', $alt);
 
Of course you would have to change $filename to whatever the variable that holds your filename.
Post Reply