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