phantomjs插件---实现通过链接生成网页截图

Phantomjs | PhantomJS


  • 配置要求

    windows下,安装完成phantomJS
    设置phantomjs环境变量【也可直接使用phantomjs目录下的执行文件】
    直接通过访问php文件执行/通过cmd命令行执行【phantomjs phantom_script.js】

    linux下,安装完成phantomJS
    设置phantomjs环境变量
    直接命令行执行phantom_script.js即可【phantomjs phantom_script.js】

  • phantom.php

php 复制代码
<?php
// 引入并执行 PhantomJS 脚本
$phantomScript = './phantom_script.js';
$command = 'phantomjs ' . $phantomScript;
$output = shell_exec($command);

echo 'Screenshot saved at: ' . trim($output);
  • phantom_script.js
javascript 复制代码
var page = require('webpage').create();
page.viewportSize = { width: 900, height: 600 };

var url = 'https://www.kancloud.cn/manual/thinkphp6_0/1037479';  // 替换为您的URL

var timestamp = new Date().getTime();
var savePath = './phantomimg/'+timestamp+'.png';  // 保存截图的路径

function takeScreenshot() {
    page.open(url, function(status) {
        if (status === 'success') {
            // 在页面加载完成后,等待一段时间以确保动态数据(如echarts 图表)渲染完全
            setTimeout(function() {
                page.render(savePath);
                console.log(savePath);
                phantom.exit();
            }, 2000);  // 设置等待时间,单位为毫秒(根据网页数据加载时间参考等待时间)
        } else {
            console.log('Unable to load the page.');
            phantom.exit();
        }
    });
}

takeScreenshot()
相关推荐
伊玛目的门徒4 天前
deepseek harness DSH 安装皮肤脚手架(dsh‑client‑ui‑skin‑center)踩坑完整记录
ui·插件·皮肤·harness·dsh
特立独行的猫a14 天前
DeepSeek Harness插件和工具的区别介绍及开发入门指南
前端·ai·agent·插件·deepseek·harness
小徐不徐说18 天前
VS 2022 Visual Assist 破解
c++·visualstudio·工具·插件
带刺的坐椅21 天前
Hot-Plug 实战:运行期管理 Solon 插件
java·solon·插件·热插拔
尸僵打怪兽1 个月前
Claude-code命令行无法下载插件
bug·插件·claude-code
李小白杂货铺1 个月前
Oh My Zsh 简记
macos·macbook·zsh·主题·插件·oh my zsh·omz
anod1 个月前
Amazon反爬虫:一场从自动化到Chrome插件的折腾之旅
javascript·chrome·amazon·插件
njsgcs2 个月前
宿主机和虚拟机共用同一个插件dll
插件
C吴新科2 个月前
IntelliJ IDEA 中 JRebel 扩展插件 激活
intellij-idea·插件·jrebel