dpuwov1487 2009-07-23 10:02
浏览 39
已采纳

如何使用PHP,shell_exec和scrot捕获x屏幕

I'm building a web page screen capture application for an internal R&D project.

Environment: Ubuntu 9.04 (default desktop install), Apache, PHP.

So far I've got a bash script that takes one parameter (URL), fires up firefox, grabs the screen and saves it as a PNG. I've tried running this from terminal and it works fine.

Here's the Bash script:

#!/bin/bash
firefox $1 # Start firefox and go to the passed in URL
scrot -d 5 test.png # Take screen grab with 5 second delay

Next I created a simple PHP page that uses shell_exec to run the script:

<?
  // Sample URL
  $url = 'http://www.google.com'; 
  // Run the script
  shell_exec('sh script.sh ' . $url);
  // Out put HTML to display image
  echo '<img src="test.png" />';
?>

However, when the PHP page is called the screen is not captured. A quick look in the apache error logs show the following message:

Error: no display specified
giblib error: Can't open X display. It *is* running, yeah

I'm guessing this is because apache is running as a different user and hasn't got access to my X display.

So, can anyone shed any light on what I'm doing wrong or how I can capture the current user display.

Thanks.

  • 写回答

3条回答 默认 最新

  • douxiong5438 2009-07-23 10:38
    关注

    Launching firefox from PHP running under Apache seems to me like a bad idea (it definitly feels wrong).

    The way I would do that :

    • a PHP webpage (which runs under Apache) that receives the URL ; something like a form, for instance
      • that page inserts the URL in a database-like system, that will be used as a queue
      • this URL is marked as "to process"
    • a PHP (or another language) script, totally independant from Apache ; for instance, launched by the crontab
      • this scripts selects an URL from the queue in the database (least recent one, for instance), and marks it as "processing"
      • it then lauches your shell-script, which launches firefox and does the screenshot
      • one the screenshot is done, the URL in the queue is marked as "done", and the screenshot's path is associated to the URL
      • this will work, as it is independant from Apache
    • another web page displays the queue, and the status of each URL ("to process", "processing", "done + path to the screenshot"
      • you can even imagine having an association betwen a user and an URL, to not display every URL+screenshot to everyone.

    With this system, several advantages :

    • php+apache for the webpages
    • php outside of apache for the "system" parts
    • you can have the webpages on one server
    • and you can have several machines (linux, windows, mac -- maybe using virtual machines) to make the screenshots
      • allow you to get screenshots from different OSes
      • scales way better ^^

    It's not really an answer to the question, but I think it's a better way... Hope this helps !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况