How to achieve the reverse effect of imagecreatefromstring in PHP? - php

How can I get a base64 string from an image resource in PHP? Note however, I made the image on the fly, so no URL exists.
Here is what I've tried, but it doesn't work:
echo 'data:image/png;base64,'.base64_encode($img);
This gives an error:
Warning: base64_encode() expects parameter 1 to be string, resource given

There's no way to tell GD to return your image as a binary string, unfortunately. GD only supports writing to a file or to the screen. What we can do though is use output buffering to capture its output and then put it in a string.
ob_start();
imagepng($img);
$image = ob_get_clean();
echo 'data:image/png;base64,'.base64_encode($image);

Related

convert image blob: to php gd

Good night I have a problem with this
imagecreatefromstring('blob:https://myweb.com/1475db94-d450-44c7-a02f-c06e9fe46a6a');
error output
Message: 'imagecreatefromstring(): Data is not in a recognized format
blob isn't any kind of protocol PHP handles (see http://php.net/manual/wrappers.php).
imagecreatefromstring() expects a string of binary data, not any kind of URI.
I think you need
$uri = 'https://myweb.com/1475db94-d450-44c7-a02f-c06e9fe46a6a';
$image = imagecreatefromstring(file_get_contents($uri));

php reverse imagecreatefromstring

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.
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);
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.:
$imageString = file_get_contents('image.jpg');
$gd = imagecreatefromstring($imageString);
Just skip step 2.

ImageJpeg() - resource required, string given

How can I write an NSData (received from an iOS Device) to a file path in PHP?
I tried the following method, but I am getting a warning regarding the format of the
imagejpeg() expects parameter 1 to be resource, string given
Please can you tell me where I am going wrong?
function store_question_image($blob) {
$blob = 'ffd8ffe000104a46494600010201004800480000f....'; //9975 chars
$filepath = "localhost/citw/img/questions/{$qid}/attachment.png";
return imagejpeg($blob, $filepath);
}
imagejpeg() takes a GD resource handle (aka the in-memory representation of a GD image) and writes it out as a jpeg. You've got what looks to be base64 data(?) of some sort.
You could use imagecreatefromstring($blob) to convert that text into a GD handle, but first you'll have to convert that text into the actual raw binary bytes of the image data, not this encoded format that GD will not know how to handle.

base64 encode an image without saving

Can I base64 encode an image that I created on the fly, without first saving it to disk? As far as I know, base64_encode() only accepts strings, and I couldn't find a way to retrive image source object as string without first saving it, and load it with file_get_contents()
GD doesn't provide a method to return an output image as text, but you can fake it with the output buffering functions:
ob_start();
imagejpeg($handle); // no second parameter, will do output instead of writing to file
$img = ob_get_clean();
echo base64_encode($img);

PHP: GD reference to binary

I have a gd image reference (I've already manipulated the image how I want to), and now I want to store the image. Instead of saving it directly to a file, is there a way I can get the binary data, that way I can convert it to base64 then save the base64 string. The only option I've been able to find is imagejpeg($image), but that either saves it as a file or prints it directly to the browser.
You can output to the buffer, and then capture it in this way.
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();

Categories