5

私は単純な php プロキシを書いていますが、png ファイルの表示に問題があります。出力は次のとおりです。

ここに画像の説明を入力

それは次のようになります。

ここに画像の説明を入力

画像は Notepad++ で開きます。私のphp curlコードは次のようになります:

$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
header('Content-Type:' . $info['content_type']);
echo $content

出力は同じで、画像は表示されません。どうすれば画像を表示できますか?

編集:データをファイルに保存し、ロケーションヘッダーを使用してリダイレクトすると、画像が正しく表示されます:

$file = fopen('proxy_tmp~', 'w');
fwrite($file, $content);
fclose($file);
header('Location: ' . DIR . 'proxy_tmp~');

EDIT 2:gzip圧縮がありましたが、それを無効にすると同じ問題が発生します.Notepad ++で両方のファイルを開くと、1つはDOS / Windows ANSI(オリジナル)で、もう1つはDOS / Windows UTF-8(スクリプト)。メモ帳でファイルを開き、エンコーディングを ANSI に変更してファイルを保存すると、すべて問題ありません。

編集 3 : GNU/Linux でも同じことをしたと思いますが、CURLOPT_BINARYTRANSFERオプションなしで問題なく動作しています。ここに私のプロジェクトhttps://github.com/jcubic/yappがあります。Windows 10 で Wamp を使用してテストしたところ、問題なく動作しました。

4

7 に答える 7

1

ダウンロードのためにファイルをユーザーに直接送信する方法は次のとおりです ( $content 変数を使用します)。

$file_array = explode("\n\r", $content, 2);
$header_array = explode("\n", $file_array[0]);
foreach($header_array as $header_value) {
$header_pieces = explode(':', $header_value);
  if(count($header_pieces) == 2) {
    $headers[$header_pieces[0]] = trim($header_pieces[1]);
  }
}
header('Content-type: ' . $headers['Content-Type']);
header('Content-Disposition: ' . $headers['Content-Disposition']);
echo substr($file_array[1], 1);

ここに完全な例があります:

http://ryansechrest.com/2012/07/send-and-receive-binary-files-using-php-and-curl/

于 2013-07-08T07:23:58.720 に答える
0

どのPHPバージョンを使用していますか? PHP バージョン 5.1.3 より上ですか? その場合、CURLOPT_BINARYTRANSFER は効果がありません。

ソース: http://php.net/manual/en/function.curl-setopt.php

ファイルを見て、次のヘッダーをページに追加する必要があります。

header('Content-Type: image/png');
于 2013-07-08T07:39:33.347 に答える
0

私は基本的なヘッダー認証で以下を使用しました..郵便配達員を使用して認証キーを取得しました

// key : > converted to basic auth

// Content type
header('Content-Type: image/jpeg');

$url = 'http://com.com.com/api/images/products/264/7971';
$headers = array(
    'Content-Type: image/png',
    'Authorization: Basic @#$%@#$%@#$%@#$%@#$%@#$%' );

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$file = curl_exec($ch);
curl_close($ch);  
//echo($result);

// create image from output of the screen
$img = @imagecreatefromstring($file);

if ($img === false) {
    echo $file;
    exit;
}

$width = imagesx($img);
$height = imagesy($img);

$newWidth = 300;

$ratio = $newWidth / $width;
$newHeight = $ratio * $height;


$resized = @imagecreatetruecolor($newWidth, $newHeight);

if ($resized === false) {
    echo $file;
    exit;
}

$quality = 90;

if (@imagecopyresampled($resized, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)) {
    @imagejpeg($resized, NULL, $quality);
} else {
    echo $file;
}
于 2015-11-19T05:47:30.047 に答える