2015-03-02 121 views
0

是否有任何方法可以對此腳本設置限制(例如2),以便在修改最新日期時顯示2個圖像?設置最大數量

此腳本向我顯示目錄中的圖像並將其置於修改順序中,因此最新的圖像顯示在前面。現在,我想爲此腳本設置一個最大值。

# To prevent browser error output 
header('Content-Type: text/javascript; charset=UTF-8'); 
# Path to image folder 
$imagefolder = '*'; 
# Show only these file types in the image folder 
$imagetypes = '{*.jpg,*.JPG,*.JPEG,*.png,*.PNG,*.gif,*.GIF}'; 
# Add images to array 
$images = glob($imagefolder.$imagetypes, GLOB_BRACE); 
# Sort the images based on its 'last modified' time stamp 
$sortedImages = array(); 
$count = count($images); 
for ($i = 0; $i < $count; $i++) { 
    $sortedImages[date ('YmdHis', filemtime($images[$i])).$i] = $images[$i]; 
} 

# Set to 'false' if you want the oldest images to appear first 
$newest_images_first = true; 
# Sort images in array 
if($newest_images_first) { 
    krsort($sortedImages); 
} else { 
    ksort($sortedImages); 
} 
# Generate the HTML output 
writeHtml('<ul class="ins-imgs">'); 
foreach ($sortedImages as $image) { 
    # Get the name of the image, stripped from image folder path and file type extension 
    $name = 'Image name: '.substr($image,strlen($imagefolder),strpos($image, '.')-strlen($imagefolder)); 
    # Get the 'last modified' time stamp, make it human readable 
    $last_modified = '(last modified: '.date('F d Y H:i:s', filemtime($image)).')'; 
    # Begin adding 
    writeHtml('<li class="ins-imgs-li">'); 
    writeHtml('<div class="ins-imgs-label">'.$name.' '.$last_modified.'</div>'); 
    writeHtml('<div class="ins-imgs-img"><a name="'.$image.'" href="#'.$image.'">'); 
    writeHtml('<img src="'.$image.'" alt="'. $name.'" title="'. $name.'">'); 
    writeHtml('</a></div>'); 
    writeHtml('</li>'); 
} 
writeHtml('</ul>'); 
writeHtml('<link rel="stylesheet" type="text/css" href="ins-imgs.css">'); 
# Convert HTML to JS 
function writeHtml($html) { 
    echo "document.write('".$html."');\n"; 
} 
+0

也許'array_chunk'? http://php.net/manual/en/function.array-chunk.php – Niols 2015-03-02 13:09:08

回答

0

您可以使用array_slice最多恢復到圖像數組中的第2個元素:

foreach (array_slice($sortedImages,0,2) as $image) { 
    //your code 
} 
+0

謝謝!奇蹟般有效 – 2015-03-02 13:40:13