【问题标题】:Convert HTML to image in php在php中将HTML转换为图像
【发布时间】:2012-04-01 16:36:29
【问题描述】:

我必须从 PHP 脚本创建一个 PNG / JPEG 图像..简要

代码将创建一个 html 表格并在该页面中包含一个图像。我需要将整个页面保存为图像,将该图像保存在我的服务器中并返回图像的路径(使用 web 服务)。

我可以很好地从 imagejpg 函数创建图像。我的问题是如何将该 HTML 转换为图像,因为进程通过 Web 服务进行,所以无法截屏。

请帮助我使用 php 将 HTML 转换为图像

提前致谢

【问题讨论】:

  • 我刚刚创建了完整的 HTML 页面,包括 MAP .... 我必须将整个 html 转换为图像.. 请帮助.. 我必须共享 HTML 代码?

标签: php image-processing php-gd


【解决方案1】:

您可以从this link 下载 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 将无法工作,您将无法进行转换。

【讨论】:

    【解决方案2】:

    您可以使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 2011-06-02
      • 2010-10-14
      • 2011-04-01
      • 2016-02-25
      相关资源
      最近更新 更多