Zend_pdf, display a url (qrcode)

二次信任 提交于 2019-12-13 04:15:45

问题


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

Thanks


回答1:


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

And then you can use $imgPath to create the PDF image.



来源:https://stackoverflow.com/questions/16031076/zend-pdf-display-a-url-qrcode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!