我已經在我的 php 腳本中生成了一個二維碼,輸出標簽是這樣的:
<img src="http://api.qrserver.com/v1/create-qr-code/?data=http://example.com/url321&size=256x256&color=000000&bgcolor=ffffff&margin=1px" alt="Scan QR-Code" title="Scan QR-Code" width="256px" height="256px">
現在,我想將這個生成的影像保存在我的主機上 我怎樣才能用 PHP 做到這一點?
我試過這段代碼但沒有成功:
//This line generates qr code:
$qrsrc = 'http://api.qrserver.com/v1/create-qr-code/?data='.$url.'&size='.$size.'&color='.$color.'&bgcolor='.$bgcolor;
//And the following lines try to store it:
$image_name = 'test.png';
$save_path = 'files/qrcode/'.$image_name;
$image_file = fopen($save_path, 'wb');
fwrite($image_file, $qrsrc);
fclose($image_file);
此代碼創建影像檔案。但影像不是正確的檔案...謝謝。
uj5u.com熱心網友回復:
您實際上并沒有獲取影像。您只是將文字 URL 作為文本存盤在test.png檔案中。
首先獲取影像:
$qrsrc = 'http://api.qrserver.com/v1/create-qr-code/?data='.$url.'&size='.$size.'&color='.$color.'&bgcolor='.$bgcolor;
// Get the image and store it in a variable
$image = file_get_contents($qpsrc);
// Save the file
$image_name = 'test.png';
$save_path = 'files/qrcode/'.$image_name;
// Save the image
file_put_contents($save_path, $image);
注意:為了能夠獲取 URL 的 using file_get_contents(),您需要確保已allow_url_fopen啟用。我使用的大多數安裝都已經默認啟用它。您可以在手冊
中閱讀有關它的更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/450089.html
