1

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.

7
  • 1
    PHP is truncating your variables. There is a configuration you can change to allow a longer length. I would switch to POST right away. Commented Jan 27, 2014 at 0:20
  • 1
    also note some browsers/servers put a max limit on url lengths as well Commented Jan 27, 2014 at 0:21
  • 2
    ...and if you don't use GET, you don't have to urldecode, which is really just another way your code could break Commented Jan 27, 2014 at 0:23
  • After you switch to POST if you still have issues, take a look at values of PHP configuration for post_max_size and memory_limit Commented Jan 27, 2014 at 0:25
  • @SetSailMedia: not PHP. the webserver. PHP couldn't really care how long a query paramter is, but the webserver will. Commented Jan 27, 2014 at 1:21

2 Answers 2

1

Thanks to the comments:

base64_encode(file_get_contents($image_path));

No urlencode or strtr needed. Use POST request, and alter the post_max_size on the web server to allow for bigger images to be passed through.

$image = base64_decode($this->getParameters('avatar'));
$image_name = md5($image) . ".jpg";
Sign up to request clarification or add additional context in comments.

Comments

0

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?

1 Comment

I’d rather say urldecode should not be applied at all, since PHP does that automatically before populating $_GET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.