0

Is there a way to turn a jpg to string, reverse of imagecreatefromstring?
I have to communicate to a server which needs binary of image, i saw plenty of jpg to binary but not the other way around.

2 Answers 2

2

Just a shot in the dark here... No real experience with this, just my thoughts after looking through some documentation...

I see in the documentation of imagecreatefromstring() an example is given where a base64 encoded string is converted into an image. Taking that example and flipping it around might just be what you are looking for.

$image = file_get_contents('image_file.jpg');
$imageString = base64_encode($image); 
Sign up to request clarification or add additional context in comments.

Comments

1

imagecreatefromstring takes a string which contains the binary data of an image and turns it into a gd image resource so you can manipulate it with the gd image library. Literally the "reverse" of that would be imagejpeg, which saves a gd image resource to a jpeg image.

I guess what you really want though is simply the initial string, which contains the binary data of the image to begin with. I.e.:

  1. $imageString = file_get_contents('image.jpg');
  2. $gd = imagecreatefromstring($imageString);

Just skip step 2.

Comments

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.