dsd57259 2019-04-27 22:37
浏览 42

是否有一个函数将html内容转换并调整为php中的png文件?

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算