php - 如何将我的 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 编码字符串。我怎么做?
解决方案
不幸的是你不能
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'];
}
}
推荐阅读
- android - 如何将进度从根 MotionLayout 传播到包含 MotionLayout 项目的子 RecyclerView?
- php - ajax 调用没有得到任何响应
- c# - c# - 我的项目没有从 listbox1 移动到 listbox2
- minecraft - Minecraft 数据包中的传送命令忽略参数
- javascript - JavaScript 中的原型继承和级联继承有什么区别?
- python - TypeError:NAType 类型的对象不是 JSON 可序列化的
- git - 复杂的 git 工作流程
- ios - 从 API 中提取数据时类型不匹配
- arrays - 未指定子数组数量时如何在C中创建数组数组?
- excel - VBA 自动填充 - 单元格引用变量