Solved

loading Blob Image from MSSQL

Posted on 2001-06-26
17
1,418 Views
Last Modified: 2008-01-16
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

0
Comment
Question by:Zuhair070699
  • 9
  • 6
  • 2
17 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 6226868
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.

0
 

Author Comment

by:Zuhair070699
ID: 6227362
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,,
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6227397
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.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6227401
Is the image you are inserting into the database still OK?

Basically, assume everything is broken.
0
 

Author Comment

by:Zuhair070699
ID: 6230030
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,,
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6230420
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.
0
 

Author Comment

by:Zuhair070699
ID: 6230737
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 ??
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6230879
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 40

Expert Comment

by:RQuadling
ID: 6230880
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.
0
 

Author Comment

by:Zuhair070699
ID: 6241852
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 ??
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6244195
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.
0
 

Author Comment

by:Zuhair070699
ID: 6244501
Thanks
0
 

Accepted Solution

by:
til earned 100 total points
ID: 6249125
Your code should look like this:

     $cur = odbc_exec( $cnx, $sql );

     // ensures that lon columns are read.
     odbc_longreadlen($cur, 524288);

     if( ! $cur ) {

Greetings...
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6249200
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!
0
 

Expert Comment

by:til
ID: 6249316
that is, what i wrote...
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6251795
Exactly. I SHOULD have looked there first!

RTFM!
0
 

Author Comment

by:Zuhair070699
ID: 6261991
Thanks that solve my problem :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now