我想要从网址显示qrcode。我试试这个但是没办法,我认为我的代码没有保存在我的电脑上的网址而且他失败去了他试图打开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);
由于
答案 0 :(得分:0)
您遇到的问题是网址的basename
,您尝试将其设置为文件名,这会产生类似C:\TEMP\chart?chs=150x150&cht=qr&chl=toto
的内容,这不是有效的文件名。
此外,您无法使用file_get_contents
“下载”图像。您需要使用cURL
。这样的事情应该可以胜任:
$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图像。