microweber/screen
Composer 安装命令:
composer require microweber/screen
包简介
A PHP Class to interact with PhantomJs and capture screenshot of a webpage
README 文档
README
Web site screenshot tool based on PHP and PhantomJS You can use it to take screenshots for testing or monitoring service
Install
Via Composer
$ composer require microweber/screen
If on any Unix system, you need to make the bin executable chmod +x /path/to/screen/bin/phantomjs
The directory /path/to/screen/jobs must be writable as well.
Linux requirements
- FontConfig -
apt-get/yum install fontconfig - FreeType -
apt-get/yum install freetype*
Usage
With this library you can make use of PhantomJs to screenshot a website.
Check our demo or read the following instructions.
Creating the object, you can either pass the URL on the constructor or set it later on
use Screen\Capture; $url = 'https://github.com'; $screenCapture = new Capture($url); // or $screenCapture = new Capture(); $screenCapture->setUrl($url);
You can also set the browser dimensions
$screenCapture->setWidth(1200); $screenCapture->setHeight(800);
you can set also DOM Element Position (top, left)
$screenCapture->setTop(100); $screenCapture->setLeft(100);
This will output all the page including the content rendered beyond the setted dimensions (e.g.: all the scrollable content), if you want just the content inside those boudaries you need to clip the result
// You also need to set the width and height. $screenCapture->setClipWidth(1200); $screenCapture->setClipHeight(800);
Some webpages don't have a background color setted to the body, if you want you can set the color using this method
$screenCapture->setBackgroundColor('#ffffff');
You can also set the User Agent
$screenCapture->setUserAgentString('Some User Agent String');
And the resulted image type
// allowed types are 'jpg' and 'png', default is 'jpg'. $screenCapture->setImageType(Screen\Image\Types\Png::FORMAT); // or $screenCapture->setImageType('png');
- If the format is
jpgand the background color is not set, the default value will be#FFFFFF, ifpngthe default background color will be transparent.
And most importantly, save the result
$fileLocation = '/some/dir/test.' . $screen->getImageType()->getFormat(); $screenCapture->save($fileLocation); // you don't need to set the file extension $fileLocation = '/some/dir/test'; $screenCapture->save($fileLocation); // Will automatically determine the extension type echo $screenCapture->getImageLocation(); // --> /some/dir/test.png
Injection your own JS into the web page
You can also run your own JS scripts or snippets before the screenshot.
For that we have the method includeJs, here are some usage examples:
// Including a remote file $jQueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js'; $screenCapture->includeJs(new \Screen\Injection\Url($jQUeryUrl)); // Including a local file $localFilePath = 'path/to/my/script.js'; $screenCapture->includeJs(new \Screen\Injection\LocalPath($localFilePath)); // Using the scripts included on the library $screen->includeJs(new \Screen\Injection\Scripts\FacebookHideCookiesPolicy()); $screen->includeJs(new \Screen\Injection\Scripts\FacebookHideSignUp()); // Using a js snippet $screen->includeJs("console.log('This is supa cool!');");
Just use this method before calling save(...)
Passing options to PhantomJS
You can set the options that will be passed to the PhantomJS binary.
$screenCapture->setOptions([ 'ignore-ssl-errors' => 'yes', // '--ignore-ssl-errors' => 'yes', // dashes may be omitted ]);
Other configurations
Additionally to the basic usage, you can set so extra configurations.
You can change the where the PhantomJS binary file is.
$screenCapture->binPath = '/path/to/bin/dir/'; // This will result in /path/to/bin/dir/phantomjs
Change the jobs location
$screenCapture->jobs->setLocation('/path/to/jobs/dir/'); echo $screenCapture->jobs->getLocation(); // -> /path/to/jobs/dir/
And set an output base location
$screenCapture->output->setLocation('/path/to/output/dir/'); echo $screenCapture->output->getLocation(); // -> /path/to/output/dir/ // if the output location is setted $screenCapture->save('file.jpg'); // will save the file to /path/to/output/dir/file.jpg
You can also clean/delete all the generated job files like this:
$screenCapture->jobs->clean();
License
The MIT License (MIT). Please see License File for more information.
Credits
Thanks to the PhantomJS (LICENSE) guys for creating their awesome WebKit scripting interface.
This tool was originally created to take screenshots for Microweber
microweber/screen 适用场景与选型建议
microweber/screen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234.2k 次下载、GitHub Stars 达 632, 最近一次更新时间为 2016 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phantomjs」 「capture」 「screen」 「print-screen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 microweber/screen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 microweber/screen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 microweber/screen 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HTML to PDF Symfony bundle. Works with Puppeteer, PhantomJS, and more.
PhantomJS client for SERPS
Captures website screenshot and converts website to PDF using Screenshot machine - online website screenshot generator and website to PDF converter.
Use Laravel Dusk without the full Laravel framework
A set of Monolog extensions.
Bundle adding a set of Monolog extensions.
统计信息
- 总下载量: 234.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 636
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-28