How convert resource into string (Nette) - php

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));

Related

PHP: Unable to save converted binary value into SQL Server database (Implicit conversion error)

I am trying to write an image into an SQL Server database in binary format with no success. The image uploaded from my frontend (AngularJS) to my server (PHP) is a Base64 string and I need to then convert it to a raw binary file starting with 0x as the database requires a varBinary(max) datatype.
Everything I try seems to end up with the value wrapped in single quotes thus trying to parse it as a varchar and resulting in an error:
General error: 20018 Implicit conversion from data type varchar(max) to varbinary(max) is not allowed.
My current code snippet:
$img = (binary) '0x'.strtoupper(bin2hex($photo['PhotoImage']));
Seems to always result with single quotes and I don't know how to get around it:
PhotoImage='0x6956424F5277304B47676F414141414E53556845556741414156414141414651434141414141444D67336B464'
Update
After some playing around, I have located my problem, I just don't know how to resolve it.
This statement:
$img = (binary)('0x' . bin2hex($photo['PhotoImage']));
dd(gettype($img));
Which (to me) should be returning me a binary value, is in fact STILL returning a string and hence why my S-PROC is failing:
...
"<span class=sf-dump-str title="6 characters">string</span>"
</pre><script>Sfdump("sf-dump-1548954697")</script>
...
Also, as a small test, it seems PHP cannot declare a true binary:
$img = 5;
dd(gettype($img));
/* "<span class=sf-dump-str title="6 characters">integer</span>" */
$img = (binary)5;
dd(gettype($img));
/* "<span class=sf-dump-str title="6 characters">string</span>" */
How can I force PHP to convert this value to a true binary?

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));

How to convert the PostgreSQL bytea value into Data URL value using PHP

PostgreSQL has this datatype called bytea. It is their version of a blob.
In displaying images, png/jpg/gif, the current trend nowadays is to use Data URLs.
My question is how to convert a bytea value into a Base64 Data URL value?
References
bytea - Binary Data Types. PostgreSQL 9.6 Documentation
Data URL - Copy image as data URI by Umar Hansa. Google Developers. May 2015
You may also fetch the complete image data URL from your database, e.g.:
SELECT 'data:image/gif;base64,' || encode(image_data, 'base64') AS image_url
FROM ...
$img_bin = hex2bin(substr($the_bytea, 2));
$url = 'data:image/gif;base64,' . base64_encode($img_bin);
Postgres bytea is a string starting with \x followed by hexadecimal characters. To get the image in binary format, need to strip off the leading \x and convert from hexadecimal characters to binary with hex2bin. Now that you have it in binary, you can base64_encode it.

Reading binary file in php and converting it into string

I spent almost a day for this , but did not get success.
What i want to do is, i have a binary file "data.dat"
I want to read the file contents and output it in text format in say "data.txt" in php.
I tried unpack function of php, but requires the type to be mentioned as the first argument(May be i am wrong, new to php).
$data = fread($file, 4); // 4 is the byte size of a whole on a 32-bit PC.
$content= unpack("C", $data); //C for unsigned charecter , i for int and so on...
But what if i dont know that at what place , what type of data is stored in the file that i am reading?
This function is restricting me because of the type.
I want something similar to this
$content= unpack("s", $data); //where s can denote to string
Thanks.
PHP does not have a "binary" type. Binary data is stored in strings. If you read binary data from a file, it's already stored as a string. You do not need to convert it into a string.
If the binary data already represents text in some standard encoding, you don't need to do anything as you already have a valid string. If the binary data represents some encoding, you need to know what you need to do with it, we don't know.

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.

Categories