7

Coldfusion 9 サーバー上に、その場で画像バナーを作成するサービスがあります。別のマシンは、次のような方法でこれらのファイルを保存する必要があります。

wget http://myserver.com/services/local/bannerCreator/250x250-v3.cfm?prodID=3&percentSaving=19

問題は、一時ファイルを使用せずに、coldfusion でバイナリ データを書き出す方法が思いつかないことです。その瞬間、画像は次のような画像タグとして表示されます。

<cfimage action = "writeToBrowser" source="#banner#" width="#banner.width#" height="#banner.height#" />

何か案は?それとも、一時ファイルを使用する必要がありますか?

4

5 に答える 5

13

画像がどのように生成されるかのサンプルコードを提供していないため、テストできませんが、この行に沿って何かを試しましたか?

<cfcontent reset="true" variable="#imageData#" type="image/jpg" />

更新: そこで、先に進んで独自のイメージを作成しました。私はあなたが似たようなことをしていると仮定します。これは私にとって完璧に機能します:

<cfset img = imageNew("",200,200,"rgb","red") />
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />

これは、ファイルへの書き込みや、仮想ファイル システム (「ramdisk」) を使用せずに機能します。

于 2011-04-12T14:57:51.733 に答える
6

ファイル/画像のバイナリバイトがある場合は、次のように出力バッファをその内容に置き換えることができます。

<cfscript>
// eg. this is how you get a file as a binary stream
// var fileBytes = fileReadBinary( '/path/to/your/file.jpg' );

// get the http response
var response = getPageContext().getFusionContext().getResponse();

// set the appropriate mime type
response.setHeader( 'Content-Type', 'image/jpg' );

// replace the output stream contents with the binary
response.getOutputStream().writeThrough( fileBytes );

// leave immediately to ensure no whitespace is added
abort;
</cfscript>

使用するとほとんど何<cfcontent>が起こりますかreset="true"

この方法の利点は<cfcontent>、cfscriptベースのcfcs内に記述できることです。

于 2011-12-15T17:41:58.197 に答える
4

上記の解決策を見つけました

<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />

私にとってはうまくいきません。

type="image/png" の設定は、応答の MIME タイプを設定するだけです。必ずしも画像をPNGとしてエンコードしているとは思いません。そのため、透明なpng(画像タイプ「argb」)を生成すると、<cfimage action = "writeToBrowser"...>方法と比較して奇妙な色が得られました。

どういうわけか、画像データを PNG として明示的にエンコードし、バイナリ データを直接出力する必要があると考えました。

基礎となるJavaを掘り下げて、これを思いつきました。これは、これまでのところうまくいくようです。

この例では、黒い円で透明な png を描画します。

<!--- create the image and draw it --->
<cfset img = ImageNew("", 23, 23, "argb")>
<cfset ImageSetDrawingColor(img, "black")>
<cfset ImageDrawOval(img, 0, 0, 21, 21, true)>

<!--- get the response object --->
<cfset response = getPageContext().getFusionContext().getResponse()>
<!--- set the response mime type --->
<cfset response.setHeader('Content-Type', 'image/png')>
<!--- get the underlying image data --->
<cfset bImage = ImageGetBufferedImage(img)>
<!--- get the magical object to do the png encoding --->
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<!--- encode the image data as png and write it directly to the response stream --->
<cfset ImageIO.write(bImage, "png", response.getResponse().getOutputStream())>

それが誰かを助けることを願っています!

于 2012-05-29T09:43:46.057 に答える
3

高さと幅の属性を取り出し、フォーマット属性を追加します。

<cfimage action = "writeToBrowser" source="#banner#" format="png" />

wget は、CF が CFFileServlet フォルダーに作成する物理ファイルへのリダイレクトを尊重する必要がありますが、そうでない場合は、それを作成するように設定できるフラグがあります--max-redirect=10

そして、あなたが示唆するように、一時ファイルも機能します。ファイルを書き込んで、cfheader と cfcontent を使用して書き出すだけです。一時ファイル名をより一意にするようにしてください。

<cfimage action="write" destination="tempfile.png" source="#banner#" format="png" />
<cfheader name="content-disposition" value="attachment; filename=banner.png" />
<cfcontent file="tempfile.png" deletefile="true" type="image/png" />
于 2011-04-12T15:01:02.307 に答える
0

私も同様のことをしていますが、タグと cfscript を組み合わせて使用​​する必要があります。必要な画像機能があります。画像を変数としてメモリに格納すると、多くの画像関数を使用できます。CFFILE や CFHTTP など、さまざまな方法で画像をメモリに取り込むことができます。

私の場合、CFIMAGE タグを使用して画像ファイルをメモリに読み込み、CFSCRIPT 画像関数で下部にテキストを追加して操作し、結果の画像を .png (または必要に応じて .jpg) としてブラウザーに出力します。 . サーバーに保存される唯一のファイルは、元の画像ファイルです。あなたの場合、画像を読み込む代わりに、既に行っているように cfhttp タグを使用して呼び出します。これが私のコードです:

<!---Get the image for processing ...--->
<cfimage action="read" source="#application.approotABS#\webcam\webcam.jpg" name="CamImage" /> 
<!--- prepare the text for overlay --->
<cfscript>
  attr=structNew();
  attr.font = "Arial";
  attr.size = 15;
  ImageSetDrawingColor(CamImage, "white");
  ImageDrawText(CamImage, 'LIVE FROM STUDIO 1', 18,(ImageGetHeight(CamImage)-54), attr);
  ImageDrawText(CamImage, '#ShowOnNow.showname#', 18,(ImageGetHeight(CamImage)-36), attr);
  ImageDrawText(CamImage, datestring,18,(ImageGetHeight(CamImage)-18), attr);

</cfscript>

<!--- further down the page, output the manipulated image: ---->
 <div class="webcam">  
      <cfimage action="writeToBrowser" source="#Camimage#"  >
  </div>

http://hawkesburyradio.com.au/index.cfm?pid=111538で動作を確認できます。

(Windows Server で CF9 を使用しています)

于 2014-08-06T11:33:47.440 に答える