2
2
3
3
namespace Screen ;
4
4
5
+ use Screen \Exceptions \InvalidArgumentException ;
6
+ use Screen \Exceptions \TemplateNotFoundException ;
7
+ use Screen \Image \Types ;
8
+ use Screen \Image \Types \Type ;
9
+ use Screen \Injection \LocalPath ;
10
+ use Screen \Injection \Url ;
5
11
use Screen \Location \Jobs ;
6
12
use Screen \Location \Output ;
7
13
@@ -57,11 +63,11 @@ class Capture
57
63
protected $ backgroundColor = '' ;
58
64
59
65
/**
60
- * Image format
66
+ * Image Type, default is jpeg
61
67
*
62
- * @var string
68
+ * @var Type
63
69
*/
64
- protected $ format = ' jpg ' ;
70
+ protected $ imageType ;
65
71
66
72
/**
67
73
* User Agent String used on the page request
@@ -72,9 +78,10 @@ class Capture
72
78
73
79
/**
74
80
* Sets the timeout period
81
+ *
75
82
* @var int
76
83
*/
77
- protected $ resourceTimeout = 0 ;
84
+ protected $ timeout = 0 ;
78
85
79
86
/**
80
87
* Bin directory, should contain the phantomjs file, otherwise it won't work
@@ -104,6 +111,27 @@ class Capture
104
111
*/
105
112
public $ output ;
106
113
114
+ /**
115
+ * Location where the file was written to
116
+ *
117
+ * @var string
118
+ */
119
+ protected $ imageLocation ;
120
+
121
+ /**
122
+ * List of included JS scripts
123
+ *
124
+ * @var array
125
+ */
126
+ protected $ includedJsScripts = array ();
127
+
128
+ /**
129
+ * List of included JS snippets
130
+ *
131
+ * @var array
132
+ */
133
+ protected $ includedJsSnippets = array ();
134
+
107
135
/**
108
136
* Capture constructor.
109
137
*/
@@ -118,18 +146,31 @@ public function __construct($url = null)
118
146
119
147
$ this ->jobs = new Jobs ();
120
148
$ this ->output = new Output ();
149
+
150
+ $ this ->setImageType (Types \Jpg::FORMAT );
121
151
}
122
152
153
+ /**
154
+ * Saves the screenshot to the requested location
155
+ *
156
+ * @param string $imageLocation Image Location
157
+ * @param bool $deleteFileIfExists True to delete the file if it exists
158
+ *
159
+ * @return bool
160
+ */
123
161
public function save ($ imageLocation , $ deleteFileIfExists = true )
124
162
{
125
- $ outputPath = $ this ->output ->getLocation () . $ imageLocation ;
163
+ $ this ->imageLocation = $ this ->output ->getLocation () . $ imageLocation ;
164
+
165
+ if (!pathinfo ($ this ->imageLocation , PATHINFO_EXTENSION )) {
166
+ $ this ->imageLocation .= '. ' . $ this ->getImageType ()->getFormat ();
167
+ }
126
168
127
169
$ data = array (
128
170
'url ' => $ this ->url ,
129
171
'width ' => $ this ->width ,
130
172
'height ' => $ this ->height ,
131
- // If used on windows the \ char needs to be handled to be used on a js string
132
- 'imageLocation ' => str_replace ("\\" , "\\\\" , $ outputPath ),
173
+ 'imageLocation ' => LocalPath::sanitize ($ this ->imageLocation ),
133
174
);
134
175
135
176
if ($ this ->clipWidth && $ this ->clipHeight ) {
@@ -141,7 +182,7 @@ public function save($imageLocation, $deleteFileIfExists = true)
141
182
142
183
if ($ this ->backgroundColor ) {
143
184
$ data ['backgroundColor ' ] = $ this ->backgroundColor ;
144
- } elseif ($ this ->getFormat () == ' jpg ' ) {
185
+ } elseif ($ this ->getImageType ()-> getFormat () == Types \Jpg:: FORMAT ) {
145
186
// If there is no background color set, and it's a jpeg
146
187
// we need to set a bg color, otherwise the background will be black
147
188
$ data ['backgroundColor ' ] = '#FFFFFF ' ;
@@ -151,12 +192,20 @@ public function save($imageLocation, $deleteFileIfExists = true)
151
192
$ data ['userAgent ' ] = $ this ->userAgentString ;
152
193
}
153
194
154
- if ($ this ->resourceTimeout ) {
155
- $ data ['resourceTimeout ' ] = $ this ->resourceTimeout ;
195
+ if ($ this ->timeout ) {
196
+ $ data ['timeout ' ] = $ this ->timeout ;
197
+ }
198
+
199
+ if ($ this ->includedJsScripts ) {
200
+ $ data ['includedJsScripts ' ] = $ this ->includedJsScripts ;
201
+ }
202
+
203
+ if ($ this ->includedJsSnippets ) {
204
+ $ data ['includedJsSnippets ' ] = $ this ->includedJsSnippets ;
156
205
}
157
206
158
- if ($ deleteFileIfExists && file_exists ($ outputPath ) && is_writable ($ outputPath )) {
159
- unlink ($ outputPath );
207
+ if ($ deleteFileIfExists && file_exists ($ this -> imageLocation ) && is_writable ($ this -> imageLocation )) {
208
+ unlink ($ this -> imageLocation );
160
209
}
161
210
162
211
$ jobName = md5 (json_encode ($ data ));
@@ -171,14 +220,14 @@ public function save($imageLocation, $deleteFileIfExists = true)
171
220
$ command = sprintf ("%sphantomjs %s " , $ this ->binPath , $ jobPath );
172
221
$ result = exec (escapeshellcmd ($ command ));
173
222
174
- return file_exists ($ outputPath );
223
+ return file_exists ($ this -> imageLocation );
175
224
}
176
225
177
226
private function getTemplateResult ($ templateName , array $ args )
178
227
{
179
228
$ templatePath = $ this ->templatePath . DIRECTORY_SEPARATOR . $ templateName . '.php ' ;
180
229
if (!file_exists ($ templatePath )) {
181
- throw new \ Exception ( " The template { $ templateName} does not exist! " );
230
+ throw new TemplateNotFoundException ( $ templateName );
182
231
}
183
232
ob_start ();
184
233
extract ($ args );
@@ -210,19 +259,7 @@ public function setBinPath($binPath)
210
259
*/
211
260
public function setUrl ($ url )
212
261
{
213
- // Prepend http:// if the url doesn't contain it
214
- if (!stristr ($ url , 'http:// ' ) && !stristr ($ url , 'https:// ' )) {
215
- $ url = 'http:// ' . $ url ;
216
- }
217
-
218
- if (!$ url || !filter_var ($ url , FILTER_VALIDATE_URL )) {
219
- throw new \Exception ("Invalid URL " );
220
- }
221
-
222
- $ url = str_replace (array ('; ' , '" ' , '<? ' ), '' , strip_tags ($ url ));
223
- $ url = str_replace (array ('\077 ' , '\'' ), array (' ' , '/ ' ), $ url );
224
-
225
- $ this ->url = $ url ;
262
+ $ this ->url = new Url ($ url );
226
263
}
227
264
228
265
/**
@@ -296,49 +333,37 @@ public function setBackgroundColor($backgroundColor)
296
333
}
297
334
298
335
/**
299
- * Sets the image format
336
+ * Sets the image type
300
337
*
301
- * @param string $format 'jpg' | 'png'
338
+ * @param string $type 'jpg', 'png', etc...
302
339
*
303
340
* @return Capture
304
341
*/
305
- public function setFormat ( $ format )
342
+ public function setImageType ( $ type )
306
343
{
307
- $ format = strtolower ($ format );
308
- if (!in_array ($ format , ['jpg ' , 'png ' ])) {
309
- throw new Exception (
310
- "Invalid image format ' {$ format }'. " .
311
- "Allowed formats are 'jpg' and 'png' "
312
- );
313
- }
314
-
315
- $ this ->format = $ format ;
344
+ $ this ->imageType = Types::getClass ($ type );
316
345
317
346
return $ this ;
318
347
}
319
348
320
349
/**
321
- * Gets the image format
350
+ * Returns the image type instance
322
351
*
323
- * @return string
352
+ * @return Type
324
353
*/
325
- public function getFormat ()
354
+ public function getImageType ()
326
355
{
327
- return $ this ->format ;
356
+ return $ this ->imageType ;
328
357
}
329
358
330
359
/**
331
- * Gets the MIME type of resulted image
360
+ * Returns the location where the screenshot file was written
332
361
*
333
362
* @return string
334
363
*/
335
- public function getMimeType ()
364
+ public function getImageLocation ()
336
365
{
337
- if ($ this ->format === 'png ' ) {
338
- return 'image/png ' ;
339
- }
340
-
341
- return 'image/jpeg ' ;
366
+ return $ this ->imageLocation ;
342
367
}
343
368
344
369
/**
@@ -357,12 +382,37 @@ public function setUserAgentString($userAgentString)
357
382
358
383
/**
359
384
* Sets the timeout period
360
- * @param int $timeout
361
- * @return $this
385
+ *
386
+ * @param int $timeout Timeout period
387
+ *
388
+ * @return Capture
389
+ * @throws InvalidArgumentException
362
390
*/
363
- public function setResourceTimeout ($ timeout = 30000 )
391
+ public function setTimeout ($ timeout )
364
392
{
365
- $ this ->resourceTimeout = $ timeout ;
393
+ if (!is_numeric ($ timeout )) {
394
+ throw new InvalidArgumentException ('The timeout value must be a number. ' );
395
+ }
396
+ $ this ->timeout = $ timeout ;
397
+
398
+ return $ this ;
399
+ }
400
+
401
+ /**
402
+ * Adds a JS script or snippet to the screen shot script
403
+ *
404
+ * @param string|URL $script Script to include
405
+ *
406
+ * @return Capture
407
+ */
408
+ public function includeJs ($ script )
409
+ {
410
+ if (is_a ($ script , Url::class)) {
411
+ $ this ->includedJsScripts [] = $ script ;
412
+ } else {
413
+ $ this ->includedJsSnippets [] = $ script ;
414
+ }
415
+
366
416
return $ this ;
367
417
}
368
418
}
0 commit comments