问题
I have to create a PNG / JPEG image from PHP script .. Briefly
The code will create a html table and will include an image in that page. I need to save this entire page as an image , save that image in my server and return the path of the image ( using webservice ).
I can create image from imagejpg function pretty well . My problem is how to convert that HTML to image, Cant take a screen shot because the processes going on through web services .
Please help me to convert HTML to image using php
Thanks in advance
回答1:
You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:
$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");
One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.
回答2:
You can use PHP PhantomJS to save a screen capture of a HTML page to the local disk:
<?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);
来源:https://stackoverflow.com/questions/9732114/convert-html-to-image-in-php