-1

I want to make a some step of saving data into DB as BLOB data type. I need convert data to BLOB in the DB. I have the data that are considered as HTML output in the some file. The selected programming language is PHP with Zend Framework 1 and database is MySQL.

I tried to save HTML page as valid BLOB image in DB, but it is still not working. I can't find any solutions about it and I don't know how to do. When I got a content from URL and saved it into DB as BLOB by using function file_get_contents($url), that is worked to me fine.

Here is a code that saves the content of the HTML page in the file described below into DB as BLOB.

$file = "C:\test.html";
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
$dbModel = new MyTable($this->db);
$dbRowSet = $dbModel->find(1);
$dbRow = $dbRowSet->current();
$dbRow->map = $content;
$dbRow->save();

Here is a preview of saved HTML page in DB, but there I can't show any BLOB image. image 1

There is shown BLOB image. I had the URL page and tried to save it into DB. image 2

I expected that the result of saving data (HTML page) into DB should be that DB contains valid BLOB images.

Thank you for your help.

  • 1
    I'm not sure what you're asking. Are you asking how to save and display an image as a blob? – Brett Gregson Aug 30 '19 at 07:57
  • your question is too unclear to provide answer to, could you be more specific about the problem statement, as far as I understand, you are having trouble in saving an image as BLOB in the database. Is it the case? – dper Aug 30 '19 at 08:09
  • _“I expected that the result of saving data (HTML page) into DB should be that DB contains valid BLOB images.”_ - well, you expect wrong. When your HTML code gets parsed inside a browser, the browser finds those image URLs inside the HTML, and makes additional request to load those images. You have no browser involved here, you are _just_ storing HTML code into your database. There is no one here that would take care of actually requesting those images. – misorude Aug 30 '19 at 08:10
  • @BrettGregson I want to know how to save HTML page and display it as image – Andrej Kozlovský Aug 30 '19 at 08:19
  • So you want to save HTML and display it as an image? What would you expect to display for `
    Hello
    `? An image containing the word "Hello"?
    – Brett Gregson Aug 30 '19 at 08:21
  • @BrettGregson yes, you expect same as what I would expect – Andrej Kozlovský Aug 30 '19 at 08:32
  • I don't expect anything, I'm asking you so I can try help you. If you want to convert HTML to an image see here: https://stackoverflow.com/questions/10721884/render-html-to-an-image – Brett Gregson Aug 30 '19 at 08:54

1 Answers1

0

You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:

$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");

One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.

Credit: @Tom Convert HTML to image in php


After this you just need file_get_contents() and save blob in the database.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78