Image 如何使用Coldfusion cfdirectory和randRange函数输出随机文件?
我还在努力掌握Coldfusion 我需要创建一个直接的文件说,有10个文件和输出5个随机文件。获取和输出文件是可以的,但我不确定在randrange中应该放在哪里。这是我的密码:Image 如何使用Coldfusion cfdirectory和randRange函数输出随机文件?,image,random,coldfusion,Image,Random,Coldfusion,我还在努力掌握Coldfusion 我需要创建一个直接的文件说,有10个文件和输出5个随机文件。获取和输出文件是可以的,但我不确定在randrange中应该放在哪里。这是我的密码: <cfdirectory action="list" directory="#expandpath("img/")#" filter="some*.*" name="dir"> <!--- imgID ---> <CFSET imgID= #RandRange(1,
<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>
Coldfusion8告诉我,问题是ArrayDelete不起作用,变量ArrayDelete未定义。你知道我做错了什么吗?我不确定你的代码是否能正常工作,因为里面似乎有几个语法错误。另外,你在img上做了一个目录列表,但是从logo中提取图像,你还没有弄清楚这些目录之间的关系 抛开这些问题不谈,下面是我将如何处理这件事
<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个元素的图像数组,然后您可以将其输出为列表
作为旁注,不建议重复使用和相关功能。如果需要调整图像大小或操作图像,则应将其缓存回磁盘,而不是在每个请求中重复操作。我不确定您的代码是否能正常工作,因为代码中似乎存在多个语法错误。另外,你在img上做了一个目录列表,但是从logo中提取图像,你还没有弄清楚这些目录之间的关系 抛开这些问题不谈,下面是我将如何处理这件事
<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个元素的图像数组,然后您可以将其输出为列表
作为旁注,不建议重复使用和相关功能。如果需要调整图像大小或操作图像,则应将其缓存回磁盘,而不是在每次请求时重复操作。一个简单的替代方法是将阵列洗牌一次,然后取出前五项:
<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# />
一个简单的替代方法是将阵列洗牌一次,然后取出前五项:
<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# />
非常感谢你。关于我的错误,你是对的。现在把它清理干净,然后按照你的建议去做。明白了。见上文。我在ARRAYDELETE中还有一个未定义的问题。并不是说我希望它是一个变量:-有什么想法吗?变量ARRAYDELETE是未定义的-并不是说我计划它是一个变量。我使用的是CF8:-啊,它是ArrayDeleteAt,但CF仍然不喜欢-值C:\path\logos\marke1111111114_test.png.png不能转换为数字谢谢。关于我的错误,你是对的。现在把它清理干净,然后按照你的建议去做。明白了。见上文。我在ARRAYDELETE中还有一个未定义的问题。并不是说我希望它是一个变量:-有什么想法吗?变量ARRAYDELETE是未定义的-并不是说我计划它是一个变量。我使用的是CF8:-啊,它是ArrayDeleteAt,但CF仍然不喜欢-值C:\path\logos\marke1111111114_test.png.png不能转换为数字。在引用ArrayDeleteAt中的变量时,也要使用ArrayDeleteAt,因为不需要输出符号。除非您直接输出一个变量,否则您几乎不需要使用。错误:无法将值C:\path\logos\marke1111111114_test.png.png转换为数字。有关代码样式的说明。唯一需要在s中包围变量或函数的时间是在打算输出结果时。这是不必要的。更具可读性。函数的参数也是如此myfuncvarname是一种错误的做法,myfuncvarname是preferable@ChrisBlackwell:谢谢。刚开始使用CF。考虑到我喜欢上面的CF输出-很抱歉,您需要传入数组中位置的数值,因此在引用数组中的变量时也应该使用ArrayDeleteAt,因为不需要输出符号。除非您直接输出一个变量,否则您几乎不需要使用。错误:无法将值C:\path\logos\marke1111111114_test.png.png转换为数字。有关代码样式的说明。唯一需要在s中包围变量或函数的时间是在打算输出结果时。这是不必要的。更具可读性。函数的参数也是如此myfuncvarname是一种错误的做法,myfuncvarname是preferable@ChrisBlackwell:谢谢。刚开始使用CF。考虑到我喜欢上面的CF输出-哎呀,你需要传入数组中位置的数值,所以它应该是我喜欢的盒子外解决方案:-我喜欢盒子外解决方案:-