【问题标题】:How to upload base64 encoded image to server如何将base64编码的图像上传到服务器
【发布时间】:2016-10-23 16:16:15
【问题描述】:

我有 jquery 插件,它以我想存储在服务器中的 base 64 编码格式发送图像

这是我尝试过的

$post = json_decode($_POST['file'], true); $data = $post['output']['image'];

 $data = str_replace('data:image/png;base64,', '', $data);

$data = str_replace(' ', '+', $data);

$data = base64_decode($data);

require('/home/example/public_html/files/image/class.upload.php');

$code = md5(time());        
$handle = new upload($data);
if ($handle->uploaded) {
  $handle->file_new_name_body = "$code";
  $handle->mime_check = true;
  $handle->allowed = array('image/*');
  $handle->image_convert = 'jpg';
$handle->jpeg_quality = 70;
$handle->image_resize         = true;
  $handle->image_x              = 600;
  $handle->image_ratio_y        = 600;
  $handle->process('/home/example/public_html/files/blog/img/');
  if ($handle->processed) {
  $file_name = $handle->file_dst_name;
  } else {
  echo "error";
  }
}

我上面的代码图片上传类适用于每张图片,但我无法上传 base 64 编码的图片,我该如何实现

【问题讨论】:

    标签: php image image-processing base64


    【解决方案1】:

    您使用的上传类不支持直接将 base64 输入其中。您最好先使用this method 将临时版本保存到您的目录,然后使用该类执行您需要执行的操作,然后将其删除:

    $data = str_replace('data:image/png;base64,', '', $data);
    
    $data = str_replace(' ', '+', $data);
    
    $data = base64_decode($data);
    
    require('/home/example/public_html/files/image/class.upload.php');
    
    $code = md5(time());   
    $write_dir = "/home/example/public_html/files/blog/img/";
    $temp_code = "temp_".$code;
    
    $ifp = fopen($write_dir.$temp_code, "wb"); 
    fwrite($ifp, $data); 
    fclose($ifp); 
    
    $handle = new upload($write_dir.$temp_code);
    if ($handle->uploaded) {
        $handle->file_new_name_body = "$code";
        $handle->mime_check = true;
        $handle->allowed = array('image/*');
        $handle->image_convert = 'jpg';
        $handle->jpeg_quality = 70;
        $handle->image_resize         = true;
        $handle->image_x              = 600;
        $handle->image_ratio_y        = 600;
        $handle->process($write_dir);
        if ($handle->processed) {
            $file_name = $handle->file_dst_name;
            unlink($write_dir.$temp_code);
        } else {
            echo "error";
        }
    }
    

    【讨论】:

    • base 64 解码会降低图像质量,但为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    相关资源
    最近更新 更多