5

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

Jurassic_C
  • 2,373
  • 3
  • 22
  • 19
getaway
  • 8,792
  • 22
  • 64
  • 94
  • 2
    Sending binary image data as JSON makes very little sense in the first place. But even if you do this, we need to know how your client actually processes the image to give any relevant information – Pekka Feb 28 '11 at 12:32
  • EDIT: one of the solution i was thinking of doing, is actually generating a new image file, and then sending it back as an url! – getaway Feb 28 '11 at 12:32

4 Answers4

8

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.

alex
  • 479,566
  • 201
  • 878
  • 984
  • but that will require me to create an image file on the server right? – getaway Feb 28 '11 at 12:35
  • @getaway Yeah, to send a path would require you make the file. You could always set an image's `src` attribute to the output of this script (drop the JSON response stuff). – alex Feb 28 '11 at 12:36
  • yeh that would be kool @alex, could give me an example of settings the src!!! im so confused!! thanks +1 from me – getaway Feb 28 '11 at 12:38
  • @getaway I mean you could do (with JavaScript) `document.getElementById('your-image').src = 'your-image-generator.php';`. – alex Feb 28 '11 at 12:41
  • oh thanks okay, in terms of the base64_encode do i run that on the image in the php script, and then decode it on my clien(javascript) – getaway Feb 28 '11 at 12:44
  • @getaway You do not need to decode it yourself. Check out [DataURIs](http://en.wikipedia.org/wiki/Data_URI_scheme). – alex Feb 28 '11 at 12:49
2
$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.

imagejpeg($image, '', 90);

This line will output the image as .jpg content immediately, without saving it to a file. You then do

 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.

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
-1

Perhaps use imagedestroy() after building and sending the json response instead of before

Jurassic_C
  • 2,373
  • 3
  • 22
  • 19
-1

I used this approach to get an image from my server: Get images from PHP server to Android

Community
  • 1
  • 1
Henridv
  • 766
  • 12
  • 23