2013-04-16 35 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