如何将php页面转换为jpeg文件并下载

Moh*_*Ram 1 html php jpeg

我正在尝试将网页转换为jpeg图像文件.我使用了以下代码.

<?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);

?>
Run Code Online (Sandbox Code Playgroud)

问题:是否有任何库将html页面转换为图像?

Emi*_*röm 5

您需要一个HTML渲染器.存在一些这样的渲染器,但是大多数渲染器需要Web服务器上的X服务器,因此请检查Xvfb以运行没有屏幕的帧缓冲设备.