File tree Expand file tree Collapse file tree 5 files changed +80
-5
lines changed Expand file tree Collapse file tree 5 files changed +80
-5
lines changed Original file line number Diff line number Diff line change 4
4
# composer.lock
5
5
jobs /* .js
6
6
7
- demo /test.jpg
7
+ demo /test. *
8
8
test.php
Original file line number Diff line number Diff line change @@ -60,9 +60,15 @@ You can also set the User Agent
60
60
$screenCapture->setUserAgentString('Some User Agent String');
61
61
```
62
62
63
+ And the resulted image format
64
+ ``` php
65
+ // allowed formats are 'jpg' and 'png', default is 'jpg'.
66
+ $screenCapture->setFormat('png');
67
+ ```
68
+
63
69
And most importantly, save the result
64
70
``` php
65
- $fileLocation = '/some/dir/test.jpg' ;
71
+ $fileLocation = '/some/dir/test.' . $screen->getFormat() ;
66
72
$screen->save($fileLocation);
67
73
```
68
74
Original file line number Diff line number Diff line change @@ -118,6 +118,19 @@ <h2>Give it a test drive...</h2>
118
118
< input type ="text " name ="bg-color " value ="#ffffff " class ="form-control color-picker " data-format ="hex "/>
119
119
</ div >
120
120
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
+
121
134
< div class ="col-md-12 ">
122
135
< button class ="btn btn-default pull-right "> Submit</ button >
123
136
< p class ="lead pull-right "> You may have to wait a little bit... </ p >
Original file line number Diff line number Diff line change 34
34
$ screen ->setBackgroundColor ($ _GET ['bg-color ' ]);
35
35
}
36
36
37
- $ fileLocation = 'test.jpg ' ;
37
+ if (isset ($ _GET ['format ' ])) { // Format
38
+ $ screen ->setFormat ($ _GET ['format ' ]);
39
+ }
40
+
41
+ $ fileLocation = 'test. ' . $ screen ->getFormat ();
38
42
$ screen ->save ($ fileLocation );
39
43
40
- $ type = 'image/jpeg ' ;
41
- header ('Content-Type: ' . $ type );
44
+ header ('Content-Type: ' . $ screen ->getMimeType ());
42
45
header ('Content-Length: ' . filesize ($ fileLocation ));
43
46
readfile ($ fileLocation );
Original file line number Diff line number Diff line change @@ -56,6 +56,13 @@ class Capture
56
56
*/
57
57
protected $ backgroundColor = '#FFFFFF ' ;
58
58
59
+ /**
60
+ * Image format
61
+ *
62
+ * @var string
63
+ */
64
+ protected $ format = 'jpg ' ;
65
+
59
66
/**
60
67
* User Agent String used on the page request
61
68
*
@@ -260,6 +267,52 @@ public function setBackgroundColor($backgroundColor)
260
267
return $ this ;
261
268
}
262
269
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
+
263
316
/**
264
317
* Sets the User Agent String to be used on the page request
265
318
*
You can’t perform that action at this time.
0 commit comments