Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending an image and return it using json?

im trying to send image to a webservice in php using json, but the client cnt read the image.. when i return it back!!

<?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!!! :))

like image 891
getaway Avatar asked Feb 28 '11 12:02

getaway


People also ask

How do I view an image in a JSON file?

Create a new Workbook object and open JSON file. Save JSON as JPEG using save method. Load JPEG document by using Image class. Save the document to JPEG2000 format using save method.

How do you convert a JSON file to a regular file for pictures?

Just drag and drop your JSON file on upload form, choose the desired output format and click convert button. Once conversion completed you can download your JPG file. You even can perform more advanced conversions.


1 Answers

The JSON MIME type is application/json, so you can't send 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.

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.

like image 195
alex Avatar answered Sep 20 '22 23:09

alex