resize на картинки от директория.

Smirnoff

Registered
Значи трябва ми скриптче с което да го сложа
в една директория и той да преоразмери всички картинки,
с размери 100х100. Като слага пред всяка thumb_

Примерно картинката се казва smirnoff.jpg
той да я направи с размери 100х100 и име thumb_smirnoff.jpg

Благодаря Ви предварително.
 
Ето кода които ползвам аз

Код:
<?php 

 $columns     = 5; 
 $thmb_width  = 100; 
 $thmb_height = 100; 

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>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>

Сега обаче новата картинка е с име _th.
Трябва да си го промениш както искаш.
Кода след стартиране прави следното:
Всички картинки които са в папката където е ги обхожда и им прави малки копия и после ти ги визуализира.
Надявам се да ти хареса
:wink: :wink: :wink:
 
Dodo1987 каза:
Ето кода които ползвам аз

Код:
<?php 

 $columns     = 5; 
 $thmb_width  = 100; 
 $thmb_height = 100; 

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>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>

Сега обаче новата картинка е с име _th.
Трябва да си го промениш както искаш.
Кода след стартиране прави следното:
Всички картинки които са в папката където е ги обхожда и им прави малки копия и после ти ги визуализира.
Надявам се да ти хареса
:wink: :wink: :wink:
Обясни как точно се използва...че и на мен много ми трябва такова нещо...а от пхп понятие си нямам.
 
Ами това прави следното:
Проверява всички снимки от папката е която е файла и след това с помоща на GD библиотеките в PHP прави малките копия thumb с големина колкото е оказано в началото на скрипта.
След като свърши с това в папката тези малки копия ги записват с желаното име- например снимите са ви 1.jpg, 2.jpg и в този случай както е кода ще се казват 1_th.jpg , 2_th и т.н.
Това име може да се променя като навсякъде в кода замените това _th
с желаното от вас име.
И най накрая ви ги визуализира тези малките картинки като линка води към оригинала на снимката - сиреч 1.jpg.
Този скрипт няма дизайн, защото на мен ми трябваше само да ми ги прави на thumb, но може да вие да си приложите дизайн.
Надявам се, че бях напълно ичерпателен.
Ако има нещо питайте :wink: :wink: :wink:
 
Dodo1987 каза:
Ами нещо от теб е
при мен си работи нормално. Къде го пробваш?
Пробвам го на платен хостинг CHMOD е пълен 7777 .
Рефрешнах няколко пъти вади тая грешка
рефрешнах още веднъж тръгна и сега пак не иска...
 
Ами значи е някои от настроиките на сървъра
За този скрипт трябва GD библиотека, а ако не е разрефена няма как.
Ще го кача на моя да видиш как е
ТУК
 

Back
Горе