douyanjing0822 2017-03-29 22:45
浏览 54
已采纳

具有图像限制的文件夹中的图像库

I have some code below, but I cant get the array_splice to limit the number of images shown.

<?php
$id = $_GET['id'];
  if (empty($id)) { 
    $clientname = 'tazz';
} else {
    $clientname = $_GET['id'];
}

$total = $_GET['limit'];
  if (empty($total)) { 
    $limit = '100';
} else {
    $limit = $_GET['limit'];
}

$basefolder = "client/client-galleries/";
$dirname = $basefolder.$clientname."/";

    $images = scandir($dirname);
    sort($images);
    //shuffle($images);
    $ignore = array(".", "..");
    $output = array_splice($ignore, 0, $limit);

foreach($images as $curimg) {
        if(!in_array($curimg, $output)) {

echo "
    <div class='gallery'>
        <img src='[[pThumb? &input=`$dirname$curimg` &options=`&w=300&h=300&zc=1&q=95&fltr[]=wmi|./client/images/gallery-watermark-white.png|BL|50|25|200`]]' alt='$curimg'>
        <div class='desc'></div>
    </div>
 ";

        }
    }

This correctly shows the images in the gallery but doesnt limit the amount of images as set by http://website.address?id=client&limit=5

  • 写回答

2条回答 默认 最新

  • dsg435665475 2017-03-29 23:00
    关注

    You're not applying array_splice to your $images array, but to your $ignore array, which only contains "." and "..". Also, array_splice is used to search/replace within an array, you're probably looking for array_slice, which is used to get a specific part of an array.

    If you use array_diff to remove unwanted values from $images, you can then use array_slice to only keep at most $limit images. Something like this:

    $images = scandir($dirname);
    $ignore = array(".", "..");
    $images_filtered = array_diff($images, $ignore);
    $images_limited = array_slice($images_filtered, 0, $limit);
    

    $images_filtered should contain all items from $images that are not in $ignore. $images_limited should contain the first $limit items from $images_filtered.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度