Link to home
Start Free TrialLog in
Avatar of Zuhair070699
Zuhair070699

asked on

loading Blob Image from MSSQL

Hi,

I am using php to connect to MS-SQL Server database through odbc, and every thing is fine.

Now I am looking to read blob field from the database and display it on my site.

I use the following code:

=========================
<?

   $cnx = odbc_connect( 'RW_SQL' , 'xxx', 'xxx' );
   if( ! $cnx ) {
            echo "ERROR";
   }
 
   $sql = "select IMAGE from rwpfext where XXEMP='".addslashes($ImgId)."'";

      $cur= odbc_exec( $cnx, $sql );
      if( ! $cur ) {
            echo "ERROR";
      }
      odbc_fetch_row( $cur );
      $db_bmp= odbc_result( $cur, 1 );
           
      header("Content-type:  image/jpeg");
   print $db_BMP;

?>
=========================

and I call it by:

<img src="loadimage.php?ImgId=012345">

But what I get is blank image and I notice that some time the dimension of the image is correct as stored in my db 110x130.

any idea ??

THANKS

Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Is the image you are storing a JPG image or a BMP type?

Also, try

print $db_bmp;

rather than

print $db_BMP;

PHP is case sensitive on variables.

Regards,

Richard Quadling.

Avatar of Zuhair070699
Zuhair070699

ASKER

Yes you are right I changed $db_BMP to $db_bmp and it displayed 25% of the image and the rest is black, my be the problem with the buffer that allocated for image ?? Any idea.

NOTE: the image type is bmp .

Regards,,
If the image is a BMP rather than a JPG, you may not get the correct results in the browser.

One method I have seen that is popular is to extract the image from the database and save it to disk with a temporary filename, then send it from the saved file instead.

If you CAN save the image to disk in PHP, is it big enough?

Can you tell if the image is in the database correctly to start with?

Regards,

Richard.
Is the image you are inserting into the database still OK?

Basically, assume everything is broken.
Yes all images are inserted correctly into the database .

I allways retrive them from the database using delphi cgi program, and I face the same problem (at the begining) of displaying only about 25% of the image.

What I did to solve this problem is to increase the size of Blob Cash from SQL Explorer.

I thisk my problem may solved if I can increase the blob cash size in php, do you have any idea how can I do that in php ?

Regards,,
Is the transfer taking too long?

By default, the PHP program will only run for 30 seconds.

If you have a large image and a slow modem, you may find things take too long.

I can't see why the cache should make any difference as this should NOT interfere with the BLOB's content. It WILL effect speed if a lot of blobs are asked for at once.

How many bytes are you actually getting from the odbc_result()?

Is it a nice base 2 number? (16284, 32768, 65536, etc?)

If it is a nice number, then I wonder if there is a limit to the amount of data you can retrieve via ODBC in a single hit.

Not too sure from here.

Regards,

Richard.
the transfere is immeditly don after i pres enter button or F5 button so that there is no time out problem.

the size of the image is not big it is only 15 KB size.

Any idea ??
In the PHP code, can you write the retrieved BLOB to a file and see if that loads OK?

What I am trying to see is if the problem lies in retrieving the information from the database or in sending the information to the browser.

I suspect it is not retrieving all of the information correctly.

25% of 15K is around 4K. If this is how much you are retreiving from the DB in a single request, this COULD be a limit!

Also, take a look at the function odbc_field_len to see how much data you can store in this field.

I wonder if, when you store the image, there is some sort of conversion of 8bit to some sort of encoded format going on?

I've not used BLOBs, but I would expect not.

What is the length of the value of $db_bmp? Is it what you expect?

I think saving the image to disk and then physically comparing (I would use FC or DIFF, both are MS-DOS though) to see if there are ANY differences in the files you start with, the one in the database, the one you retrieve and the one that is received by the browser.

Bit of a pain, but I think you may be looking at 1 or 2 problems.

1 - There is a limit to how much data you can get out of a query (not sure, but I would suspect SOME limit somewhere!)
2 - The image info is corrupt when it comes out of the database.

Try a VERY small image too.

Regards,

Richard.
In the PHP code, can you write the retrieved BLOB to a file and see if that loads OK?

What I am trying to see is if the problem lies in retrieving the information from the database or in sending the information to the browser.

I suspect it is not retrieving all of the information correctly.

25% of 15K is around 4K. If this is how much you are retreiving from the DB in a single request, this COULD be a limit!

Also, take a look at the function odbc_field_len to see how much data you can store in this field.

I wonder if, when you store the image, there is some sort of conversion of 8bit to some sort of encoded format going on?

I've not used BLOBs, but I would expect not.

What is the length of the value of $db_bmp? Is it what you expect?

I think saving the image to disk and then physically comparing (I would use FC or DIFF, both are MS-DOS though) to see if there are ANY differences in the files you start with, the one in the database, the one you retrieve and the one that is received by the browser.

Bit of a pain, but I think you may be looking at 1 or 2 problems.

1 - There is a limit to how much data you can get out of a query (not sure, but I would suspect SOME limit somewhere!)
2 - The image info is corrupt when it comes out of the database.

Try a VERY small image too.

Regards,

Richard.
As I said the problem is  a buffer problem, I face the same problem when I write cgi ext using delphi but I solve this problem by increase the blob buffer from SQL explorer.

Is there is a way to increase the blob buffer in php for example in php.ini ??
I noticed that the BLOB type (there are BLOB, TINYBLOB, MEDIUMBLOB and LONGBLOB) have limits of their own.

I've not used blobs yet. I would probably NOT store images in the DB. Instead hold them outside the DB and store the paths.

Sorry I can't help any further.

I've not managed to find any reference to a blob buffer in PHP.

Try using native mssql rather than odbc. Does this make a difference? Could the ODBC driver be doing something?

I use ODBCExpress for my Delphi work, but again, not used blobs so I'm a little short on more ideas.

Regards,

Richard Quadling.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of til
til

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aha!!!!

From the PHP Manual
-------------------

odbc_longreadlen
(PHP 3>= 3.0.6, PHP 4 )

odbc_longreadlen -- Handling of LONG columns
Description

int odbc_longreadlen (int result_id, int length)


(ODBC SQL types affected: LONG, LONGVARBINARY) The number of bytes returned to PHP is controled by the parameter length. If it is set to 0, Long column data is passed thru to the client.

Note: Handling of LONGVARBINARY columns is also affected by odbc_binmode().


--------------------

Doh!
that is, what i wrote...
Exactly. I SHOULD have looked there first!

RTFM!
Thanks that solve my problem :)