循环遍历ColdFusion中多个目录中的所有图像

时间:2013-05-03 15:09:40

标签: loops coldfusion directory image-manipulation coldfusion-8

我正在制作一个图像处理脚本,以便为服务器上已有的图像创建较小的缩略图。

我需要搜索的目录结构如下:

Content
    -Att1
        -image1
            -imgA1_1.png
            -imgA1_1_large.png
        -image2
            -imgA1_2.png
            -imgA2_large.png
        -image3
            -imgA1_3.png
            -imgA1_3_large.png
    -Att2
        -image1
            -imgA2_1.png
            -imgA2_1_large.png
        -image2
            -imgA2_2.png
            -imgA2_2_large.png
        -image3
            -imgA2_3.png
            -imgA2_3_large.png
    -Att3
        -image1
            -imgA3_1.png
            -imgA3_1_large.png
        -image2
            -imgA3_2.png
            -imgA3_2_large.png
        -image3
            -imgA3_3.png
            -imgA3_3_large.png

etc...

所以我想要的是能够遍历上面显示的所有图像,如果该图像的尺寸超过500-500,那么创建一个100-100的缩略图。

如果不循环遍历每个目录,有没有办法做到这一点?

2 个答案:

答案 0 :(得分:5)

cfdirectory具有递归属性。这将循环遍历所有文件夹,但它不会要求您编写代码以循环遍历所有文件夹

<cfdirectory directory="yourDirectory" recurse="yes">

答案 1 :(得分:0)

如果你正在处理非常大的目录,cfdi​​recotry最终会很慢。这是一个使用我写的java的函数,因为我们有一些拥有1000个图像的目录。它会查看并为您的所有文件创建查询结果,并且速度更快

<cffunction name="getDirectorylisting" returntype="query" output="true">
        <cfargument name="dirName" type="string" required="true" />
        <cfargument name="recurse" type="boolean" default="false" required="false" />
        <cfargument name="dirInfo1" type="query" default="#queryNew('datelastmodified,name,size,type,directory,hidden,pathname')#">
            <cfscript>
                var thisFile = '';
                var listFiles = '';
                var pathToParse = trim(dirName);

                var thisPath = '';
                var relPath = '';
                var theFileObj = '';
                var isDir = '';
                var isFile = '';
                var thisSize = '';
                var lastModified = '';
                var isHidden = '';
                var theType = '';

                if (left(dirName,2) == 'c:' || left(dirName,2) == 'd:' || left(dirName,2) == 'e:' || left(dirName,2) == '\\'){
                    //do nothing path is already absolute
                } else {
                    pathToParse = expandPath(pathToparse);
                }
                if(right(pathToParse,1) == '/' || right(pathToParse,1) == '\'){pathToParse = left(pathToParse,len(pathToParse)-1);}
                if(right(dirName,1) == '/' || right(dirName,1) == '\'){dirName = left(dirName,len(dirName)-1);}
                if (directoryExists(pathToParse)){
                    listFiles =  createObject("java","java.io.File").init(pathToParse).list();
                    for (thisFile=1;thisFile<=arrayLen(listFiles);thisFile=thisFile+1){
                        queryAddRow(arguments.dirInfo1);
                        thispath = "#pathToParse#\#listFiles[thisFile]#";
                        relpath = "#dirName#/#listFiles[thisFile]#";
                        theFileObj = createObject("java","java.io.File").init(thispath);
                        isDir = theFileObj.isDirectory();
                        isFile = theFileObj.isFile();
                        thisSize = val(theFileObj.length());
                        lastModified = theFileObj.lastModified();
                        isHidden = theFileObj.isHidden();
                        theType = "dir";
                        if (isFile){theType = "file";}
                        querySetCell(arguments.dirInfo1,"datelastmodified", lastModified );
                        querySetCell(arguments.dirInfo1,"name", listFiles[thisFile] );
                        querySetCell(arguments.dirInfo1,"size", thisSize );
                        querySetCell(arguments.dirInfo1,"directory", pathToParse );
                        querySetCell(arguments.dirInfo1,"hidden", isHidden );
                        querySetCell(arguments.dirInfo1,"type", theType );
                        querySetCell(arguments.dirInfo1,"pathName", relPath );
                        if (arguments.recurse && isDir && !isHidden){
                            arguments.dirInfo1 = getDirectoryListing(relPath,true, arguments.dirInfo1);
                        }
                    }
                }
                return arguments.dirInfo1;
            </cfscript>
    </cffunction>