【问题标题】:Pipe generated javascript via commandline通过命令行管道生成的javascript
【发布时间】:2015-10-08 08:13:50
【问题描述】:

我们使用PhantomJS 创建网页的屏幕截图。它是从命令行运行的,因为出于某种原因php-phantomjsscreen-master 在我们的服务器上不起作用。解决方案是使用 PHP shell_exec() 命令运行命令行脚本。

PhantomJS 使用一个 javascript 文件,它告诉我们要做什么、要加载哪个页面以及在哪里写入文件。由于多个用户将使用此功能,我需要将用户 ID 或其他内容传递到 javascript 文件中,以便它可以加载用户特定页面并使用名称中的用户 ID 保存屏幕截图。

一种选择是通过 PHP 生成 javascript,然后使用它。采取以下javascript:

var id = 1234;
var page = require('webpage').create();
page.viewportSize = { width: 1024, height: 768 };
page.clipRect = { top: 0, left: 0, width: 1024, height: 768 };
page.open('http://test.local/screenshot.php?id='+id, function(status) {
  console.log("Status: " + status);
  if(status === "success") {
    page.render('/var/www/test/tmp/screenshot-'+id+'.png');
  }
  phantom.exit();
});

shell命令是这样的:

/path/to/phantomjs /var/www/test/js/phantom.js

变量id 的值为1234。该值不是固定的,而是根据用户而变化。 javascript 文件不是动态的,但可以使用 PHP 生成。我可以生成 javascript 文件,或者只生成脚本。

我可以通过命令行将此生成的脚本(或文件)传送到 PhantomJS 吗?

我还有哪些其他选项可以更好地完成这项工作?

【问题讨论】:

    标签: javascript php shell pipe phantomjs


    【解决方案1】:

    如果你想发送命令 args 到 pj,你可以这样做:

     var system = require('system');
        var page = require('webpage').create();
        page.open(system.args[1], function () {
            page.paperSize = { format: 'A4', orientation: 'landscape'};
            window.setTimeout(function(){
                page.render(system.args[2]);
                phantom.exit();
            },300);
        });
    

    在 php 中:

    $cmd = 'phantomjs '.$path. 'render.js '.$html_tpl.' '. $image;
    exec($cmd);
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多