2016-01-05 88 views
1

OK,讓我們說我們有一個表格,並在其中一個簡單的文件上傳輸入保存上傳圖片的縮略圖,並以Base64編碼字符串

<form action="index.php" method="post" enctype="multipart/form-data"> 
<input name="image_file" type="file" /> 
<input type="submit" name="submit-btn" value="Upload" /> 
</form> 

然後是index.php文件

// check $_FILES['ImageFile'] not empty 
if(!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])){ 
     die('Image file is Missing!'); // output error when above checks fail. 
} 

//uploaded file info we need to proceed 
$image_name = $_FILES['image_file']['name']; //file name 
$image_size = $_FILES['image_file']['size']; //file size 
$image_temp = $_FILES['image_file']['tmp_name']; //file temp 

$image_size_info = getimagesize($image_temp); //get image size 

if($image_size_info){ 
    $image_width  = $image_size_info[0]; //image width 
    $image_height  = $image_size_info[1]; //image height 
    $image_type   = $image_size_info['mime']; //image type 
}else{ 
    die("Make sure image file is valid!"); 
} 

然後我嘗試重新大小的文件

switch($image_type){ 
    case 'image/png': 
     $image_res = imagecreatefrompng($image_temp);break; 
    case 'image/gif': 
     $image_res = imagecreatefromgif($image_temp); break; 
    case 'image/jpeg': case 'image/pjpeg': 
     $image_res = imagecreatefromjpeg($image_temp); break; 
    default: 
     $image_res = false; 
} 

現在我要輸出的圖像用戶瀏覽器不存儲文件。 這爲什麼我試圖做到這一點

$data = base64_encode(file_get_contents($_FILES['image_file']['tmp_name'])); 

但我覺得在這裏我有我的邏輯失效,becaue它不輸出的畫面,我想反正東西到底是呼應圖片如下所示。

switch($image_type){ 
    case 'image/png': 
     echo '<img src="data:image/png;base64,"'.$data.' alt="" />'; break; 
    case 'image/gif': 
     echo '<img src="data:image/gif;base64,"'.$data.' alt="" />';break; 
    case 'image/jpeg': case 'image/pjpeg': 
     echo '<img src="data:image/jpeg;base64,"'.$data.' alt="" />';break; 
    } 

有人可以看到失敗?

回答

1

的問題是因爲你的雙引號(")在這裏,

... 
echo '<img src="data:image/png;base64,"'.$data.' alt="" />'; break; 
            ^your double quote here is wrong 

它應該是這樣的,

echo '<img src="data:image/png;base64,'.$data.'" alt="" />'; break; 
              ^your double quote should be here 

另外,我不明白任何你使用imagecreatefromXXX()函數的原因,它對你的邏輯沒有任何影響。

根據你的問題:

...要上傳的圖片轉換爲一個變量爲一個字符串或什麼,並顯示它使用<img>標籤

然後你的代碼是這樣的:

HTML

<form action="index.php" method="post" enctype="multipart/form-data"> 
<input name="image_file" type="file" /> 
<input type="submit" name="submit-btn" value="Upload" /> 
</form> 

PHP

if(isset($_POST['submit-btn'])){ 
    // check $_FILES['ImageFile'] not empty 
    if(!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])){ 
      die('Image file is Missing!'); // output error when above checks fail. 
    } 

    //uploaded file info 
    $image_temp = $_FILES['image_file']['tmp_name']; //file temp 

    $image_size_info = getimagesize($image_temp); //get image size 

    if($image_size_info){ 
     $image_type = $image_size_info['mime']; //image type 
    }else{ 
     die("Make sure image file is valid!"); 
    } 

    $data = base64_encode(file_get_contents($image_temp)); 
    switch($image_type){ 
    case 'image/png': 
     echo '<img src="data:image/png;base64,'.$data.'" alt="" />'; break; 
    case 'image/gif': 
     echo '<img src="data:image/gif;base64,'.$data.'" alt="" />';break; 
    case 'image/jpeg': case 'image/pjpeg': 
     echo '<img src="data:image/jpeg;base64,'.$data.'" alt="" />';break; 
    } 
} 
1

正如你所編碼的文件,如下:

$data = base64_encode(file_get_contents($_FILES['image_file']['tmp_name'])); 

所以你應該將它輸出象下面這樣:

switch($image_type){ 
    case 'image/png': 
     echo '<img src="data:image/png;base64,"'.$data.'" alt="" />'; break; 
    case 'image/gif': 
     echo '<img src="data:image/gif;base64,"'.$data.'" alt="" />';break; 
    case 'image/jpeg': case 'image/pjpeg': 
     echo '<img src="data:image/jpg;base64,'.$data.'" alt="" />';break; 
} 

沒有必要使用解碼功能。

1

設置Content-Type頭,並使用$ image_resimagejpegimagegifimagepng需要:

switch($image_type){ 

    case 'image/png': 
     header('Content-Type: image/png'); 
     imagepng($image_res); 

    case 'image/gif': 
     header('Content-Type: image/gif'); 
     imagegif($image_res); 

    case 'image/jpeg': case 'image/pjpeg': 
     header('Content-Type: image/jpeg'); 
     imagejpeg($image_res); 

} 

爲什麼它看起來像在您發佈的屏幕截圖的原因是因爲頭可以」在發送到瀏覽器後修改。輸出表單時,會發送標題。

可以通過分離的形式,並且在不同的頁面PHP的邏輯解決這個問題:

的index.php

<form action="process.php" method="post" enctype="multipart/form-data"> 
<input name="image_file" type="file" /> 
<input type="submit" name="submit btn" value="Upload" /> 
</form> 

過程。PHP

// check $_FILES['ImageFile'] not empty 
if (!isset($_FILES['image_file']) || 
    !is_uploaded_file($_FILES['image_file']['tmp_name'])){ 
     die('Image file is Missing!'); // output error when above checks fail. 
} 

// uploaded file info we need to proceed 
$image_name = $_FILES['image_file']['name']; //file name 
$image_size = $_FILES['image_file']['size']; //file size 
$image_temp = $_FILES['image_file']['tmp_name']; //file temp 

$image_size_info = getimagesize($image_temp); //get image size 

if ($image_size_info) { 
    $image_width  = $image_size_info[0]; //image width 
    $image_height  = $image_size_info[1]; //image height 
    $image_type   = $image_size_info['mime']; //image type 
} 
else { 
    die("Make sure image file is valid!"); 
} 


switch ($image_type) { 

    case 'image/png': 
     $image_res = imagecreatefrompng($image_temp);break; 

    case 'image/gif': 
     $image_res = imagecreatefromgif($image_temp); break; 

    case 'image/jpeg': case 'image/pjpeg': 
     $image_res = imagecreatefromjpeg($image_temp); break; 

    default: 
     $image_res = false; 
} 

switch($image_type){ 

    case 'image/png': 
     header('Content-Type: image/png'); 
     imagepng($image_res); 

    case 'image/gif': 
     header('Content-Type: image/gif'); 
     imagegif($image_res); 

    case 'image/jpeg': case 'image/pjpeg': 
     header('Content-Type: image/jpeg'); 
     imagejpeg($image_res); 

} 
+0

您的解決方案是不工作我得到'警告:imagepng()預計參數1是資源,串given' –

+0

啊,對不起。我的錯。我會更新答案。 – budwiser

相關問題