Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top