SQLite DB how to show picture stocked as BLOB in php
lazarus / 16-Databases
----------------------------------------------------------------------
Dzandaa · 2024-03-08 10:09
Hi everybody,
I created a database of books in Lazarus.
I have a "BLOB" called "Picture" field that contains the cover image
of the books (BMP format)
Here is part the code I use to store a picture in the DB:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
DBCoverPicture: TDBImage;Picture: TPicture;... Picture :=
TPicture.Create; Picture.LoadFromFile(ID); Picture :=
ResizePicture(Picture); DBCoverPicture.Picture.Assign(Picture);
DBCoverPicture.Refresh; ProcessMessages; Picture.Destroy;
... Flux := TMemoryStream.Create; DBCoverPicture.Pic
ture.SaveToStream(Flux); Flux.Position :=
0; SQLQuery.Params.ParamByName('Picture').LoadFromStream(Flux, ftBlob)
; SQLQuery.ExecSQL; SQLTransaction.Commit; SQLQuery.Close; Flux.Free;
Does anyone know how to display this "BLOB" in a WWW page with HTML
and PHP?
I know how to access my DB in HTML/PHP, but I don't know how to
display the image.
Thank you.
B->
----------------------------------------------------------------------
Zvoni · 2024-03-08 10:38
Aircode
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i Var
StreamFromDB:TStream; StreamFromDB:=SQLQuery.CreateBlobStream(SQLQuer
y.FieldByName('Picture'),bmRead); TPicture has LoadFromStream....
And you can write a Stream to a (temp.?) File. The moment you have it
as a file......
----------------------------------------------------------------------
Dzandaa · 2024-03-08 11:41
Hi,
The goal is not to create a file, but to display the picture in php
directly from the sqlite database.
B->
----------------------------------------------------------------------
Zvoni · 2024-03-08 12:10
Quote from: Dzandaa on March 08, 2024, 12:41:19 pmHi,
The goal is not to create a file, but to display the picture in php
directly from the sqlite database.
B->
Post your question in a php-forum?
EDIT: Found here (for MySQL):
https://stackoverflow.com/questions/20556773/php-display-image-blob-
from-mysql
Code: PHP [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i echo '';with
$row being the Resultset and 'image' the Fieldname (in your case
"Picture"?)
----------------------------------------------------------------------
Dzandaa · 2024-03-08 12:40
Hi,
Thank you, but I already try:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i echo
'
';
But the www server tellme that the image had invalid data.
When I look with a SQLite browser https://sqlitebrowser.org/ The
picture is correctly displayed as a BMP image
I'll try again.
B->
----------------------------------------------------------------------
Zvoni · 2024-03-08 12:48
Quote from: Dzandaa on March 08, 2024, 01:40:09 pmHi,
Thank you, but I already try:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i echo
'
';
But the www server tellme that the image had invalid data.
When I look with a SQLite browser https://sqlitebrowser.org/ The
picture is correctly displayed as a BMP image
I'll try again.
B->
Save it to a file, and look at its header (or maybe use one of the
ReaderClasses FPC provides) to find out, which image-format you really
have
Just looked at TPicture again
https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tpicture.html
The interesting Property is "Graphic"
https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tgraphic.html
...and there is a MimeType available
Add a column "MimeType" to your Database, and save whatever Mimetype
the Image really is?
----------------------------------------------------------------------
Nicole · 2024-03-08 13:01
If you have many images and many books after a while, this will become
rather huge in terms of space-taking.
In the same situation I decided not to save the images in the DB, but
their links and have saved them in a second folder.
----------------------------------------------------------------------
Zvoni · 2024-03-08 13:25
Quote from: Nicole on March 08, 2024, 02:01:47 pmIf you have many
images and many books after a while, this will become rather huge in
terms of space-taking.
Not really. A File is a file, and has a size, which is an expression
of bytes.
A blob is a blob, and has a size, which is an expression of bytes.
Both get saved on a disk, just in different places, with the
difference, you (probably) have a slight overhead of bytes when saved
as a blob in a db
QuoteIn the same situation I decided not to save the images in the DB,
but their links and have saved them in a second folder.
Uhh.....please don't start one of those "save the image as a file, and
save the path to it in the DB" vs. "save image as blob in a db"
discussions.
That said: and someone renames any part in the whole folder-path, and
you can't find the images.
--> speaking from experience here.....
User: "Yeah, but i thought naming it that way...."
Me (interrupting user): "You thought? Did it hurt badly?"
----------------------------------------------------------------------
Dzandaa · 2024-03-08 13:34
Hi,
@Nicole,
In the previous version of my DB, I also had a Directory with the
images and a link in the DB, but it was difficult to update the web
server
due to the number of files (about 750 books), so I choose to integrate
the images into the DB. the whole DB is now 350Mb with the pictures.
The first bytes of header are $42 $4d (BM) correspond to a Windows
Bitmap (bmp).
I'll retry again, perhaps I missed some quote or double quote
B->
----------------------------------------------------------------------
Nicole · 2024-03-08 13:44
LOL, this is true. The renaming of folders is a ticking bomb.
I use this solution in a second system (a ods-file with NOT embedded
images) and ask myself, if I will shoot it one day.
Even worse: If my computer says goodbye to me one day, the paths to
the folders are shot.
Yes, this is the disadvantage.
And no, there is not needed less space.
The advantage is, that it is light-weighted. The huger a db becomes,
the more risk is with it in handling as a file.
Poor windows does not have an error control on copy.
So if I have thousands of images, I personally would prefer to use
some folders for them and save this folders as zipped files one file a
folder.
----------------------------------------------------------------------
Dzandaa · 2024-03-08 13:45
Go it!!!
Code: PHP [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
$image = base64_encode($row['Picture']); print "