php 发送图像并使用json返回它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5141986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sending an image and return it using json?
提问by getaway
im trying to send image to a webservice in php using json, but the client cnt read the image.. when i return it back!!
我正在尝试使用 json 将图像发送到 php 中的网络服务,但是客户端 cnt 读取图像..当我返回时!!
<?php
//recieve the image
$media = json_decode($_POST['media']);
header('content-type: image/jpeg');
$image = imagecreatefromjpeg($media);
imagefilter($image,IMG_FILTER_GRAYSCALE);
imagefilter($image,IMG_FILTER_COLORIZE,100,50,0);
imagejpeg($image, '', 90);
imagedestroy($image);
//return image
$response = array(
'image' => $image
);
echo json_encode($response);
?>
from the code, is there anything I'm doing wrong? Thanks!!! :))
从代码来看,我做错了什么吗?谢谢!!!:))
回答by alex
The JSON MIME type is application/json
, so you can't send image/jpeg
.
JSON MIME 类型是application/json
,因此您无法发送image/jpeg
.
I think it would be easier to send the path to the image, and have your JavaScript make the request to the image.
我认为将路径发送到图像会更容易,并让您的 JavaScript 向图像发出请求。
Otherwise, I'm not sure how you are going to recreate that on the client. Date URIs are recommended to be base64 encoded, so if you insist on doing it like this, please call base64_encode()
first on it.
否则,我不确定您将如何在客户端上重新创建它。日期 URI 建议使用 base64 编码,因此如果您坚持这样做,请先调用base64_encode()
它。
回答by Marc B
$media = json_decode($_POST['media']);
How exactly are you sending your image to this script? File uploads in PHP are placed into the $_FILES array, not $_POST. Unless your client-side script is doing something funky, a file send from client->php would never show up in _POST.
您究竟是如何将图像发送到此脚本的?PHP 中的文件上传放在 $_FILES 数组中,而不是 $_POST。除非您的客户端脚本正在做一些奇怪的事情,否则从 client->php 发送的文件永远不会出现在 _POST 中。
imagejpeg($image, '', 90);
This line will output the image as .jpg content immediately, without saving it to a file. You then do
此行将立即将图像输出为 .jpg 内容,而不将其保存到文件中。然后你做
echo json_encode($response);
which would be a small snippet of JSON data. However, you've already output the image's binary data with the imagejpeg()
call, so now you're appending some garbage to the file you send.
这将是一小段 JSON 数据。但是,您已经通过imagejpeg()
调用输出了图像的二进制数据,因此现在您将一些垃圾附加到您发送的文件中。
Besides, $image
is not the binary data of the image. It's a handle into the GD system, which is a resource. Doing json_encode on it will not give you a json'd .jpg, it'll give you a json'd PHP object of some sort.
此外,$image
不是图像的二进制数据。它是进入 GD 系统的句柄,这是一种资源。在它上面做 json_encode 不会给你一个 json'd .jpg,它会给你一个 json'd PHP 对象。
回答by Henridv
I used this approach to get an image from my server: Get images from PHP server to Android
我使用这种方法从我的服务器获取图像:Get images from PHP server to Android
回答by Jurassic_C
Perhaps use imagedestroy() after building and sending the json response instead of before
也许在构建和发送 json 响应之后而不是之前使用 imagedestroy()