Table of Contents
回复内容:
Home Backend Development PHP Tutorial How to take screenshot and save using phantomjs

How to take screenshot and save using phantomjs

Aug 10, 2016 am 09:07 AM
html5 javascript php

这是我的js代码:

<code>var page = require('webpage').create();
var address = 'http://baidu.com';//填写需要打印的文件位置
var output = './img/'+'xigua'+'.png';//存储文件路径和名称
page.viewportSize = { width: 100, height: 100 };//设置长宽
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.render(output);
            phantom.exit();
        }, 500);
    }
});
</code>
Copy after login
Copy after login

这是php代码:

<code>&lt;?php
$command = 'phantomjs '.'phantomjs\123.js'; //上面提到的js文件的路径
exec($command,$out);
</code>
Copy after login
Copy after login

使用以上的代码是可以截图并且保存的,但是无法动态生成图片名.
比如现在需求是保存的图片名称是订单号,就不知道如何传值,然后尝试使用.php来保存上面的js代码,以为这样可以使用动态的名称了,但是也遇到比较多的问题,所以请教各位大神如何传值,才能让phantomjs保存图片名的时候实现动态定义?谢谢.

回复内容:

这是我的js代码:

<code>var page = require('webpage').create();
var address = 'http://baidu.com';//填写需要打印的文件位置
var output = './img/'+'xigua'+'.png';//存储文件路径和名称
page.viewportSize = { width: 100, height: 100 };//设置长宽
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.render(output);
            phantom.exit();
        }, 500);
    }
});
</code>
Copy after login
Copy after login

这是php代码:

<code>&lt;?php
$command = 'phantomjs '.'phantomjs\123.js'; //上面提到的js文件的路径
exec($command,$out);
</code>
Copy after login
Copy after login

使用以上的代码是可以截图并且保存的,但是无法动态生成图片名.
比如现在需求是保存的图片名称是订单号,就不知道如何传值,然后尝试使用.php来保存上面的js代码,以为这样可以使用动态的名称了,但是也遇到比较多的问题,所以请教各位大神如何传值,才能让phantomjs保存图片名的时候实现动态定义?谢谢.

给phantomjs传递参数,比如

<code>phantomjs phantomjs/123.js tupianming</code>
Copy after login

在phantomjs里获取参数

<code>var args = require('system').args;
//args[1]就是你传入的图片名称(tupianming)  args[0]是js文件名</code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles