如何在base64中上传和转换图像而不在php中存储图像

时间:2014-04-11 09:27:35

标签: php

我可以上传图像并将其保存在任何位置,然后我在base64中转换该图像。但我们怎么做才能在不保存的情况下转换base64中的图像。请问任何解决方案?

2 个答案:

答案 0 :(得分:0)

如果您有通过$ _POST管理图片上传的Web表单。你可以用这个..

<?php 
if(isset($_POST)){
    $name = $_FILES['file']['name']; 
    $size = $_FILES['file']['size']; 
    $type = $_FILES['file']['type']; 
    $tmp_name = $_FILES['file']['tmp_name']; 

    if(isset($name)){ 
      if(!empty($name)){ 
          $target_path = basename( $_FILES['file']['name']);
          if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
             echo "The file ".  basename( $_FILES['file']['name'])." has been uploaded";

             $im = file_get_contents($_FILES['file']['name']);
             $imdata = base64_encode($im);   
             var_dump($im);
             unlink($_FILES['file']['name']);
          }
        }else{ 
            echo 'Please choose a File'; 
        } 
    }
}
?> 
<form method="POST" enctype="multipart/form-data"> 
<input type="file" name="file"> 
<input type="submit" value="Upload"> 
</form>

答案 1 :(得分:0)

$photoTmpPath = $_FILES['filename']['tmp_name'];
$data = file_get_contents($photoTmpPath);
$base64 = base64_encode($data);

如果你想测试:

header('Content-type: image/png');
echo $data;
exit;