Zend_pdf,显示网址(qrcode) [英] Zend_pdf, display a url (qrcode)
问题描述
我想显示URL中的qrcode.我尝试了一下,但是没有用,我认为我的代码无法将网址保存在我的计算机上,但是他失败了,他尝试打开qrcode
I want want to display a qrcode from a url. I try this but that dind't work, I think my code doesn't save the url on my computer and he fail went he try to open the qrcode
$imageUrl = 'https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=toto';
$imagePath = sys_get_temp_dir() . '\\' . basename($imageUrl);
file_put_contents($imagePath, file_get_contents($imageUrl));
$image = Zend_Pdf_Image::imageWithPath($imagePath);
unlink($imagePath);
$page = $this->newPage($settings);
$page->drawImage($image, 0, 842 - 153, 244, 842);
谢谢
推荐答案
您遇到的问题是URL的basename
,您正在尝试将其设置为文件名,结果类似于
同样,您不能使用file_get_contents
下载"图像.您需要使用 cURL
.像这样的事情应该可以完成这项工作:
The problem you're having is with the basename
of the URL, you're trying to set as file name, which results in something like C:\TEMP\chart?chs=150x150&cht=qr&chl=toto
, which isn't a valid file name.
Also you can't "download" the image using file_get_contents
. You'll need to use cURL
. Something like this should do the job:
$imageUrl = 'https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=toto';
$imgPath = sys_get_temp_dir() . '/' . 'qr.png';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$raw = curl_exec($ch);
if (is_file($imgPath)) {
unlink($imgPath);
}
$fp = fopen($imgPath, 'x');
fwrite($fp, $raw);
fclose($fp);
然后您可以使用$imgPath
创建PDF图像.
And then you can use $imgPath
to create the PDF image.
这篇关于Zend_pdf,显示网址(qrcode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!