PHP如何保存在api.qrserver.com/v1/create-qr-code中生成的二维码

发布于 2025-01-15 09:00:20 字数 814 浏览 4 评论 0原文

我已经在我的 php 脚本中生成了一个 qrcode,输出标签如下所示:

<img src="http://api.qrserver.com/v1/create-qr-code/?data=http://example.com/url321&amp;size=256x256&amp;color=000000&amp;bgcolor=ffffff&amp;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.'&amp;size='.$size.'&amp;color='.$color.'&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听你说爱我 2025-01-22 09:00:20

您实际上并没有获取图像。您只需将文字 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);

注意:为了能够使用 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:

$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);

Note: To be able to fetch URL's using file_get_contents(), you need to make sure that you have allow_url_fopen enabled. Most installs I've used does already have it enabled as default.
You can read more about it in the manual

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文