Skip to content

Commit bb1b2e9

Browse files
author
Chris
committed
Merge pull request #3 from r4nm4/master
Added objectById parameter on capture command
2 parents d985a4f + 7c52d5d commit bb1b2e9

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/JonnyW/PhantomJs/Client.php

+32-7
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,25 @@ public function open(RequestInterface $request, ResponseInterface $response, $de
134134
* @param \JonnyW\PhantomJs\Message\RequestInterface $request
135135
* @param \JonnyW\PhantomJs\Message\ResponseInterface $response
136136
* @param string $file
137+
* @param integer $delay
138+
* @param string $objectById
137139
* @return \JonnyW\PhantomJs\Message\ResponseInterface
138140
*/
139-
public function capture(RequestInterface $request, ResponseInterface $response, $file, $delay = 0)
141+
public function capture(RequestInterface $request, ResponseInterface $response, $file, $delay = 0, $objectById = null)
140142
{
141143
if (!is_writable(dirname($file))) {
142144
throw new NotWriteableException(sprintf('Path is not writeable by PhantomJs: %s', $file));
143145
}
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+
}
150156

151157
return $this->request($request, $response, $cmd);
152158
}
@@ -377,6 +383,25 @@ protected function parse($data)
377383
}, %2\$s);
378384
EOF;
379385

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+
380405
/**
381406
* PhantomJs page open
382407
* command template

0 commit comments

Comments
 (0)