从 PostgreSQL 中,我试图将图像文件读入 Imagick 对象 - 并简单地将相同的文件重新写入另一个 bytea 字段。目标是充分理解 PHP 和 Imagick 中使用的字符串处理和转换。
这是在 PostgreSQL 9 上,所以本机 bytea_output 是十六进制;我一直在关注将 bytea_output 更改为转义以供 PHP 处理的注释。
最终,目标是将此文件转换为 ImageMagick 支持的任何格式。
事情开始得很好!取消转义后,$imagestring 就是磁盘上文件的长度。到现在为止还挺好。事情很快就失控了……
我找不到任何要写回的字符串,它将以相同的长度将文件重新构成回数据库。
这里看起来有什么问题?
// an entire image file is stored in the bytea col: bfile
$query = "SET bytea_output = 'escape'";
$result = spi_exec($query);
# Run the query and get the $result object.
$result = spi_exec($query);
# Fetch the row from the $result.
$row = spi_fetch_row($result);
$content = $row['bfile'];
// pg_unescape it to make it usable?
$unescaped = pg_unescape_bytea($content);
// Image is read into imagestring
$imagestring = substr($unescaped,12); // remove Mac file header?
$length_image = strlen($imagestring); // 330,494 (EXACTLY length on disk)
// Create Imagick object
$im = new Imagick();
// Convert image into Imagick
$im->readimageblob($imagestring);
// Imagick::getImageSize is deprecated. use Imagick::getImageLength
// - here things get weird....
$image_in_length = $im->getImageLength(); // 330,897
// ============= DO WORK HERE ====================================
/* Set format to pdf, or png, or... */
// THIS IS THE OBJECTIVE, of course!
// $im->setImageFormat( 'pdf' );
// =================================================================
// FOR THE TIME BEING, would like to simply re-write the same file
// Output the image
$output = $im->getimageblob();
//IS THIS RIGHT? ouput length is shorter?
$length_output = strlen($output); // 39,654
$escaped = pg_escape_bytea($output);
$length_escaped = strlen($escaped); // 182,720
// FINALLY: Re-concatenate the strings, and write back to blobtest:destfile:
// ALL of these produce the same result:
$query = "UPDATE blobtest SET destfile = '".$escaped."' WHERE pkey = '" .$args[0]. "'";
$result = spi_exec($query);
$length = getImageLength($image);
//return $inputtype;
$query = "SET bytea_output = 'hex'";
$result = spi_exec($query);
return $length;