gpt4 book ai didi

PHP 将 Base 64 数据解码为 jpeg 以在 Web 浏览器中查看

转载 作者:行者123 更新时间:2023-11-29 00:52:33 24 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我使用 base64 编码的 jpg 图像创建一个 POST 请求,如下所示

    let imageData = UIImageJPEGRepresentation(image, 1.0)

if(imageData == nil ) { return }

let request = NSMutableURLRequest(URL: NSURL(string:"\(self.ip)")!)
request.HTTPMethod = "POST"
request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")


let base64Encoded = imageData!.base64EncodedDataWithOptions(.Encoding64CharacterLineLength)
let postString = "id=\(self.user.userID)&image=\(base64Encoded)"

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())

let task = session.dataTaskWithRequest(request) { ...

然后,在我的 PHP 文件中有

$imgData = base64_decode($_POST["image"]);

if(file_put_contents('image.jpg', $imgData)) {
echo json_encode([
"message" => "The file has been uploaded.",
"status" => "success",
"id" => $id
]);
}
else {
echo json_encode([
"message" => "The file has failed uploading.",
"status" => "error",
"id" => $id
]);
}

PHP 文件说它已经全部正确上传,但是当我转到 image.jpg 的 url 时,它没有显示图像,只有一个问号。

图像真的存在吗?我可以看到 image.jpg 的文件大小在变化,但我无法查看图像。谢谢!

最佳答案

var base64Encoded = imageData!.base64EncodedStringWithOptions([])
base64Encoded = base64Encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet(charactersInString: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~"))!

关于PHP 将 Base 64 数据解码为 jpeg 以在 Web 浏览器中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37959745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com