6

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

ramesh
  • 4,008
  • 13
  • 72
  • 117
  • I just created full HTML page including MAP .... I have to convert this entire html to image .. Please help .. I have to share HTML code ? – ramesh Mar 16 '12 at 05:46

2 Answers2

12

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.

Tom
  • 4,422
  • 3
  • 24
  • 36
4

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);
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109