11

http://sourceforge.net/projects/phpqrcode/は素晴らしいライブラリですが、png 画像を文字列として返す方法が見つかりません。基本的な例は次のとおりです。

QRcode::png('code data text', 'filename.png'); // creates file 
QRcode::png('some othertext 1234'); // creates code image and outputs it directly into browser

私はドキュメントをチェックしましたが、何もありませんでした。:B

4

4 に答える 4

26
ob_start();
QRCode::png('text', null);
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();
于 2012-07-05T20:51:51.607 に答える
7
$qrTempDir = 'path/to/your/temp';
$filePath = $qrTempDir.'/'.uniqid();

QRcode::png('some text', $filePath);
$qrImage = file_get_contents($filePath);

unlink($filePath);

これはあなたが探しているものでなければなりません。次のような画像を表示するように拡張できます。

<img src="data:image/png;base64,<?php echo base64_encode($qrImage) ?>" />

残念ながら、ライブラリは現時点で他のメソッドをサポートしていません. file パラメーターなしで QRcode::png 関数を呼び出すと、それらのヘッダーが送信されるだけでなく、コードの実行も終了するため、.ヘッダー。

于 2014-05-22T12:10:58.630 に答える
2

@ iim.hlk と同じ問題に遭遇しました

これは私が@Lusitanianのこれに対する彼の答えを少し修正したものです

ob_start();
QRCode::png($string);
$imageString = base64_encode( ob_get_clean() );
header('Content-Type: text/html');

これにより、単に上書きするだけでヘッダーの問題が修正されます。きれいでも何でもありませんが、目的のために機能します。

于 2014-07-30T15:10:36.293 に答える
2

これは私のために働く

include '../phpqrcode/qrlib.php';
$content = "any content";
ob_start();
QRcode::png($content);
$result_qr_content_in_png = ob_get_contents();
ob_end_clean();
// PHPQRCode change the content-type into image/png... we change it again into html
header("Content-type: text/html");
$result_qr_content_in_base64 =  base64_encode($result_qr_content_in_png);

次に、htmlファイルで

<img src="data:image/jpeg;base64,$result_qr_content_in_base64'"/>
于 2018-10-04T14:14:58.867 に答える