Convert HTML to image in php
Asked Answered
G

2

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

Grownup answered 16/3, 2012 at 4:56 Comment(1)
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 ?Grownup
C
13

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.

Caribbean answered 29/3, 2012 at 3:37 Comment(0)
P
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);
Proxy answered 11/5, 2017 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.