为什么我不能在强制下载后查看图像?

时间:2016-01-22 08:18:05

标签: php image download web qr-code

首先,我正在尝试生成 QR码,一旦生成,图像将立即下载到计算机上。如果有人可以告诉我为什么图像没有出现,我将不胜感激。请注意我仍然是编码的新手。非常感谢您的帮助。以下是我的代码:

generate.php

<?php
if(isset($_POST['type']))
{
//here we got all data from submitted form
//we include class file
include("qrcode.php");
//create an instance
$qr = new qrcode();
//then we check what type of information user wanted to create qr code
//to know what are possible types and what information needs to inserted, check the example file
switch($_POST['type'])
{
    case "url":
        //then we use submitted information here, word inside $_POST[] brackets must match value of name attribute in the input field
        //<p>http://<input type='text' name='url' 
        $qr->link($_POST['url']);
    break;
    case "txt":
        $qr->text($_POST['txt']);
    break;
    case "sms":
        $qr->sms($_POST['sms_phone'], $_POST["sms_text"]);
    break;
    case "bookmark":
        $qr->bookmark($_POST['mms_phone'], $_POST["mms_text"]);
    break;
    case "tel":
        $qr->phone_number($_POST['tel']);
    break;
    case "contactinfo":
        $qr->contact_info($_POST["contact_name"], $_POST["contact_address"], $_POST["contact_phone"], $_POST["contact_email"]);
    break;
    case "email":
        $qr->email($_POST["email_address"], $_POST["email_subject"], $_POST["email_txt"]);
    break;
    case "geo":
        $qr->geo($_POST["geo_lat"], $_POST["geo_lon"], $_POST["geo_above"]);
    break;
    case "wifi":
        $qr->wifi($_POST["wifi_aut"], $_POST["wifi_ssid"],   $_POST["wifi_pass"]);
    break;
}
//here we specify inputted size and get link to image
echo "<p><img src='".$qr->get_link($_POST['size'])."' border='0'/></p>";

//to download image
//echo "<p><img src'".$->download_image($_POST['size'])."' border='0'/></p>"
$download =  $qr->get_link();
$qr->download_image($download);
}
?>

qrcode.php

<?php  

 class qrcode  
 {  
private $data;  

//creating code with link mtadata  
public function link($url){  
    if (preg_match('/^http:\/\//', $url) || preg_match('/^https:\/\//', $url))   
    {  
        $this->data = $url;  
    }  
    else  
    {  
        $this->data = "http://".$url;  
    }  
}  

//creating code with bookmark metadata  
public function bookmark($title, $url){  
    $this->data = "MEBKM:TITLE:".$title.";URL:".$url.";;";  
}  

//creating text qr code  
public function text($text){  
    $this->data = $text;  
}  

//creatng code with sms metadata  
public function sms($phone, $text){  
    $this->data = "SMSTO:".$phone.":".$text;  
}  

//creating code with phone   
public function phone_number($phone){  
    $this->data = "TEL:".$phone;  
}  

//creating code with mecard metadata  
public function contact_info($name, $address, $phone, $email){  
    $this->data = "MECARD:N:".$name.";ADR:".$address.";TEL:".$phone.";EMAIL:".$email.";;";  
}  

//creating code wth email metadata  
public function email($email, $subject, $message){  
    $this->data = "MATMSG:TO:".$email.";SUB:".$subject.";BODY:".$message.";;";  
}  

//creating code with geo location metadata  
public function geo($lat, $lon, $height){  
    $this->data = "GEO:".$lat.",".$lon.",".$height;  
}  

//creating code with wifi configuration metadata  
public function wifi($type, $ssid, $pass){  
    $this->data = "WIFI:T:".$type.";S:".$ssid.";P:".$pass.";;";  
}  

//creating code with i-appli activating meta data  
public function iappli($adf, $cmd, $param){  
    $param_str = "";  
    foreach($param as $val)  
    {  
        $param_str .= "PARAM:".$val["name"].",".$val["value"].";";  
    }  
    $this->data = "LAPL:ADFURL:".$adf.";CMD:".$cmd.";".$param_str.";";  
}  

//creating code with gif or jpg image, or smf or MFi of ToruCa files as content  
public function content($type, $size, $content){  
    $this->data = "CNTS:TYPE:".$type.";LNG:".$size.";BODY:".$content.";;";  
}  

//getting image  
public function get_image($size = 150, $EC_level = 'L', $margin = '0'){  
    $ch = curl_init();  
    $this->data = urlencode($this->data);   
    curl_setopt($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart');  
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($ch, CURLOPT_HEADER, false);  
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);  

    $response = curl_exec($ch);  
    curl_close($ch);  
    return $response;  
}  

//getting link for image  
public function get_link($size = 250, $EC_level = 'L', $margin = '0'){  
    $this->data = urlencode($this->data);   
    $image =  'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data;                                                               
    return $image; 
}  

//forcing image download 
public function download_image($file){ 
    header("Content-Description: File Transfer");
    header("Cache-Control: public");
    header('Content-Type: application/octet-stream');
    header('Expires: 0');
    header("Content-Disposition: attachment; filename=QRcode.png");     
    header("Content-Transfer-Encoding: binary");    
    header('Pragma: public');

    echo $file;


} 


//save image to server
public function save_image($file, $path = "./QRcode.png"){ 
    file_put_contents($path, $file);
} 
}  
?>

下载图像时,会显示一条错误消息,指出该文件不受支持

enter image description here

在文本编辑器上打开图像但仍然找不到错误

<p><img src='http://chart.apis.google.com/chart?chs=250x250&cht=qr&chld=L|0&chl=sdfds' border='0'/></p>http://chart.apis.google.com/chart?chs=250x250&cht=qr&chld=L|0&chl=sdfds<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>QR-codes</title>
<script type="text/javascript">

function change_type(id)
{
    var divs = document.getElementById('forms').getElementsByTagName('div');
    for(var i = 0; i < divs.length; i++)
    {
        divs[i].style.display = "none";
    }
    if(document.getElementById(id))
    {
        document.getElementById(id).style.display = "block";
    }
}
</script>
</head>
<body>
<form action='' method='post'>
<!-- Here is a select option element to change divs -->
<p>Select type: <select name='type' onchange='change_type(this.value)'>
<option value="url" >Link</option>
<option value="txt" selected>Text</option>
<option value="sms" >SMS</option>
<option value="bookmark" >Bookmark</option>
<option value="tel" >Phone number</option>
<option value="contactinfo" >Contact information</option>
<option value="email" >Email</option>
<option value="geo" >Geographical information</option>
<option value="wifi" >Wifi Network config</option>
</select></p>
<p>Image size in pixels: <input type='text' size='5' name='size' value='250'/></p>
<!-- This div element contains all form div element -->
<div id='forms'>
<!-- This div is for link -->
<div id='url'>
<p>Link:</p>
<p>http://<input type='text' name='url' value=''/></p>
</div>
<!-- This div is for text, style='display:none;' means it won't be visible from start -->
<div id='txt' style='display:none;'>
<p>Text:</p>
<p><textarea name='txt' rows='10' cols='30'>sdfds</textarea></p>
</div>
<!-- This div is for sms -->
<div id='sms' style='display:none;'>
<p>Phone:</p>
<p><input type='text' name='sms_phone' value=''/></p>
<p>Text:</p>
<p><textarea name='sms_text' rows='10' cols='30'></textarea></p>
</div>
<div id='bookmark' style='display:none;'>
<p>Title:</p>
<p><input type='text' name='bookmark_title' value=''/></p>
<p>url:</p>
<p><input name='bookmark_url' value=''/></p>
</div>
<div id='tel' style='display:none;'>
<p>Phone nubmer:</p>
<p><input type='text' name='tel' value=''/></p>
</div>
<div id='contactinfo' style='display:none;'>
<p>Contact information</p>
<p>Name:</p>
<p><input type='text' name='contact_name' value=''/></p>
<p>Address</p>
<p><textarea name='contact_address' rows='10' cols='30'>
</textarea></p>
<p>Phone number:</p>
<p><input type='text' name='contact_tel' value=''/></p>
<p>Email address:</p>
<p><input type='text' name='contact_email' value=''/></p>
</div>
<div id='email' style='display:none;'>
<p>Email(supported by latest phones)</p>
<p>Reciever:</p>
<p><input type='text' name='email_address' value=''/></p>
<p>Subject:</p>
<p><input type='text' name='email_subject' value=''/></p>
<p>Message:</p>
<p><textarea name='email_txt' rows='10' cols='30'>
</textarea></p>
</div>
<div id='geo' style='display:none;'>
<p>Geographical information(supported by latest phones)</p>
<p>Latitude:</p>
<p><input type='text' name='geo_lat' value=''/></p>
<p>Longitude:</p>
<p><input type='text' name='geo_lon' value=''/></p>
<p>Meters above earth:</p>
<p><input type='text' name='geo_above' value=''/></p>
</div>
<div id='wifi' style='display:none;'>
<p>Wifi Network configuration(supported by Android devices)</p>
<p>Authentication type:</p>
<p><select name='wifi_aut'>
<option value='WEP'>WEP</option>
<option value='WAP'>WAP</option>
</select></p>
<p>Network SSID:</p>
<p><input type='text' name='wifi_ssid' value=''/></p>
<p>Password:</p>
<p><input type='text' name='wifi_pass' value=''/></p>
</div>
</div>
<p><input type='submit' value='Get code'/></p>
<p><input type='submit' value='Download'/></p>

</form>
<script type="text/javascript">
    window.onload = function () 
    { 
        //this piece of code is so value selected by user will be shown after form submission
        change_type('txt'); }
</script>
</body>
</html> 

0 个答案:

没有答案