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));
Related
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));
I have a PHP webservice which currently returns a zip archive as its only output. I'm reading the zip archive from disk using file_get_contents and sending it back as the body of the response.
I'd like it to return some additional metadata, in a JSON format:
{
"generatedDate": "2012-11-28 12:00:00",
"status": "unchanged",
"rawData": <zip file in raw form>
}
The iOS app which talks to this service will receive this response, parse the JSON, and then store the zip file locally for its own use.
However, if I try to stuff the result of file_get_contents into json_encode, it rightfully complains that the string is not in UTF-8 format. If I UTF-8-encode it using mb_convert_encoding($rawData, 'UTF-8',
mb_detect_encoding($rawData, 'UTF-8, ISO-8859-1', true));, it will encode it happily, but I can't find a way to reverse the operation on the client (calling [dataString dataUsingEncoding:NSUTF8StringEncoding] and then treating the result as a zip file fails with BOM could not extract archive: Couldn't read pkzip local header.
Can anyone suggest a good way to insert a blob of raw data as one field in a JSON response?
Surely if you successfully included the raw data in the JSON then you'd have the opposite problem at the other end, when you try to decode the JSON and whatever you use to decode can't handle the raw data?
Instead, I would suggest that you send the raw data only in the response body, and use headers to send the metadata.
Strike this question.
It turns out that UTF-8 encoding raw data like this is nonstandard at best, and the standard solution is base-64 encoding it and then using a base-64 decoder to recover it on the client:
$this->response(200, array('rawData' => base64_encode($rawData)));
...
NSString *rawDataString = [[response responseJSON] objectForKey:#"rawData"];
NSData *rawData = [Base64 decode:rawDataString];
ZIP archives are not text—they are binary files! Trying to convert your archive from ISO-8859-1 to UTF-8 makes as much sense as trying to rotate it.
There're several algorithms to serialize binary streams as text but they'll all increase the file size. If that's not an issue, have a look at:
base64_encode()
bin2hex()
unpack()
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.
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.
We are storing entire image files in PostgreSQL, using bytea columns.
In PHP, am trying to open an image file from the bytea field (these stored as hex), then want to manipulate/convert the image using Imagick.
Must the bytestream be converted out of hex to be manageable - in a file-like way - by Imagick? Is there any other secret sauce?
I wouldn't be surprised if we had to read beyond the file header bits, either. Offending snippet is below:
// Decode image from hex?
$image = new Imagick ($row['thewholefile']);
// ERROR: Uncaught exception 'ImagickException' with message 'Unable to read the file: /x0000000 (etc)
Actually the problem here had to do with PostgreSQL's bytea presentation format- as we are using v9.n of PG, the default output is hex:
We had to first set output to PG's 'Old School' bytea handling. Then, by pg-unescaping the raw column data, we had something we could work with:
SET bytea_output = 'escape'
$unescaped = pg_unescape_bytea($content);
You need readimageblob. Here you go:
http://www.php.net/manual/en/function.imagick-readimageblob.php
The error is pretty clear, just as the documentation: the constructor requires a filename or an array of filenames. Use Jauzsika's solution.