I am converting an html page to a .png image file. Is there a lightweight library/way to do this?
I have tried using the following but it seems not to be resizing the image after saving using api.rest7.com
//Use api.rest7.com to convert html to png
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api.rest7.com/v1/html_to_image.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "url=https://www.google.com/",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
//save file to server directory
$image = file_get_contents(json_decode($response)->{"file"});
file_put_contents('logs/rendered_page.png', $image);
//reesize image and rewrite it to directory
$image = resize_image('logs/rendered_page.png', 200, 200);
file_put_contents('logs/rendered_page.png', $image);
//function to resize image
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
I am able to save the file but not able to resize it. Are there any other ways we can achieve converting, resizing and saving html to png without using different libraries/apis?