有人要求我创建一个 PHP 图像库,它将从目录中读取图像,然后为该库创建缩略图。有一个包含缩略图的目录和一个包含全尺寸图像的目录。
我从 /thumb/ 目录中读取文件名,并将每个文件名作为值插入到thumbArray中。从那里我回显缩略图 src 中的值 (
),其中 $i 只是一个柜台。因此,缩略图图像是从数组生成的,但是当您单击缩略图时,它会将 ?filename 查询到 url 中。然后使用 $_SERVER['QUERY_STRING'] 读取所述查询字符串并将查询(文件名)插入到大 ;” >>
。但这是有限的。因为我现在无法读取数组作为参考点,并且无法再在数组中向前或向后指向。
我现在更有意义了吗?
请帮忙...
<?php
$i = 0;
/* Large file name and thumbnail file name must match */
/* Large image size = 480px x 300px */
echo '<img class="frameImg" src="images/large/'.$_SERVER['QUERY_STRING'].'" />';
?>
<p id="prevNext"><a href="#"><< Prev </a> || <a href="#"> Next >></a></p>
</div>
<div id="thumbs">
<ul>
<?php
/* Must change $dir to the full path of directory all the way from root /home/user/domain/images/thumb */
$dir = "*************************";
$dh = opendir($dir);
/* Thumbnail file name and large file name must match */
$thumbArray = array();
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
$thumbArray[$i]=$file;
echo '<li id="'.$i.'"><a href="?'.$thumbArray[$i].'"><img src="images/thumb/'.$thumbArray[$i].'" alt="Alt for '.$thumbArray[$i].'" /></a></li>';
$i++;
}
}
closedir($dh);
?>
Somebody asked me to created a PHP image gallery that will read the images out of a directory and then create thumbnails for the gallery. There is one directory with thumnails, and one directory with fullsize images.
I read the file names out of the /thumb/ directory and insert each file name as a value into the thumbArray. From there I echo out the values in thumbnail src (<img src="<?php echo $thumbArray[$i]; ?>" />
) where $i is just a counter. So the thumbnail images are produced from the array but when you click on the thumbnail, it queries ?filename into the url. Using $_SERVER['QUERY_STRING'] I then read said query string and insert the query, (filename), into the the large <img src"<?php echo $_SERVER['QUERY_STRING']; ?>" />
. That is limited though. As I can now not read the array, as a reference point, and can no longer point forwards or backwards in the array.
Am I making more sense now?
Please help...
<?php
$i = 0;
/* Large file name and thumbnail file name must match */
/* Large image size = 480px x 300px */
echo '<img class="frameImg" src="images/large/'.$_SERVER['QUERY_STRING'].'" />';
?>
<p id="prevNext"><a href="#"><< Prev </a> || <a href="#"> Next >></a></p>
</div>
<div id="thumbs">
<ul>
<?php
/* Must change $dir to the full path of directory all the way from root /home/user/domain/images/thumb */
$dir = "*************************";
$dh = opendir($dir);
/* Thumbnail file name and large file name must match */
$thumbArray = array();
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
$thumbArray[$i]=$file;
echo '<li id="'.$i.'"><a href="?'.$thumbArray[$i].'"><img src="images/thumb/'.$thumbArray[$i].'" alt="Alt for '.$thumbArray[$i].'" /></a></li>';
$i++;
}
}
closedir($dh);
?>
发布评论
评论(1)
如果我理解正确,您只需要从 $thumbArray[$i] 数组中选择下一个和上一个值。
例如。
当然,第一次和最后一次你必须格外小心。 (并检查元素是否存在)
编辑:
好吧,我明白了。
重新排列您的代码。您必须阅读目录的每一页。那么为什么不在代码顶部将其读入数组,并循环遍历该数组来构建缩略图列表。
对于上一个/下一个按钮,您现在可以使用已构建数组中的索引来查找上一个/下一个图像的文件名。
我让自己变得容易理解了吗?
if i understand you correctly you just need to select the next and prev values from your $thumbArray[$i] Array.
eg.
of course for first and last you must take extra care. (and checking the existent of the element)
EDIT:
ok i understand.
Rearange your code. you have to read your directory on every page. so why not read it at top of your code into an array, and loop through this array to build the thumbnail list.
for the prev/next buttons you can now use the index from your already build array to find the filename for the prev/next image.
did i make myself understandble ?