4

I have:

echo"
<div id=IwantToSaveThisWholeDivAsAnImage)>
  <div id=OneOfMultipleDivs>
    <table>multiple tables rendered here
    </table>
  </div>
</div>";

I have tried (one of many):

$html_code = " 
<html> 
  <head> 
   <title>Image</title> 
   <style> 
     body { 
     font-family:verdana; 
     font-size:11px; 
     color:black 
   } 
   </style> 
  </head> 
  <body> 
   my tables here
  </body> 
</html>";
$img = imagecreate("300", "600"); 
$c = imagecolorallocate($img, 0, 0, 0);
$c2 = imagecolorallocate($img, 255, 255, 255);
imageline($img,0,0,300,600,$c);
imageline($img,300,0,0,600,$c2);

$white = imagecolorallocate($img, 255, 255, 255); 
imagettftext($img, 9, 0, 1, 1, $white, "arial.tff", '$html_code'); 
header("Content-type: image/jpeg");
imagejpeg($img);

I'm not allowed to use outside libaries. Read that it can be done with GD, but I have been unsuccessful thus far. Any ideas and help would be greatly appreciated! UB

UBES
  • 43
  • 1
  • 4
  • I'm a bit confused. Are you trying to render the actual html document and than save it to img? – macino Oct 04 '12 at 11:41
  • Yes. I have html that I build dynamically and I'm trying to save that to an image (to combine with other elements and finally create a pdf) – UBES Oct 04 '12 at 11:44
  • Why are you not allowed to use any libraries? – BadHorsie Oct 04 '12 at 11:53
  • So it's not a php issue. php provieds just html source code (server-side). The rendering is an browser issue (client-side). See http://stackoverflow.com/questions/1952570/php-how-to-capture-browser-window-screen-with-php/1952665 – macino Oct 04 '12 at 11:54
  • That decision was made by the boss. Just an employee.. But guys, we can go to the moon, surely (I - or I just might need some more experience) must be able to create an image? – UBES Oct 04 '12 at 11:56
  • 1
    Impossible without external tools. Not even possible with PHP alone. I've had succesful experiments using wkhtmltopdf, where one can take a "screenshot" of a website, and you could crop the part you need. – Berry Langerak Oct 04 '12 at 12:03
  • Thank you for everyone's comments so far, I'm going to look at everything you are suggesting and will get back to you. If anyone else has a lightbulb idea, please enlighten us! :D – UBES Oct 04 '12 at 12:18
  • Thank you to Berry, Macino, blue and Amadox for the input. Will have to do this on the server. I accepted Berry's answer as the correct one although I think all of the answers I got were correct (can't mark all of them, sorry) – UBES Oct 04 '12 at 17:22

3 Answers3

6

To explain a bit further than my comment: the rendering of HTML is done by your browser. PHP is server-side, so it doesn't know how to render HTML, nor will it ever. This means that you're going to need "something" that can render HTML and save the rendered result to an image, while PHP can "talk" to it. For this, wkhtmltoimage works (give it a URL, and you end up with a JPG which you can then crop). I've heard good things about phantomjs too, which might be even better suited for your objective, as you can "select" a certain div.

If you're absolutely shut out of using any external tools, you're basically without options.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
3

You can render if by using a tool like wkhtmltoimage

Since it's not written in php, it means you are able to install it on your server and to run it from php.

blue112
  • 52,634
  • 3
  • 45
  • 54
1

You can build tables and write texts in GD, of course, but not from HTML - you'd have to build those yourself (effectivly drawing the tables line by line and then filling them in)

As was already explained by some other users, HTML is rendered clientside, not serverside, your php doesn't know how this will look and thus your GD doesn't either.

Amadox
  • 111
  • 1
  • 3