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 "";        print " description
here"; Thank you for the Help B-> ---------------------------------------------------------------------- Nicole · 2024-03-08 13:47 @Dzandaa Thank you for the information. Is there no better way than bmp? I rather would prefer png / jpg  or even svg. Sure, to convert 750 images is not an easy task. ---------------------------------------------------------------------- Dzandaa · 2024-03-08 13:52 Hi, @Nicole QuotePoor windows does not have an error control on copy. If you're talking of Windows (O.S.) I always use TeraCopy https://www.codesector.com/teracopy with error control But my WWW site is a pure Linux Apache server B-> ---------------------------------------------------------------------- Nicole · 2024-03-08 15:27 I cannot decide to change to Linux. It is that much to learn coming from windows. Since years I want this change for basic reasons, which you all here know. About copy file: https://personal-backup.rathlev-home.de/ Not sure, if this link is good for Linux. The software is absolutely free and this is the story behind: A professor for informatics was a Delphi programmer. He hated it, that there are so many file types and some needed to backup, others not. He wanted slim backups. He created a backup tool for file-type selection, include, exclude, encrypted, .... and all you ever can think. As this tool was that genius, he made it open source and added general backup-functions to it. Never have seen a backup-software which does not miss anything. You can even add a taskline-icon for certain projects, to backup it every 19 minutes 34 seconds. Case anybody is interested to bring this software to Lazarus, Linux, etc. Just write a mail to the professor. He is very kind and generous. ---------------------------------------------------------------------- Dzandaa · 2024-03-08 17:56 Hi @Nicole For backup on Windows, I use SyncBack Free for many years. https://www.2brightsparks.com/syncback/syncback-hub.html It can synchronize files by copying only the files that were modified very quickly. Of Course, Linux  is the best, no more License problem, no need to buy new computer because the O.S. is "updated" and also it can run on older P.C.  Not as complicated as you think!!! Linux is Windowed and the updates include all the programs installed with the "Software manager" Like Firefox, LibreOffice, etc... You can test it in a virtual box. B-> ---------------------------------------------------------------------- Dzandaa · 2024-03-09 10:29 Hi, One more thing: I changed the picture format to jpg. Result: 357 Mb to 25Mb Here is the modified code: 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  Flux := TMemoryStream.Create; DBCoverPicture.WriteHeader := False;  DBCoverPicture.Picture.SaveToStreamWithFileExt(Flux, 'jpg'); // **** Save in jpg format, O.K. Flux.Position := 0;  SQLQuery.Params.ParamByName('Picture').LoadFromStream(Flux, ftBlob );  SQLQuery.ExecSQL; SQLTransaction.Commit; SQLQuery.Close; Flux.Free ;   B-> ---------------------------------------------------------------------- Nicole · 2024-03-09 18:12 WOW, this is a great improvement. I bet, this made your week. Think about changing to png, this is under den line just a kind of renamed jpg (so you just type one more line "save as"). I prefer it, e.g. png has an alhpa-channel. For a library this may be very useful. Not sure about the size, I think, it was even slimmer than jpg. Do you speak German? There shall be an English version of it as well: https://de.wikipedia.org/wiki/Portable_Network_Graphics ---------------------------------------------------------------------- Dzandaa · 2024-03-10 10:27 Hi @Nicole Since I also do computer graphics, I'm familiar with the PNG format. But here, transparency doesn't matter, just size, for that, the jpg format is more suitable. https://www.adobe.com/creativecloud/file-types/image/comparison/jpeg- vs-png.html# For example: My DB in jpg = 25Mb in png = 221Mb No Photo. And no sorry, I don't speak German, just English, French and a little Dutch:) B-> ---------------------------------------------------------------------- af0815 · 2024-03-10 10:43 Quote from: Dzandaa on March 10, 2024, 11:27:43 amAnd no sorry, I don't speak German, just English, French and a little Dutch:) You can use a translator, like a lot of other, not so good english speaking people do.  BTW: if you look for the englishe wiki site it is still available https://en.wikipedia.org/wiki/PNG ---------------------------------------------------------------------- Nicole · 2024-03-12 15:18 @ Dzandaa Thank you for this interesting link!