我仍然试图抓住Coldfusion ......
我需要创建一个直接的文件(比如有10个文件)并输出5个随机文件。获取和输出文件是可以的,但我不确定在randrange中的哪个位置。这是我的代码:
<cfdirectory action="list" directory="#expandpath("img/")#" filter="some*.*" name="dir">
<!--- imgID --->
<CFSET imgID= #RandRange(1, #dir.allRecords#)#>
<!--- this only grabs the first 5 files --->
<cfoutput query="dir" maxrows="5">
<cfif FileExists("#expandpath("img/#name#")#")>
<cfimage source="#expandpath("img/#name#")#" name="myImage"> <cfif IsImage(myImage) is true>
<cfset ImageSetAntialiasing(myImage,"on")>
<cfset ImageScaleToFit(myImage,"highestQuality")>
<!--- append to a list --->
<li><cfimage source="#myImage#" action="writeToBrowser"></li>
</cfif>
</cfif>
</cfoutput>
这可以显示前5个图像。但是,我希望有5个随机图像。
感谢您的一些见解!
修改
这就是我最终做到这一点 - 一个问题没有解决 -
<!-- get the directy, listinfo="name" because I only need filenames --->
<cfdirectory action="list" LISTINFO="name" directory="#expandpath(" logos/")#" filter="marke*.*" name="dir">
<cfset images=[ ]>
<!-- since dir is not indexable, like dir[pos], I need another array!-->
<cfset dirArr=[ ]>
<cfset blocker="false">
<cfset maxLogos=5>
<!-- fill new dirArr(ay) -->
<cfoutput query="dir">
<cfset #ArrayAppend(dirArr, #expandpath( "logos/#name#")#)#>
</cfoutput>
<!-- loop -->
<cfloop condition="blocker eq false">
<-- random position -->
<cfset pos=R andRange(1, #dir.recordcount#)>
<cfif #dir.recordcount# eq 0 OR #ArrayLen(images)# gte #maxLogos#>
<-- STOP loop -->
<cfset blocker="true">
</cfif>
<cfset ArrayAppend(images, #dirArr[pos]#)>
<!-- BROKEN unknown ARRAYDELETE -->
<!--- <cfset ArrayDelete(dirArr, #dirArr[pos]#)> --->
<!-- IMG -->
<cfimage source="#dirArr[pos]#" name="myImage">
<cfif IsImage(myImage) is true>
<cfoutput>
<li data-icon="false">
<cfimage source="#myImage#" action="writeToBrowser">
</li>
</cfoutput>
</cfif>
</cfloop>
问题是ArrayDelete不起作用变量ARRAYDELETE未定义,Coldfusion(8)告诉我。知道我做错了吗?
答案 0 :(得分:3)
一个简单的替代方法是将数组洗牌一次,然后取前五项:
<cfset MaxLogos = 5 />
<cfset Images = [] />
<cfset Files = DirectoryList( expandPath("logos") , false, "name" , "marke*.jpg" ) />
<cfset createObject( "java", "java.util.Collections" ).shuffle( Files ) />
<cfloop index="i" from="1" to=#Min(MaxLogos,ArrayLen(Files))# >
<cfset ArrayAppend( Images , Files[i] ) />
</cfloop>
<cfdump var=#Images# />
答案 1 :(得分:1)
我不确定你的代码是否会真正起作用,因为它中似乎存在一些语法错误。你还在 img 上做一个目录列表,然后从 logos 中提取图像,你还没弄清楚这些目录之间的关系是什么
抛开这些问题,这就是我如何处理这个问题。<cfscript>
// this code is untested, but should get you going
// get list of image file names as an array
dir = directoryList(expandPath("imgs"), false, "name", "*.jpg");
images = [];
while(true) {
// if out directory list is now empty or we have 5 results, we're done
if(!arrayLen(dir) or arrayLen(images) gte 5) break;
// get an image from a random point in the list
pos = randrange(1, arrayLen(dir));
// append it to our images array
arrayAppend(images, dir[pos]);
// delete form the source array, this avoids duplicates in further iterations
arrayDeleteAt(dir, pos);
}
</cfscript>
这将为您提供一个包含0到5个元素的图像数组,然后您可以将其作为列表输出。
作为附注,不建议使用&lt; cfimage&gt;和相关的功能反复。如果您需要调整图像大小或操作图像,则应将其缓存回磁盘,而不是每次请求都重复操作。