0

When I run the below query

select first 1 IMG from img_lib;

I get the output as IMG 4cc:0 BLOB display set to subtype 1. This BLOB: subtype = 0

Can any one help me with a PHP code to display the blob content as an Image? I don't think MySQL will work here, Can anyone help me with firebird query?

3
  • I don't know PHP, but this question suggests it should be possible with ibase_blob_echo. Commented Jan 8, 2019 at 10:10
  • That question seems to be more about reverse-engineering application-specific internal format. But there indeed recently was a question, about reading multi-segment blobs.... stackoverflow.com/questions/53304792/… Commented Jan 9, 2019 at 8:38
  • See discussion in the comments at php.net/manual/ru/function.ibase-blob-get.php - the very page seems to only showcase the simpliest case of short blobs (1 segment only) then the users show more elaborate code in discussion. Commented Jan 9, 2019 at 8:40

1 Answer 1

2

PHP offers two libraries to work with Firebird/Interbase databases: Ibase & PDO.

Ibase offer two ways to retrieve blobs. One is using lazy loading with ibase_blob_get function:

if (is_null($field)) return null;
$info = ibase_blob_info($field);
$handle = ibase_blob_open($field);
return ibase_blob_get($handle, $info[0]);

This way is good, when you need to fetch just some blobs, not all, as it is a costly operation.

Another way is to use IBASE_TEXT fetch flag parameter in ibase_fetch_assoc function or fetch object / row. It will return right in fetch result the blob content.

The second library, PDO with Firebird driver, fetches blobs automatically.

Next after you have loaded BLOB content into memory, you need to output it as an image. Here is a sample code, assuming you have a jpeg image format:

<?php
// fetch here blob content described in previous methods
header('Content-Type: image/jpeg');
echo $blob_content;

Later point image source to your script like <img src="blob_image.php" />

Sign up to request clarification or add additional context in comments.

6 Comments

will IBASE_TEXT return contents of big blobs, that exceed the VARCHAR size max limit (32KB AFAIR) ?
"Later point image source to your script" - or use data:// URI scheme :-D
@Arioch'The, as far as i know, yes, we store big blobs and retrieve them without problems. The PHP engine fetch entire blob content, if the memory permits :)
Great! I just was afraid the lib goes lazy way and modifies the SQL SELECT into something like ...cast (blob-column-name as varchar(...) collate octet)...
@Arioch'The, currently we use PDO. Patched some bugs and now it works well. Checked ibase_blob_get function, it allow to load big blobs in chunks ibase_blob_get(handle, size). Strange, at time we used ibase_ we loaded it in one call. PDO internally load the whole blob in chunks: github.com/php/php-src/blob/master/ext/pdo_firebird/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.