2014-09-02 76 views
0

美好的一天。有人能帮我解决我的问题吗?我是新的Python和PHP。我想发送base64编码的图像到我的php服务器。但我不知道我的PHP脚本会发生什么。Python - PHP将base64保存到图像

编码的数据正确地发送到php脚本并将newImage.jpg保存到目录c:/image/newImage.jpg。但是,当我尝试预览newImage.jpg,它说“Windows照片查看器无法打开此图片,因为该文件似乎已损坏,损坏或是很大”

问题是我如何保存图像正常。 任何意见和建议,非常感谢。

对不起,我的英语。谢谢。

PHP脚本:

<?php 
    $encodedString = str_replace(' ','+',$_POST['test']); 
    $decoded=base64_decode($encodedString); 
    file_put_contents('c:/image/1/newImage.JPG',$decoded); 
?> 

Python脚本:

import urllib 
import urllib2 
from urllib import urlencode 

url = 'http://192.168.5.165/server/php/try2.php' 
encoded = urllib.quote(open("c:/image/1.jpg", "rb").read().encode("base64")) 

data = {'test': encoded} 
encoded_data = urlencode(data) 

website = urllib2.urlopen(url, encoded_data) 
print website.read() 

回答

0

在你需要urldecode()$_POST['test']数据你的PHP代码,然后base64_decode()它。你不需要用'+'替换空格(无论如何都是反向的)。

在Python中,你只需要urlencode(),你也不需要urllib.quote()

所以你的PHP可以是:

<?php 
    $decoded = base64_decode(urldecode($_POST['test'])); 
    file_put_contents('c:/image/1/newImage.JPG',$decoded); 
?> 

而Python代码:

import urllib 
import urllib2 
from urllib import urlencode 

url = 'http://192.168.5.165/server/php/try2.php' 
encoded = open("c:/image/1.jpg", "rb").read().encode("base64") 

data = {'test': encoded} 
encoded_data = urlencode(data) 

website = urllib2.urlopen(url, encoded_data) 
print website.read() 
0

我是一个懒惰的家伙,但我会帮助改变这个在php

<?php 
    $encodedString = str_replace(' ','+',$_POST['test']); 
    $decoded=base64_decode($encodedString); 
    $decoded=imagecreatefromstring($decoded); 
    imagejpg($decoded, "temp.jpg"); 
    copy("temp.jpg",'c:/image/1/newImage.JPG'); 
?> 

这个问题很简单,你试图把一个还没有成为图像的对象保存到一个文件中,所以首先让它把图像设置为一个临时图像e,然后将其复制到您想要的位置。

相关问题