1

ファンシーボックスを使用しているときに、ディレクトリからギャラリーにデータを入力するコードを最終的に取得しました。クリックすると大きな画像が表示される基本的なサムネイル ギャラリーです。唯一の問題は、サムネイル ディレクトリから多くのファイルが失われていることです。

このコードは、大きな画像のすべてのリンクを取得しますが、すべてのサムネイルを取得するわけではなく、それらの一部のみを取得し、順序も正しくありません。

私のコードで何が間違っていますか?

<?php
$directory = 'thumb';   //where the gallery thumbnail images are located
$allowed_types=array('jpg','jpeg','gif','png');//allowed image types
$file_parts=array(); $ext=''; $title=''; $i=0;//try to open the directory 
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))    //traverse through the files 
{ if($file=='.' || $file == '..') continue; //skip links to the current and parent  directories 
$file_parts = explode('.',$file); //split the file name and put each part in an array
$ext = strtolower(array_pop($file_parts));  //the last element is the extension 
$title = implode('.',$file_parts); //once the extension has been popped out, all that   is left is the filename
$title = htmlspecialchars($title);  //make the filename html-safe to prevent potential    security issues 
natsort($file_parts); //sort by filename--NOT WORKING
$nomargin='';
if(in_array($ext,$allowed_types))   //check if the extension is an allowable type
{
if(($i+1)%4==0) $nomargin='nomargin';   //the last image on the row is assigned the CSS class "nomargin" 

//Begin thumbs containers with fancybox class 
echo '<div class="thumbs fancybox '.$nomargin.'"   style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;"> <a rel="group" 
href="images/'.$file.'" title="'.$title.'">'.$title.'</a> 
</div>'; 
$i=0; //increment the image counter 
} } closedir($dir_handle); //close the directory
?>
4

1 に答える 1

0

サムネイルのリストを保護するために使用する別の方法:

$directory = 'thumb';   //where the gallery thumbnail images are located    
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename

次に、それをレンダリングするには、次のようにします。

<?php
for($x = 0; $x < count($files); $x++):
    $thumb = $files[$x];
    $file = basename($thumb);
    $nomargin = $x%4 == 0?" nomargin":"";
    $title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>"
     style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
     <a rel="group" href="images/'.<?= $file ?>" title="<?= $title ?>"><?= $title ?></a> 
</div>
<?php
endfor;
于 2013-07-02T05:54:50.293 に答える