[英]Convert HTML to image in php
我必须从PHP脚本创建一个PNG / JPEG图像。简而言之
代码将创建一个html表,并在该页面中包含一个图像。 我需要将整个页面保存为图像,将该图像保存在我的服务器中并返回图像的路径(使用webservice)。
我可以很好地从imagejpg功能创建图像。 我的问题是如何将HTML转换为图像,因为通过Web服务进行处理,因此不能拍摄屏幕截图。
请帮我用PHP将HTML转换为图像
提前致谢
您可以从此链接下载wkhtmltoimage。 所有操作系统都有一个版本,因此不应该是一个问题。 然后你可以像这样使用它:
$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");
您要注意的一件事是,如果PHP处于安全模式,则shell_exec将无法运行,您将无法进行转换。
您可以使用PHP PhantomJS将HTML页面的屏幕捕获保存到本地磁盘:
<?php
use JonnyW\PhantomJs\Client;
$client = Client::getInstance();
$width = 800;
$height = 600;
$top = 0;
$left = 0;
$request = $client->getMessageFactory()->createCaptureRequest('http://example.com', 'GET');
$request->setOutputFile('/path/to/save/capture/file.jpg');
$request->setViewportSize($width, $height);
$request->setCaptureDimensions($width, $height, $top, $left);
$response = $client->getMessageFactory()->createResponse();
// Send the request
$client->send($request, $response);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.