2012-08-25 47 views
1

我正在运行coldfusion8/MySQL5.0.88,并且我有一个产品搜索,我需要在显示它们之前从各种外部源获取和调整图像大小。是否可以使用Coldfusions cfthread延迟加载图像?

尽管我可以在产品创建时(而不是搜索时)主要做到这一点,但我无法确保所有图像都能从我的服务器调整大小,因此我需要在产品搜索中进行某种回退。

现在,我检查图像是否可用服务器上,如果没有,我火了例行像这样抢,并调整图片的大小:

// check if image is available 
<cfif results.typ NEQ "img" AND results.typ NEQ "alt"> 
    <cfif results.bildpfad neq "" AND results.bilddateiname neq ""> 
    <cfset imgSuccess = form_img_handler.upload 
     (command = "upload_search" 
     , imgPath = results.bildpfad 
     , imgFile = results.bilddateiname 
     , sellerILN = results.iln 
     , cookie = variables.screenWidth 
     )/> 
    <cfif imgSuccess EQ "false"> 
     <cfset variables.imgFail = "true"> 
    </cfif> 
</cfelse> 
    <cfset variables.imgFail = "true"> 
</cfif> 

// missing img 
<cfif variables.imgFail EQ "true"> 
    <cfoutput><div class="resultsImgWrap noImgFound"></div></cfoutput> 
    <cfset variables.imgFail = "false"> 
<cfelse> 
// show image 
    <cfoutput><div class="resultsImgWrap"><img src="#variables.imageSrc#" /></div></cfoutput> 
</cfif> 

upload函数处理cfhttp请求调整大小并返回true/false

我想知道在这种情况下是否可以使用cfthread,所以当用户进行搜索时,我输出包含正确URL链接的标记,但图像拉/调整大小/保存到目的地将在一个cfthread,以加快显示用户的结果。

问题
将图像显示出来,一旦cfthread处理完成后或将这种方法产生试图加载尚未创建(可能)的图像时,404错误?是否有任何其他方式向用户展示某些内容,并在用户上传和处理图像时让用户继续操作?

感谢您的意见!

编辑:
好的。根据我的回答,我提出了以下几点。虽然还没有工作,但我认为这是朝着正确的方向发展。

// check media log, if image is logged (already created), if so, load it, if not pull it from external 
<cfif results.typ NEQ "img" AND results.typ NEQ "alt"> 
    // check path and filename 
    <cfif results.bildpfad neq "" AND results.bilddateiname neq ""> 
     // pull in 
     // arguments: 
     // cm = form 
     // pt = path to image 
     // fl = filename 
     // se = seller id 
     // ck = screen width (I'm using adaptive image sizes 
     // ax = origin 
     // gb = which image size to return 
     <cfset variables.imgSrc = expandPath("../services/img_handler.cfc") & "?method=up&cm=results&pt=" & results.bildpfad & "&fl=" & results.bilddateiname & "&se=" & results.id & "&ck=" & variables.screenWidth & "&ax=rm&gb=s"> 
    <cfelse> 
     cfset variables.imgFail = "true"> 
    </cfif> 
</cfif> 

<cfif variables.imgFail EQ "true"> 
    <cfoutput><div class="resultsImgWrap noImgFound"><img src="../images/not_found.png"></div></cfoutput> 
    <cfset variables.imgFail = "false"> 
<cfelse> 
    <cfoutput><div class="resultsImgWrap"><a class="swipeMe" rel="external" href="#variables.zoomSrc#"> 
       <img src="#variables.imageSrc#" class="adaptImg ui-li-thumb" /></a> 
       </div></cfoutput> 
</cfif> 

所以这将检查我的媒体日志中的图像,我与查询结果一起查询(避免不必要的s3检查现有图像)。如果没有图像日志中,我检查路径/文件名不能为空,引发我intelligent图像加载器,它具有以下功能:

<cfcomponent output="false" hint="image handler"> 
    <cffunction name="Init" access="public" returntype="any" output="false" hint="Initialize"> 
     <cfreturn true /> 
    </cffunction>  

    <cffunction name="upload" access="remote" output="false" hint="creates images and stores them to S3"> 
     <cfargument name="cm" type="string" required="true" hint="" /> 
     <cfargument name="pt" type="string" required="true" hint="" /> 
     <cfargument name="fl" type="string" required="true" hint="" /> 
     <cfargument name="se" type="string" required="true" hint="" /> 
     <cfargument name="ck" type="string" required="true" hint="" /> 
     <cfargument name="gb" type="string" required="false" hint="" /> 
     <cfargument name="ax" type="string" required="false" hint="" /> 

     <cfscript> 
      var LOCAL = {}; 
      // arguments 
      LOCAL.command = cm; 
      LOCAL.imgPath = pt; 
      LOCAL.imgFile = fl; 
      LOCAL.sellerILN = se; 
      LOCAL.cookie = LSParseNumber(ck); 
      LOCAL.getBack = gb; 
      LOCAL.access = ax; 
      // s3 
      // commander 
      if (LOCAL.command NEQ "") { 
       LOCAL.action = LOCAL.command; 
      } else { 
       LOCAL.action = "upload"; 
       } 
      // s3 misc 
      LOCAL.bucketPath = Session.bucketPath; 
      LOCAL.bucketName = Session.bucketName; 
      LOCAL.acl = "public-read"; 
      LOCAL.storage = ""; 
      LOCAL.tempDirectory = expandPath("../members/img/secure/"); 
      LOCAL.allow = "png,jpg,jpeg"; 
      LOCAL.failedLoads = ""; 
      LOCAL.altFailedLoads = ""; 
      LOCAL.createBucket = "false"; 
      LOCAL.errorCount = 0; 
      LOCAL.altErrorCount = 0; 
      LOCAL.cacheControl = 1; 
      LOCAL.contentType = "image"; 
      LOCAL.httptimeout = "300"; 
      LOCAL.cacheDays = "30"; 
      LOCAL.storageClass = "REDUCED_REDUNDANCY"; 
      LOCAL.keyName = ""; 
      LOCAL.baseUrl = "http://www.baseurl.com"; 
      LOCAL.imageSrc = ""; 
      LOCAL.testFilePath = LOCAL.imgPath & LOCAL.imgFile; 
      LOCAL.fileExt = ListLast(LOCAL.testFilePath, "."); 
      LOCAL.runner = "s,m,l,xl"; 
      LOCAL.worked = "true"; 
     </cfscript> 
     // runner is the file size setter, in results I only create two sizes, during imports I create all four 
     <cftry> 
     <cfhttp timeout="45" 
      throwonerror="no" 
      url="#LOCAL.testFilePath#" 
      method="get" 
      useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" 
      getasbinary="yes" 
      result="objGet"> 
     <cfscript> 
      // set file sizes to create 
      if (LOCAL.command EQ "upload_search"){ 
       if (LOCAL.cookie LT 320) { 
        LOCAL.runner = "l,s"; 
        } 
       else if (LOCAL.cookie GTE 320 AND LOCAL.cookie LTE 767) { 
        LOCAL.runner = "l,m"; 
        } 
       else if (LOCAL.cookie GT 768) { 
        LOCAL.runner = "xl,m"; 
        } 
       else if (LOCAL.cookie GT 1280){ 
        LOCAL.runner = "xl,l"; 
        } 
       } else if (LOCAL.command EQ "upload_import") { 
        LOCAL.runner = "xl,l,m,s"; 
        } 
      // validate 
      if (len(objGet.Filecontent) EQ 0 OR objGet.Mimetype EQ "text/html" ){ 
       LOCAL.worked = "false length or mime"; 
       } else if (NOT listfindnocase(LOCAL.allow, LOCAL.fileExt)){ 
       LOCAL.worked = "false wrong extension"; 
       } else { 
       // create temp 
       LOCAL.objImage = ImageNew(objGet.FileContent); 
       LOCAL.basePath = LOCAL.tempDirectory & "_base_" & LOCAL.imgFile; 
       imageWrite(LOCAL.objImage, LOCAL.basePath, ".99"); 
       LOCAL.base = imageRead(LOCAL.basePath); 
       LOCAL.imageSrc = LOCAL.tempDirectory; 

       // formats 
       // S = 100x127, 
       // M = 180x230, 
       // L = 290x370, 
       // XL = 870x1110 

       // portrait 
       if (ImageGetWidth(LOCAL.base) LT ImageGetHeight(LOCAL.base)){ 
        for (LOCAL.i = 1; LOCAL.i LTE ListLen(LOCAL.runner,","); LOCAL.i = LOCAL.i+1){ 
         LOCAL.lt = ListGetAt(LOCAL.runner, LOCAL.i, ","); 
         LOCAL.base = imageRead(LOCAL.basePath); 
         ImageSetAntialiasing(LOCAL.base,"on"); 
         // default image width/height 
         LOCAL.height = Application.strConfig.respH[LOCAL.lt]; 
         LOCAL.width = ""; 
         ImageScaleToFit(LOCAL.base, LOCAL.width, LOCAL.height, "highestQuality"); 

         LOCAL.filekey = LOCAL.lt & "_" & LOCAL.imgFile; 
         LOCAL.keyName = LOCAL.sellerILN & "/" & LOCAL.filekey; 
         LOCAL.filename = LOCAL.tempDirectory & LOCAL.filekey; 
         imageWrite(LOCAL.base, LOCAL.filename, ".99"); 

         // s3 
         Application.strObjs.s3.putObject(LOCAL.bucketName, LOCAL.filekey, LOCAL.contentType, LOCAL.httptimeout, LOCAL.cacheControl, LOCAL.cacheDays, LOCAL.acl, LOCAL.storageClass, LOCAL.keyName, LOCAL.imageSrc, "false"); 
         fileDelete(LOCAL.tempDirectory & LOCAL.lt & "_" & LOCAL.imgFile); 
         } 
        } else { 
        // same for landscape 
        ... 
        } 
       } 
       // cleanup 
       fileDelete(LOCAL.tempDirectory & "_base_" & LOCAL.imgFile); 
      } 
     </cfscript> 
    // update media log 
     ... 
     <cfcatch> 
     // dump errror message 
     </cfcatch> 
     </cftry> 

    // return image 
     <cfif LOCAL.access EQ "rm"> 
      <cftry> 
      <cfscript> 
      if (LOCAL.getBack EQ "s"){ 
       LOCAL.passPath = Session.bucketPath & Session.bucketName & "/" & LOCAL.sellerILN & "/" & ListGetAt(LOCAL.runner, Listlen(LOCAL.runner), ",") & "_" & LOCAL.imgFile; 
      } else if(LOCAL.getBack EQ "l") { 
       LOCAL.passPath = Session.bucketPath & Session.bucketName & "/" & LOCAL.sellerILN & "/" & ListGetAt(LOCAL.runner, 1, ",") & "_" & LOCAL.imgFile; 
      } 
      LOCAL.mime = "image/" & LOCAL.fileExt; 
      </cfscript> 

      <cfcontent type="#LOCAL.mime#" file="#LOCAL.passPath#" /> 
      <cfcatch> 
      // dump errors 
      </cfcatch>        
     </cftry> 
    <cfelse> 
     <cfreturn LOCAL.worked> 
    </cfif> 
    </cffunction> 
</cfcomponent> 

所以我我要创建的文件大小设置LOCAL.runner并查看此列表,创建将基础图像调整为我的预设图像尺寸并将创建的图像存储在s3上。

问题cfcontent正试图在创建图像之前返回图像。我在运行上面的代码时遇到了The file specified in contentTag does not exist错误。如果我检查S3,图像就在那里,所以我认为这是一个计时问题。

感谢任何关于如何强制'cfcontent'等待图像创建的指针!

回答

1

如何输出链接到本地​​CFM将加载img?我的意思是 - 对于服务器上存在的图像,输出指向它的<img>标记。对于图像,你需要加载,你的输出是这样的:

<img src="imagegetter.cfm?p=#urlofimagetoload#"> 

imagegetter.cfm将负责执行HTTP来获取和局部调整图像大小。然后它可以通过cfcontent提供这些位。

+0

好的。发现[this] :-)还有一个问题:我使用上面的[photoswipe](http://www.webdevelopernews.com/serving-up-cfimages-via-image-tags) .photoswipe.com /),它需要设置一个URL,如果photoswipe被激活,图像将被加载。如果我通过'imagegetter.cfm'运行丢失的图像,当我发回图像时,我也会有一个有效的路径。任何想法如何用新路径替换'imageGetter.cfm?...'url?如果我离开路径,photoswipe将在激活时重新请求imagegetter.cfm。 – frequent

+1

如果imagegetter.cfm是'智能'(即它知道它何时已经下载/调整大小),那么只使用*这个URL并不重要。对? –

+0

坐在上面,让它变得聪明:-)。另外,由于我使用自适应尺寸(= s,m,l,xl),因此我正在使用imagegetter显示小型版本,并向photos版本输出常规链接到大版本,应在用户触发时完成photoswipe。仍在测试中,一旦我有所有工作,我将发布解决方案。再次感谢迄今! – frequent