0

I've got a python Image object from a screen capture that I want to save on a server running PHP.
I want to do all of this without at all saving the image on the user's computer, so I want to send the PHP script an encoding it can understand which I can get from the PIL Image instance.

So far, I've tried saving the Image to a buffer (in python) and sending the data to the PHP function file_put_contents, but the resulting file isn't a correct .png image (I've just saved the PNG encoding string to a file with a .png extension...)

Here's my code...

Client

# shot is an Image instance

buff = StringIO()
shot.save(buff, 'PNG')

args = {'filename':'eg.png','image': buff.getValue()}
data = urllib.urlencode(args)
request = urllib2.Request(self.uploadURL, data)
urllib2.urlopen(request).read()

Server

# 'filename' arg ends in .png,  'image' arg is the upload parameter ^

$filename = 'images/'.$_POST['filename'];
file_put_contents($filename, $_POST['image']);

I take it I need to convert the image data with PHP before I try to write it to file, but I'm not sure how.

I've done this before by saving the Image to file and sending the binary content in base 64, but I don't wish to save the Image on the client's computer at all.

Thanks!
Disclaimer: This question and my perception on encoding feels entirely 'icky' to me, so sorry for being such a huge noob / horrible misunderstanding.

Anti Earth
  • 4,671
  • 13
  • 52
  • 83
  • Follow this link http://stackoverflow.com/questions/7877282/how-to-send-image-generated-by-pil-to-browser may be help for you. – Anup Feb 26 '13 at 12:41
  • @Anup I really don't want to save the image on their computer at all, even if only temporary. I simply don't believe it's required (or a good solution) – Anti Earth Feb 26 '13 at 12:45
  • I checked the Python side of your solution, and it seems to work: it should give to `upload()` the whole PNG data. You need to check what is really transmitted and received on the server side. What does the final .png file look like if you open it with a text editor? – Armin Rigo Feb 26 '13 at 12:51
  • @Armin [The saved image content](http://justpaste.it/imagecontents) – Anti Earth Feb 26 '13 at 12:57
  • @AntiEarth Uh, that looks like a valid PNG image to me. But you can't open it in an image viewer? – Armin Rigo Feb 26 '13 at 13:00
  • @Armin No, I can't. Can you? :/ – Anti Earth Feb 26 '13 at 13:02
  • For debugging, save the file locally, and compare them. What is the difference? – Armin Rigo Feb 26 '13 at 13:08
  • @Armin Image still can't be viewed and the data looks much the same format. Could this be an issue with the buffer? – Anti Earth Feb 26 '13 at 13:17
  • @Armin Progress: The file is saved correctly locally if I write the data to a file open in binary mode. Does PHP have such a mode? – Anti Earth Feb 26 '13 at 13:21
  • Adding 'b' to the fopen mode in PHP did nothing, and the last commentor's suggestion on [this thread] (http://php.net/manual/en/function.fwrite.php) didn't work either. :( – Anti Earth Feb 26 '13 at 13:33

2 Answers2

1

Solved

Some funny business was happening with the UTF-8 encoding in PHP.
(Using the utf8_encode function was not successful)

To avoid UTF8, I base64 encoded the UTF8 data and sent it to the PHP script, which decoded it and wrote it to a file (in binary mode).

<?php

$filename = 'images/'.$_POST['filename'];

$fp = fopen($filename, 'wb');
fwrite($fp, base64_decode($_POST['image']));
fclose($fp);

?>
Anti Earth
  • 4,671
  • 13
  • 52
  • 83
0

The PHP side is more complex. I guess that the upload is done with an HTTP POST file. If it is, then $_POST['image'] is not the whole content of the uploaded file. See http://www.php.net/manual/en/features.file-upload.post-method.php and move_uploaded_file().

Armin Rigo
  • 12,048
  • 37
  • 48