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.
Related
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));
How can convert gd resource type to stream image string context (exactly reverse imagecreatefromstring function)
I need the stream image string for convert it to the base64 by base64_encode function.
Without use any external or temp file.
I have image in PostgreSQL. I saved it as bytea (escaped by pg_escape_bytea). But I cannot get unescaped data for sending to browser:
pg_unescape_bytea() expects parameter 1 to be string, resource given
$image = $this->imgRepos->getRecord($id);
$rawimage = pg_unescape_bytea($image->picture);
$rawimage = pg_unescape_bytea(base64_encode($image->picture));
There is solution without base 64 encoding which allows You to read image as is, but it needs to read a file - I'm not sure it will take binary image:
$rawimage = pg_unescape_bytea(file_get_contents($image->picture));
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);
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.