在PHP中将本地图像Blob转换为base64 [英] Convert local image blob to base64, in PHP

查看:560
本文介绍了在PHP中将本地图像Blob转换为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为内部工具编写(HTML)表单.用户可以填写有关问题的数据并附加屏幕截图.然后将此表单通过Ajax提交给PHPMailer进行发送.问题出在截图上.由于系统限制,我无法让用户实际将文件上传到服务器.

I'm working on an (HTML) form for an internal tool. Users can fill data out about an issue and attach screenshots. This form is then submitted via ajax to PHPMailer to be sent. The issue is with the screenshots. Due to system limitations I'm unable to have the users actually upload the files to the server.

当前,我正在使用HTML5文件阅读器来选择文件.然后,我将图像Blob转换为base64,并将其发送到PHPMailer,以转换为附件.这实际上工作得很好.但是,我遇到了文件大小问题.具体来说就是1000px x 1000px(402KB)的测试图像.产生的base64字符串超过一百万个字符,并且请求返回 413(请求实体太大).

Currently, I'm using HTML5 filereader to select the files. I then convert the image blobs to base64 and send them to PHPMailer, to be converted to attachments. This is actually working great. However, I'm running into file size issues. Specifically a 1000px x 1000px (402KB) test image. The resulting base64 string is over a million characters long and the request is returning 413 (Request Entity Too Large).

我知道base64并不是传输大型图像的有效方法,并且我已经看到有关从数据库检索/转换图像斑点的各种文章.我找不到的是有关检索本地图像Blob并将其转换为base64的信息.

I understand that base64 is not an efficient method for transferring large images and I've seen various posts about retrieving / converting image blobs from a database. What I haven't been able to find is info on retrieving a local image blob and converting it to base64.

我的图片Blob网址如下所示: blob: http://example.com/18960927-e220-4417-93a4-edb608e5b8b3

My image blob URLs look like this: blob:http://example.com/18960927-e220-4417-93a4-edb608e5b8b3

是否有可能在PHP中获取此本地图像数据,然后将其转换为base64?

Is it possible to grab this local image data in PHP and then convert it to base64?

我不能发布很多源代码,但是,以下内容将使您了解我如何使用FileReader

I cannot post much of the source but, the following will give you an idea of how I am using filereader

window.onload=function(){
window.URL = window.URL || window.webkitURL;

var fileSelect = document.getElementById("fileSelect"),
    fileElem = document.getElementById("fileElem"),
    fileList = document.getElementById("fileList");

fileSelect.addEventListener("click", function (e) {
  if (fileElem) {
    fileElem.click();
  }
  e.preventDefault(); // prevent navigation to "#"
}, false);
}

function handleFiles(files) {
  if (!files.length) {
    fileList.innerHTML = "<p>No files selected!</p>";
  } else {
    fileList.innerHTML = "";
    var list = document.createElement("ul");
    fileList.appendChild(list);
    for (var i = 0; i < files.length; i++) {
        if(files[i].size > 1000000) {
            alert(files[i].name + ' is too big. Please resize it and try again.');
        } else {
        var li = document.createElement("li");
      list.appendChild(li);

      var img = document.createElement("img");
      img.src = window.URL.createObjectURL(files[i]);
      img.height = 60;
      img.setAttribute("class", "shotzPrev");
      img.onload = function() {
        window.URL.revokeObjectURL(this.src);
      }
      li.appendChild(img);
      var info = document.createElement("span");
      info.innerHTML = files[i].name + "<br>" + files[i].size + " bytes";
      li.appendChild(info);

        }
    }
  }
}

推荐答案

您可以将File对象的POST php

fetch("/path/to/server", {
  method: "POST"
  body: files[i]
})
.then(response => console.log(response.ok))
.catch(err => console.error(err));

这篇关于在PHP中将本地图像Blob转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆