开发者

How to convert a php page in to jpeg file and to download

开发者 https://www.devze.com 2023-03-08 18:57 出处:网络
I am trying to convert a web page into jpeg image file. i had used following codes. <?php//put your html code here

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>
<hea开发者_运维知识库d>
 <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 ?


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消