PHP如何保存在api.qrserver.com/v1/create-qr-code中生成的二维码
我已经在我的 php 脚本中生成了一个 qrcode,输出标签如下所示:
<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);
此代码创建图像文件。但图像不是正确的文件... 谢谢。
I have been generated a qrcode in my php script and the output tag is something like this:
<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">
Now, I want to save this generated image on my host
How can I do this with PHP?
I tried this code but no success:
//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);
This code creates the image file. but image is not correct file...
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上并没有获取图像。您只需将文字 URL 作为文本存储在
test.png
文件中。首先获取图像:
注意:为了能够使用
file_get_contents()
获取 URL,您需要确保已启用allow_url_fopen
。我使用过的大多数安装都已默认启用它。您可以在手册中阅读更多相关信息
You're not actually fetching the image. You're just storing the literal URL as text in your
test.png
file.First fetch the image:
Note: To be able to fetch URL's using
file_get_contents()
, you need to make sure that you haveallow_url_fopen
enabled. Most installs I've used does already have it enabled as default.You can read more about it in the manual