1

我正在跑步Coldfusion8并且有一个显示产品图片的页面。图像可以预先存储在Amazon S3(首选)上,或者我需要从远程源(由所有者指定)加载它们,调整大小,存储在 S3 上并从那里显示。

虽然这在“理论上”有效(图像被拉入、转换和存储),但我在显示新创建的图像时遇到了问题,这些图像在第一次加载时永远不会起作用。刷新页面后,图像加载正常。

这就是我正在做的事情:

  1. 在我的 display_image.cfc 中,它构造了 HTML 标记:

     <!--- check media log for availablity through S3 --->
     <cfif results.typ NEQ "img" AND results.typ NEQ "alt">
        <!--- call cfc to pull in and store image to s3 --->
        <cfset variables.imageSrc = "http://www.page.de/test/services/img_handler.cfc" & "?method=upload&cm=upload_search&pt=" & results.bildpfad & "&fl=" & results.bilddateiname & "&se=" & results.iln & "&ar=" & results.artikelnummer & "&ck=" & variables.screenWidth & "&ax=rm&gb=s">
     <cfelse>
         <cfset variables.imgFail = "true">
     </cfif>
    
  2. 传递的变量

    method = the method handling uploads  
    cm = routing inside method  
    pt = remote image path  
    fl = remote file name   
    se = seller id  
    ar = item corresponding to this image  
    ck = screen width (to load responsive image size)  
    ax = remote access trigger  
    gb = return images size  
    
  3. 在我的 image_handler.cfc 里面

    <!--- set variables --->
    <cfscript>  
        var LOCAL = {};                 
            LOCAL.command = cm;
            LOCAL.imgPath = pt;
            LOCAL.imgFile = fl;
            LOCAL.sellerILN = se;
            LOCAL.article = ar;
            LOCAL.cookie = LSParseNumber(ck);
            LOCAL.getBack = gb;
            LOCAL.access = ax;
            // routing
            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.page.com";
            LOCAL.imageSrc = "";
            LOCAL.testFilePath = LOCAL.imgPath & LOCAL.imgFile;
            LOCAL.fileExt = ListLast(LOCAL.testFilePath, ".");
            // image size indicator
            LOCAL.runner = "s,m,l,xl";
            LOCAL.worked = "true";
        </cfscript> 
    
        <cftry>
            <!--- get image --->
            <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">
    
    <!--- determine which image size to create (depending on screen width) --->
            <cfif LOCAL.cookie LT 320 >
                <cfset LOCAL.runner = "l,s">
            <cfelseif LOCAL.cookie GTE 320 AND LOCAL.cookie LTE 767>
                <cfset LOCAL.runner = "l,m">
            <cfelseif LOCAL.cookie GT 768 AND LOCAL.cookie LTE 1279>
                <cfset LOCAL.runner = "xl,m">
            <cfelseif LOCAL.cookie GT 1280>
                <cfset LOCAL.runner = "xl,l">
            </cfif>
    
            <!--- validate --->
            <cfif len(objGet.Filecontent) EQ 0 OR objGet.Mimetype EQ "text/html">
                <cfset LOCAL.worked = LOCAL.worked& "false length or mime">
            <cfelseif NOT listfindnocase(LOCAL.allow, LOCAL.fileExt )>
                 <cfset LOCAL.worked = LOCAL.worked& "false wrong extension">
            <cfelse>
                 <!--- create temp image and resize --->
                 <cfset LOCAL.objImage = ImageNew(objGet.FileContent)>
                 <cfset LOCAL.basePath = LOCAL.tempDirectory & "_base_" & LOCAL.imgFile>
                 <cfset imageWrite( LOCAL.objImage, LOCAL.basePath, ".99")>
                 <cfset LOCAL.base = imageRead( LOCAL.basePath )>
                 <cfset LOCAL.imageSrc = LOCAL.tempDirectory>
                 <cfset ImageSetAntialiasing(LOCAL.base,"on")>
                 <cfset variables.loopCount = 0>
    
                 <!--- portrait --->
                 <cfif ImageGetWidth( LOCAL.base ) LT ImageGetHeight( LOCAL.base )>
                      <!--- loop through image size, like "xl,m" --->
                      <cfloop list="#LOCAL.runner#" delimiters="," index="letter"><cfset variables.loopCount = variables.loopCount+1>
                          <cfset LOCAL.lt = ListGetAt(LOCAL.runner, variables.loopCount, ",")>
                          <cfset LOCAL.height = Application.strConfig.respH[LOCAL.lt]>
                          <cfset LOCAL.width = "">
                          <cfset ImageScaleToFit(LOCAL.base, LOCAL.width, LOCAL.height, "bilinear")>
                          <cfset LOCAL.filekey = LOCAL.lt & "_" & LOCAL.imgFile>
                          <cfset LOCAL.keyName = LOCAL.sellerILN & "/" & LOCAL.filekey>
                          <cfset LOCAL.filename = LOCAL.tempDirectory & LOCAL.filekey>
                          <cfset imageWrite( LOCAL.base, LOCAL.filename, ".99" )>
    
                          <!--- call s3 --->
                         <cfset 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" )>
                         <cfset fileDelete( LOCAL.tempDirectory & LOCAL.lt & "_" & LOCAL.imgFile )>
                     </cfloop>
                     <!--- cleanup --->
                     <cfset fileDelete( LOCAL.tempDirectory & "_base_" & LOCAL.imgFile )>
               </cfif>
               <!--- pass back image THIS DOESN'T WORK --->
    
               <cfscript>
               // set image path and mime type
                   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;
                   }
               if ( LOCAL.fileExt EQ "jpg" ){
                       LOCAL.fileExt = "jpeg";
                   }
               LOCAL.mime = "image/" & LOCAL.fileExt;
               </cfscript>
               <!--- pass back as content --->
               <cfcontent reset="no" type="#LOCAL.mime#" file="#LOCAL.passPath#" />
           <cfreturn />
    

就像我说的,这运行正常,拉入、验证和存储图像,创建的较小的图像作为cfcontent元素传回。但是,我无法让它工作=创建并存储了图像,但是无论我传回什么,图像都不会显示,直到我刷新页面并触发新的搜索。

问题:
我做错了iamge_handler什么吗?我认为这可能与我的使用有关,cfcontent但我不确定。

4

0 回答 0