idci / webpagescreenshot-bundle
Symfony WebPageScreenShotBundle
Requires
- php: >=5.3.2
- doctrine/doctrine-bundle: *
- symfony/framework-bundle: >=2.2
- twig/twig: *
This package is auto-updated.
Last update: 2024-09-24 20:28:11 UTC
README
用于生成网页截图的Bundle
安装
要安装此Bundle,请按照以下步骤操作
步骤 1:下载WebPageScreenShotBundle
首先,在您的composer.json
文件中添加依赖项
"require": { ... "idci/webpagescreenshot-bundle": "dev-master", "gregwar/image-bundle": "dev-master" },
然后使用以下命令安装Bundle
php composer.phar update
步骤 2:启用Bundle
在您的app/AppKernel.php
中注册Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new IDCI\Bundle\WebPageScreenShotBundle\IDCIWebPageScreenShotBundle(), new Gregwar\ImageBundle\GregwarImageBundle(), ); }
步骤 3:安装phantomjs
此Bundle使用phantomjs来生成网站截图。您可以通过apt-get
在Linux上安装它。
sudo apt-get install phantomjs
现在Bundle已安装。
步骤 4:配置Bundle并设置目录
在您的config.yml
中包含一个资源
imports: .... - { resource: @IDCIWebPageScreenShotBundle/Resources/config/config.yml }
在您的routing.yml文件中添加一个控制器。
idci_web_page_screen_shot: resource: "../../vendor/idci/webpagescreenshot-bundle/IDCI/Bundle/WebPageScreenShotBundle/Controller" type: annotation
您必须在您的parameters.yml
文件中指定一些默认值。以下是一个示例。
parameters: ... screenshot_phantomjs_bin_path: "/usr/bin/phantomjs" screenshot_width: 800 screenshot_height: 600 screenshot_mode: file screenshot_format: png screenshot_cache_enabled: true screenshot_cache_delay: 86400 screenshot_cache_directory: %kernel.cache_dir%/screenshot/cache/
screenshot_phantomjs_bin_path表示phantomjs的可执行路径。
您可以使用以下命令查找它
whereis phantomjs
然后,您可以指定一个宽度,一个高度,一个渲染模式和一个渲染格式。有三种模式可供选择:file,url和base64。格式包括png,jpg和gif。enabled缓存参数指定是否将图像放入缓存。延迟参数表示图像的TTL(生存时间)。
使用方法
您可以通过两种方式创建截图。在两种情况下,截图都将存储在parameters.yml文件中指定的目录中。截图的最大分辨率为1440*900。
使用命令创建截图
命令允许您创建截图。就像这样简单
php app/console idci:create:screenshot [url] [width] [height] [mode] [format]
例如,一个有效的命令会是这样的
php app/console idci:create:screenshot https://symfony.ac.cn 800 600 file jpg
如果您没有指定除了URL之外的其他任何参数,提示将建议默认配置值(那些在您的parameters.yml文件中)。您可以按Enter接受它们,或者如果您愿意,可以更改它们。
在控制器中使用服务
已经存在一个控制器。您可能想查看它这里。
有2个操作可用。
第一个操作处理请求,并返回生成的图像作为响应。
请求应如下所示:http://mysymfonyapp/screenshot/capture?url=http://mywebsite.com&format=jpg&mode=url
url模式用于检索匹配第二个操作的URL。
第二个操作简单检索已生成的截图。
请求应如下所示:http://mysymfonyapp/screenshot/get/800x600_website.com.png
您可能想做一些其他的事情。截图管理器可以通过名为idci_web_page_screen_shot.manager的服务访问。因此,您可以在控制器中这样做
$renderer = $screenshotManager ->capture($params) ->resizeImage() ->getRenderer() ;
渲染器负责根据所选模式渲染截图。要获取截图内容,请使用render函数。
$screenshot = $renderer->render();
根据模式,它可以是URL,文件或base64编码的字符串。
$params 是一个包含参数的数组。在现有的控制器中,它是通过请求的参数构建的。无论你做什么,它都应该看起来像这样
$params = array( "url" => "http://mywebsite.com", "mode" => "base64", "width" => 1024, "format" => "gif" );
在此数组中,只需要URL。其他参数将覆盖参数.yml文件中的值。