1

Let's say I've done the following in PHP:

$image = imagecreatefromjpeg('myImage.jpg);

Is there a way the image data from $image could be converted to a string that could be sent in my AJAX response?

4
  • What do you want to do with the response? Do you want to display the image? Have the user download it? Based on what your final goal is, there could be a different solution. Commented Sep 18, 2013 at 17:51
  • move it to a folder and then send the path to it as a string in the ajax. Commented Sep 18, 2013 at 17:51
  • @RocketHazmat send it to an iOS app I'm building in Titanium so save locally. Commented Sep 18, 2013 at 17:52
  • 1
    @nathanhayfield I'd considered just sending a path instead... K.I.S.S. Commented Sep 18, 2013 at 17:52

2 Answers 2

2

Something like this should work to get a base64-encoded string from the image:

<?php
$image = imagecreatefromjpeg('myImage.jpg');
ob_start();
imagejpeg($image);
$imagestring = ob_get_contents();
ob_end_clean();
$encoded = base64_encode($imagestring);
?>
<pre><?php echo $encoded ?></pre>
<img src="data://image/jpeg;base64,<?php echo $encoded ?>" alt="myImage" />

This will output the base64-encoded image as a string and will also display the image using that encoded string.

Sign up to request clarification or add additional context in comments.

Comments

0

Use base64, or some other binary->text encoding schemes available, and pass it with the data in ajax, and decode at the client.

PHP has base64 functions, see http://php.net/manual/en/function.base64-encode.php

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.