@@ -134,19 +134,25 @@ public function open(RequestInterface $request, ResponseInterface $response, $de
134
134
* @param \JonnyW\PhantomJs\Message\RequestInterface $request
135
135
* @param \JonnyW\PhantomJs\Message\ResponseInterface $response
136
136
* @param string $file
137
+ * @param integer $delay
138
+ * @param string $objectById
137
139
* @return \JonnyW\PhantomJs\Message\ResponseInterface
138
140
*/
139
- public function capture (RequestInterface $ request , ResponseInterface $ response , $ file , $ delay = 0 )
141
+ public function capture (RequestInterface $ request , ResponseInterface $ response , $ file , $ delay = 0 , $ objectById = null )
140
142
{
141
143
if (!is_writable (dirname ($ file ))) {
142
144
throw new NotWriteableException (sprintf ('Path is not writeable by PhantomJs: %s ' , $ file ));
143
145
}
144
-
145
- if ($ delay ) {
146
- $ cmd = sprintf ($ this ->captureCmdWithDelay , $ file , $ delay );
147
- } else {
148
- $ cmd = sprintf ($ this ->captureCmd , $ file );
149
- }
146
+
147
+ if ($ objectById !== null ) {
148
+ $ cmd = sprintf ($ this ->captureObjectByIdCmd , $ file , $ objectById );
149
+ } else {
150
+ if ($ delay ) {
151
+ $ cmd = sprintf ($ this ->captureCmdWithDelay , $ file , $ delay );
152
+ } else {
153
+ $ cmd = sprintf ($ this ->captureCmd , $ file );
154
+ }
155
+ }
150
156
151
157
return $ this ->request ($ request , $ response , $ cmd );
152
158
}
@@ -377,6 +383,25 @@ protected function parse($data)
377
383
}, %2 \$s);
378
384
EOF ;
379
385
386
+ /**
387
+ * PhantomJs object id capture
388
+ * command template
389
+ *
390
+ * @var string
391
+ */
392
+ protected $ captureObjectByIdCmd = <<<EOF
393
+
394
+ var clipRect = page.evaluate(function() {
395
+ return document.getElementById("%2 \$s").getBoundingClientRect();
396
+ });
397
+
398
+ page.clipRect = { top: clipRect.top, left: clipRect.left, width: clipRect.width, height: clipRect.height }
399
+ page.render('%1 \$s', {format: 'jpeg', quality: '100'});
400
+
401
+ console.log(JSON.stringify(response, undefined, 4));
402
+ phantom.exit();
403
+ EOF ;
404
+
380
405
/**
381
406
* PhantomJs page open
382
407
* command template
0 commit comments