Skip to content

Commit f2de905

Browse files
Limon MonteMASNathan
authored andcommitted
Add PNG support. (#58)
It works perfectly, thanks for your submission @limonte
1 parent 2fde90e commit f2de905

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ vendor/
44
#composer.lock
55
jobs/*.js
66

7-
demo/test.jpg
7+
demo/test.*
88
test.php

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ You can also set the User Agent
6060
$screenCapture->setUserAgentString('Some User Agent String');
6161
```
6262

63+
And the resulted image format
64+
``` php
65+
// allowed formats are 'jpg' and 'png', default is 'jpg'.
66+
$screenCapture->setFormat('png');
67+
```
68+
6369
And most importantly, save the result
6470
``` php
65-
$fileLocation = '/some/dir/test.jpg';
71+
$fileLocation = '/some/dir/test.' . $screen->getFormat();
6672
$screen->save($fileLocation);
6773
```
6874

demo/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ <h2>Give it a test drive...</h2>
118118
<input type="text" name="bg-color" value="#ffffff" class="form-control color-picker" data-format="hex"/>
119119
</div>
120120

121+
<div class="form-group col-md-3">
122+
<label for="bg-color">Format</label>
123+
<div class="clearfix"></div>
124+
<label class="radio-inline">
125+
<input type="radio" name="format" value="jpg" checked/>
126+
JPG
127+
</label>
128+
<label class="radio-inline">
129+
<input type="radio" name="format" value="png"/>
130+
PNG
131+
</label>
132+
</div>
133+
121134
<div class="col-md-12">
122135
<button class="btn btn-default pull-right">Submit</button>
123136
<p class="lead pull-right">You may have to wait a little bit... &nbsp;</p>

demo/shot.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
$screen->setBackgroundColor($_GET['bg-color']);
3535
}
3636

37-
$fileLocation = 'test.jpg';
37+
if (isset($_GET['format'])) { // Format
38+
$screen->setFormat($_GET['format']);
39+
}
40+
41+
$fileLocation = 'test.' . $screen->getFormat();
3842
$screen->save($fileLocation);
3943

40-
$type = 'image/jpeg';
41-
header('Content-Type:' . $type);
44+
header('Content-Type:' . $screen->getMimeType());
4245
header('Content-Length: ' . filesize($fileLocation));
4346
readfile($fileLocation);

src/Capture.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class Capture
5656
*/
5757
protected $backgroundColor = '#FFFFFF';
5858

59+
/**
60+
* Image format
61+
*
62+
* @var string
63+
*/
64+
protected $format = 'jpg';
65+
5966
/**
6067
* User Agent String used on the page request
6168
*
@@ -260,6 +267,52 @@ public function setBackgroundColor($backgroundColor)
260267
return $this;
261268
}
262269

270+
/**
271+
* Sets the image format
272+
*
273+
* @param string $format 'jpg' | 'png'
274+
*
275+
* @return Capture
276+
*/
277+
public function setFormat($format)
278+
{
279+
$format = strtolower($format);
280+
if (!in_array($format, ['jpg', 'png'])) {
281+
throw new Exception(
282+
"Invalid image format '{$format}'. " .
283+
"Allowed formats are 'jpg' and 'png'"
284+
);
285+
}
286+
287+
$this->format = $format;
288+
289+
return $this;
290+
}
291+
292+
/**
293+
* Gets the image format
294+
*
295+
* @return string
296+
*/
297+
public function getFormat()
298+
{
299+
return $this->format;
300+
}
301+
302+
/**
303+
* Gets the MIME type of resulted image
304+
*
305+
* @return string
306+
*/
307+
public function getMimeType()
308+
{
309+
if ($this->format === 'png') {
310+
return 'image/png';
311+
}
312+
313+
return 'image/jpeg';
314+
}
315+
263316
/**
264317
* Sets the User Agent String to be used on the page request
265318
*

0 commit comments

Comments
 (0)