2

これは私が作っているプロジェクトへのリンクです: http://1-to-n.com/ik/

何でも入力して送信を押すだけで、次のページに移動して画像にテキストが表示されます。すべてがうまく機能していますが、画像を右クリックして保存しようとすると、拡張子が「picture.php.jpe」のように変です。

画像生成ページのコードは次のとおりです。

<?php
//Set the Content Type
header('Content-type: image/jpeg');

// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('vote.jpg');

// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 47, 45, 46);

// Set Path to Font File
$font_path = 'MISTRAL.TTF';

// Set Text to Be Printed On Image
$text = $_POST['name'];

// Print Text On Image
imagettftext($jpg_image, 75, 0, 500, 130, $white, $font_path, $text);

// Send Image to Browser
imagejpeg($jpg_image);

// Clear Memory
imagedestroy($jpg_image);
?> 
4

1 に答える 1

4

ここでの解決策は ですがContent-Disposition、まさに必要なものは、UI をどのように機能させたいかによって異なります。

設定する内容:

header('Content-Disposition: %token%; filename="%name_of_file%"');

どこ:

  • %name_of_file%ユーザーに表示するファイルの名前です

  • %token%またはのattachmentいずれかinlineです。に設定するとattachment、ユーザーはすぐにファイルをダウンロードするように求められます。ブラウザは現在のように画像を表示しません。を設定inlineすると、ブラウザ ペインに現在の画像が表示されます。画像を保存するには、右クリック -> 名前を付けて保存する必要があります。

使用するヘッダーを決定する前に、UI をどのように機能させるかを決定する必要があります。

わかりやすくするために、具体的な例をいくつか示します。

// Prompt the user to save the file immediately
header('Content-Disposition: attachment; filename="file.jpg"');

// OR

// Display the image to the user and ask them to manually save it
header('Content-Disposition: inline; filename="file.jpg"');

注目に値するのは、IE の古いバージョンである IIRC にinlineは、提案されたファイル名に注意を払わないという問題があったことです。ただし、これは、name=""トークンを に追加することで回避できますContent-Type:。これは標準違反ですが、かなり無害です。

于 2013-04-19T15:30:04.547 に答える