2

I am trying to convert a web page into jpeg image file. i had used following codes.

<?php    //put your html code here
$html_code = "
<html>
<head>
 <title>My test title</title>
 <style>
 body {
 font-family:verdana;
 font-size:11px;
 color:black
 }
 </style>
</head>
<body>
 this is the body
</body>
</html>";

// Create the image
$img = imagecreate("300", "600");
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,300,600,$c);
imageline($img,300,0,0,600,$c);

$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 9, 0, 1, 1, $white, "VERDANA.TTF", $html_code);

// Display the image
header("Content-type: image/jpeg");
imagejpeg($img);

?>

Question: Is there any library to convert html page into image ?

asheeshr
  • 4,088
  • 6
  • 31
  • 50
Mohan Ram
  • 8,345
  • 25
  • 81
  • 130

4 Answers4

5

You will need an HTML renderer for this. There exists a few such renderers, but most of them requires an X server on your web server, so check out Xvfb to run a framebuffer device without a screen.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
2

GD does not do this natively, you can render a picture from your screen though but that is as far as it goes. I had painty in my links with a description of html to jpg php conversion.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
1

You need to internally render the page. You can find possible solutions here.

Community
  • 1
  • 1
didi_X8
  • 5,018
  • 10
  • 42
  • 46
0

When I needed to 'screen-capture' an HTML table as an image a few months back, I couldn't find anything suitable.

I eventually settled for writing my own code using PHP's GD library with GIF output.

It was for a very specialised purpose and I'm sure it wouldn't suit an application outputting jpegs.

pavium
  • 14,808
  • 4
  • 33
  • 50
  • What about an HTML to PDF to IMG? Depending on the complexity of the display, I would imagine this might work for some cases. – Jared Farrish May 28 '11 at 12:06
  • Well, no, the HTML page was a table of coloured cells - a *wafer map* - and I wanted images I could embed in webpages. I spent months thinking about it, but it was simpler to implement than I expected. – pavium May 28 '11 at 12:11
  • 1
    This answer sounds like it came from imaginationland to me. – defines May 28 '11 at 12:11
  • Maybe I'll think of a question and post my code. I wouldn't help anyone, though, it's too specialised. – pavium May 28 '11 at 12:13