2013-04-16 39 views
0

我想要显示一个URL的qrcode。我尝试这个但是dind't工作,我想我的代码不保存在我的电脑上的网址,他失败,他就去尝试打开QR码Zend_pdf,显示一个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); 

感谢

+0

请在描述问题时更具体,以增加获得帮助的机会。单纯的“没有工作”不是很具描述性。 – Havelock

+0

你是否检查过你的'temp_dir'来查看它是否有内容? – Havelock

回答

0

你的问题与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图像。

+1

谢谢哈夫洛克 – Jeremy