Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML to image in php

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

like image 452
ramesh Avatar asked Mar 16 '12 04:03

ramesh


People also ask

How to convert HTML page to image in PHP?

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

How can convert HTML image to laravel?

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.


2 Answers

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.

like image 50
Tom Avatar answered Oct 16 '22 08:10

Tom


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);
like image 27
João Pimentel Ferreira Avatar answered Oct 16 '22 09:10

João Pimentel Ferreira