首页 > 解决方案 > 如何将我的 base64 图像 url 缩小为图像名称?

问题描述

php脚本:

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

if ($contentType === "application/json") {
  //Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));

  $decoded = json_decode($content, true);

  //If json_decode failed, the JSON is invalid.
  if(! is_array($decoded)) {
      echo "Invalid json";

  } else {
      $image=$decoded['image'];
  }
}

在此处输入图像描述

正如您在上面给出的图片中看到的,我只想上传图像文件名而不是它的 base64 编码字符串。我怎么做?

标签: phpmysqlphpmyadmin

解决方案


不幸的是你不能

base64 字符串不包含文件名,只包含文件的内容(source

但是,如果您可以控制发送编码图像的客户端,则可以在 json 有效负载中添加文件名,如下所示:

{
  "image": "your base64 encoded image",
  "filename": "image file name"
}

并在您的 php 代码中获取它:

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

if ($contentType === "application/json") {
  //Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));

  $decoded = json_decode($content, true);

  //If json_decode failed, the JSON is invalid.
  if(! is_array($decoded)) {
      echo "Invalid json";

  } else {
      $image=$decoded['image'];
      $fileName=$decoded['filename'];
  }
}

推荐阅读