I have image files in a database, and the institution I'm working for won't give users permission to write files to the directory structure. So what I want to do is pull an image from the database, then resize it to thumbnail size for display. The PHP is running version 4.2.2, and GD version 1.6, so i have to use imagecopyresize, not imagecopyresample. Also, it uses a PostGRES database.
I can successfully pull the image from the database and display it, using pg_lo_open(), which makes a large object identifier, and pg_lo_read_all(), which outputs the large object data to the browser. All I have to do is make the header of type image/jpeg.
However, the object ID I get from the database doesn't seem to be compatible with the imagecopyresize function. It says:
"
Warning: imagecopyresized(): supplied resource is not a valid Image resource"
I tried writing it to the buffer, using imagejpeg(), but it still gives me the same error.
Anyone have any ideas? I guess the question boils down to how to convert a large object resource to an image resource. I would have thought that data resources would be interchangable.
Here is my code:
Code:
<?php
// open database.
$conn = pg_connect("port=5432 dbname=mseavey");
if (!$conn) {
echo "An error occured connecting to the database.<BR>";
exit;
}
// query the image.
$imageQuery = "SELECT filedata, filetype FROM filetest WHERE id = ".$_GET['id'];
$rs = pg_exec($conn, $imageQuery);
$row = pg_fetch_row($rs, 0);
pg_exec($conn, "begin");
$loid = pg_lo_open($conn, $row[0], "r");
// try to get $loid to be read by the resize function.
ob_start();
imagejpeg($loid);
$buffer = ob_get_contents();
ob_end_clean();
$sloid = imagecreate(100, 100);
// resize the image.
if (imagecopyresized($sloid, $buffer, 0, 0, 0, 0, 100, 100, 477, 478)) {
imagejpeg($sloid);
} else {
echo "Error resizing image.";
}
pg_loclose($loid);
pg_exec ($conn, "commit");
pg_close();
?>