【问题标题】:Zend_pdf, display a url (qrcode)Zend_pdf,显示一个url(qrcode)
【发布时间】:2013-04-16 07:23:12
【问题描述】:

我想显示来自 url 的二维码。我尝试了这个,但那不起作用,我认为我的代码没有将 url 保存在我的计算机上,他失败了,他尝试打开 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);

谢谢

【问题讨论】:

  • 请在描述问题时更加具体,以增加获得帮助的机会。仅仅didn't work 的描述性不是很好。
  • 你检查过你的temp_dir 看看里面有没有什么东西吗?

标签: php pdf zend-pdf


【解决方案1】:

您遇到的问题是 URL 的 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 图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多