使用next/prev按钮在php中显示来自sql的图像


Displaying images from sql in php with next/prev buttons

如何更改此代码,使其一次只显示一个图像,并使用下一个和上一个按钮浏览图像。

我使用了这个网站的代码

$sql = "select * from people";
    $result = mysql_query($sql) or die ("Could not access DB: " .  mysql_error());
    while ($row = mysql_fetch_assoc($result))
    {
        echo "<div class='"picture'">";
        echo "<p>";
// Note that we are building our src string using the filename from the database
        echo "<img src='"images/" . $row['filename'] . "'" alt='"'" /><br />";
        echo $row['fname'] . " " . $row['lname'] . "<br />";
        echo "</p>";
        echo "</div>";

如果没有人愿意帮忙,他们能给我指一个可能有答案的教程或网站的方向吗。我是php的新手,所以非常感谢所有的帮助。

$page = $_GET['page'];    
$sql = "select * from people LIMIT $page,1";
while(...){
  ...
  $next_page = $page+1;
  $prev_page = $page-1;
  $next_btn = "<a href='script.php?page=".$next_page."'>Next</a>";
}

这里有一个基本的实现,不要忘记负/最大验证和mysql注入!

图像裁剪。你可以通过url中的参数来获得你想要的输出图像大小,比如image.php?src=img/random.jpg&w=300&h=200

<?php
header("Content-type: image/jpeg");
$image = imagecreatefromjpeg($_GET['src']);
$thumb_width = $_GET['w'];
$thumb_height = $_GET['h'];
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if($original_aspect >= $thumb_aspect) {
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
} else {
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
imagejpeg($thumb);
?>

在一次将一个图像放在页面上并浏览它们方面,请使用上一个和下一个按钮。这需要一些javascript来实现。

有很多不错的图库可以一次显示一个图像,而且它们已经解决了图像大小调整的问题。看看HERE