【问题标题】:PHP/GD - photo thumbnails in a gallery: random displayPHP/GD - 画廊中的照片缩略图:随机显示
【发布时间】:2012-02-04 21:24:37
【问题描述】:

我只有基本的 php 技能,我不得不把照片画廊放在网站上。由于这个网站会被对编程知之甚少的人使用,所以我想要一个简单的使用:上传一个包含照片的文件夹(编辑:通过FTP),照片库缩略图会自动显示,点击缩略图会显示完整大小图像。

我使用了一个即时生成缩略图的脚本。如果我直接在浏览器中调用它,这个脚本似乎工作正常。我得到我的缩略图。但是当我在我的页面中显示一个画廊时,每个图像源都是对我的脚本的调用,只显示缩略图的随机子集,其他只显示替代文本。例如,在一个 8 的画廊上,我第一次显示页面时可能只得到 3,4 和 7,而在刷新 1,3,5,6,8 后:

我找不到其他拇指未加载的错误消息,但在这里我的 php 基本技能可能会让我失败,也许我只是不知道在哪里可以找到这样的错误消息。

这是对我的脚本的调用:

<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); 
        }
    }
}  
?>

有什么想法吗?

【问题讨论】:

    标签: php image gd


    【解决方案1】:

    您说用户在您的网站上上传了一个文件夹?会不会是照片名称中有特殊字符或空格,您在上传时没有检查?

    【讨论】:

    • 其实只是网站外的ftp上传,所以,不,不检查文件名。但是在这里,我仔细检查了名称,没有问题。无论如何,正如我所说,每个缩略图有时都会显示,并且在直接在浏览器中调用缩略图时所有工作。
    • 好的。会不会是路径错误? mini.php 是否在子文件夹中。因为如果是这种情况并且您从图像中调用它,那么您的 '$dir.'/'' 可能不再合适了...如果不是,我很抱歉我没有找到任何解决方案...
    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多