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
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");
use Spatie\Browsershot\Browsershot; Here's the easiest way to create an image of a webpage: Browsershot::url('https://example.com')->save($pathToImage); By default the screenshot's type will be a png.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With