dstnlhhv791576 2014-01-27 00:16
浏览 437

PHP base64编码图像无法正确显示

I'm trying to send an image (any size) over to an API I'm creating, using base64 encoding. The encoded string hits my API as a parameter in the URL. The API built with PHP.

Once the request hits my API, I want to store the image onto my server, and save the file name in the database. This is working, however I'm getting some odd outputs with the actual image.

For reference, I also followed http://blog.justin.kelly.org.au/simple-base64-encodedecode-url-safe-functions/ but have the same results.

The image I am trying to store on my server:

enter image description here

$encode = base64_encode(file_get_contents($image));

echo $encode;

For testing, the ouput of this is the string I'm using to pass to my API.

In the API:

$image = base64_decode($_POST['image']));
$image_name = md5($image) . ".jpg";
file_put_contents(/public/image/ . $image_name, $image);

This works, my image is put onto the server in the correct directory with a random name which is saved to the database.

However, when navigating to the image directly, the image is warped:

enter image description here

If you have a sharp eye, the top part of the image is actually correct before it starts to fail, which makes me start to think whether the string is not getting encoded correctly to be sent as a parameter?

Any clues would be lovely, cheers.

EDIT: Changed it to POST, removed urlencode/decode & removed strtr.

  • 写回答

2条回答 默认 最新

  • doujie4050 2014-01-27 00:35
    关注

    Maybe I'm looking at it wrong, but first you encode it base64 -> urlencode and then you decode it base64 -> urldecode again, shouldn't the latter be in reverse order?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog