$media = json_decode($_POST['media']);
How exactly do you submit your image to this script? Uploading files in PHP is placed in the $ _FILES array, not $ _POST. If your client side of the script does something funky, sending the file with client-> php will never appear in _POST.
imagejpeg($image, '', 90);
This line displays the image as .jpg content immediately, without saving it to a file. Then you do
echo json_encode($response);
which will be a small piece of JSON data. However, you have already outputted the binary image data using the imagejpeg() call, so now you are adding garbage to the file to be sent.
In addition, $image not binary image data. This is the handle to the GD system, which is a resource. Running json_encode on it will not give you json'd.jpg, it will give you some kind of json'd PHP object.
source share