2012-02-04 82 views
0

我只有php的基本技能,而且我不得不在網站上放置照片畫廊。由於這個網站將被那些對編程知之甚少的人使用,我想要一個簡單的使用方法:上傳一個帶有照片的文件夾(編輯:通過FTP),照片庫縮略圖會自動顯示,點擊縮略圖會顯示滿大小的圖像。PHP/GD - 畫廊中的照片縮略圖:隨機顯示

我使用了一個腳本,可以即時生成縮略圖。如果我直接在瀏覽器中調用它,該腳本似乎工作正常。我獲得了我的縮略圖。但是當我在頁面中顯示一個圖庫時,每個圖像源都是對我的腳本的調用,只顯示縮略圖的一個隨機子集,其他圖標只顯示alt文本。例如,在一個8的畫廊,我第一次顯示的頁面,我可能只得到3,4和7,並刷新後1,3,5,6,8:

我可以找到沒有錯誤信息給出由其他大拇指不加載,但在這裏我的PHP的基本技能可能會失敗我,也許我只是沒有kow在哪裏可以找到這樣的錯誤信息。

這裏是調用我的腳本:

<a href="resources/galleries/example.jpg"><img src="mini.php?f=example.jpg" alt="Photo" /></a> 

和mini.php使用GD:

<?php 
$ratio = 150; 
$dir = './resources/galleries'; 

if (!isset($_GET['f'])) { 
    header('location: index.php'); 
    exit(); 
} 
else { 
    $image = $_GET['f']; 
    $tableau = @getimagesize($dir.'/'.$image); 
    if ($tableau == FALSE) { 
     header('location: index.php'); 
     exit(); 
    } 
    else { 
     // if jpeg 
     if ($tableau[2] == 2) { 
      $src = imagecreatefromjpeg($dir.'/'.$image); 
      if ($tableau[0] > $tableau[1]) { 
      $im = imagecreatetruecolor(round(($ratio/$tableau[1])*$tableau[0]), $ratio); 
      imagecopyresampled($im, $src, 0, 0, 0, 0, round(($ratio/$tableau[1])*$tableau[0]), $ratio, $tableau[0], $tableau[1]); 
      } 
      else { 
      $im = imagecreatetruecolor($ratio, round(($ratio/$tableau[0])*$tableau[1])); 
      imagecopyresampled($im, $src, 0, 0, 0, 0, $ratio, round($tableau[1]*($ratio/$tableau[0])), $tableau[0], $tableau[1]); 
      } 
      header ("Content-type: image/jpeg"); 
      imagejpeg ($im); 
     } 
     elseif ($tableau[2] == 3) { // PNG 
      $src = imagecreatefrompng($dir.'/'.$image); 
      if ($tableau[0] > $tableau[1]) { 
       $im = imagecreatetruecolor(round(($ratio/$tableau[1])*$tableau[0]), $ratio); 
       imagecopyresampled($im, $src, 0, 0, 0, 0, round(($ratio/$tableau[1])*$tableau[0]), $ratio, $tableau[0], $tableau[1]); 
      } 
      else { 
       $im = imagecreatetruecolor($ratio, round(($ratio/$tableau[0])*$tableau[1])); 
       imagecopyresampled($im, $src, 0, 0, 0, 0, $ratio, round($tableau[1]*($ratio/$tableau[0])), $tableau[0], $tableau[1]); 
      } 
      header ("Content-type: image/png"); 
      imagepng ($im); 
     } 
    } 
} 
?> 

任何想法?

回答

0

你說用戶在你的網站上傳文件夾?照片名稱是否有特殊字符或空格,您沒有檢查上傳?

+0

事實上,它只是ftp上傳到網站外部,所以,不,文件名不被檢查。但在這裏,我仔細檢查了名字,沒有任何問題。無論如何,正如我所說的,每個縮略圖有時會顯示,並且所有工作都是在縮略圖直接在瀏覽器中調用的。 – Steph 2012-02-04 13:21:06

+0

好的。它可能是一個路徑錯誤?在子文件夾中是mini.php。因爲如果是這種情況,並且您從圖像中調用它,那麼也許您的'$ dir。'/''不再適用了......如果不是,我很抱歉我沒有找到任何解決方案... – 2012-02-06 15:53:35