简体   繁体   English

如何将base64编码的图像上传到服务器

[英]How to upload base64 encoded image to server

I have jquery plugin which send image in base 64 encoded format which i want to store in server 我有jquery插件,该插件以我要存储在服务器中的base 64编码格式发送图像

here is what I've tried 这是我尝试过的

$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";
  }
}

My above code image upload class works on every image but i'm unable to uploade base 64 encoded image, how can i achieve that 我上面的代码图像上载类适用于每张图像,但是我无法上载base 64编码图像,我该如何实现

The upload class you're using doesn't support feeding base64 directly into it. 您使用的上载类不支持直接将base64输入。 You're best off using this method to save a temporary version to your directory first, using the class to do what you need to do, then deleting it: 最好使用此方法将临时版本保存到目录中,然后使用该类执行所需的操作,然后将其删除:

$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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM