2
2
3
3
namespace Screen ;
4
4
5
+ use Screen \Exceptions \TemplateNotFoundException ;
6
+ use Screen \Image \Types ;
7
+ use Screen \Image \Types \Type ;
8
+ use Screen \Injection \LocalPath ;
9
+ use Screen \Injection \Url ;
5
10
use Screen \Location \Jobs ;
6
11
use Screen \Location \Output ;
7
12
@@ -57,11 +62,11 @@ class Capture
57
62
protected $ backgroundColor = '' ;
58
63
59
64
/**
60
- * Image format
65
+ * Image Type, default is jpeg
61
66
*
62
- * @var string
67
+ * @var Type
63
68
*/
64
- protected $ format = ' jpg ' ;
69
+ protected $ imageType ;
65
70
66
71
/**
67
72
* User Agent String used on the page request
@@ -98,6 +103,27 @@ class Capture
98
103
*/
99
104
public $ output ;
100
105
106
+ /**
107
+ * Location where the file was written to
108
+ *
109
+ * @var string
110
+ */
111
+ protected $ imageLocation ;
112
+
113
+ /**
114
+ * List of included JS scripts
115
+ *
116
+ * @var array
117
+ */
118
+ protected $ includedJsScripts = array ();
119
+
120
+ /**
121
+ * List of included JS snippets
122
+ *
123
+ * @var array
124
+ */
125
+ protected $ includedJsSnippets = array ();
126
+
101
127
/**
102
128
* Capture constructor.
103
129
*/
@@ -112,18 +138,31 @@ public function __construct($url = null)
112
138
113
139
$ this ->jobs = new Jobs ();
114
140
$ this ->output = new Output ();
141
+
142
+ $ this ->setImageType (Types \Jpg::FORMAT );
115
143
}
116
144
145
+ /**
146
+ * Saves the screenshot to the requested location
147
+ *
148
+ * @param string $imageLocation Image Location
149
+ * @param bool $deleteFileIfExists True to delete the file if it exists
150
+ *
151
+ * @return bool
152
+ */
117
153
public function save ($ imageLocation , $ deleteFileIfExists = true )
118
154
{
119
- $ outputPath = $ this ->output ->getLocation () . $ imageLocation ;
155
+ $ this ->imageLocation = $ this ->output ->getLocation () . $ imageLocation ;
156
+
157
+ if (!pathinfo ($ this ->imageLocation , PATHINFO_EXTENSION )) {
158
+ $ this ->imageLocation .= '. ' . $ this ->getImageType ()->getFormat ();
159
+ }
120
160
121
161
$ data = array (
122
162
'url ' => $ this ->url ,
123
163
'width ' => $ this ->width ,
124
164
'height ' => $ this ->height ,
125
- // If used on windows the \ char needs to be handled to be used on a js string
126
- 'imageLocation ' => str_replace ("\\" , "\\\\" , $ outputPath ),
165
+ 'imageLocation ' => LocalPath::sanitize ($ this ->imageLocation ),
127
166
);
128
167
129
168
if ($ this ->clipWidth && $ this ->clipHeight ) {
@@ -135,7 +174,7 @@ public function save($imageLocation, $deleteFileIfExists = true)
135
174
136
175
if ($ this ->backgroundColor ) {
137
176
$ data ['backgroundColor ' ] = $ this ->backgroundColor ;
138
- } elseif ($ this ->getFormat () == ' jpg ' ) {
177
+ } elseif ($ this ->getImageType ()-> getFormat () == Types \Jpg:: FORMAT ) {
139
178
// If there is no background color set, and it's a jpeg
140
179
// we need to set a bg color, otherwise the background will be black
141
180
$ data ['backgroundColor ' ] = '#FFFFFF ' ;
@@ -145,8 +184,16 @@ public function save($imageLocation, $deleteFileIfExists = true)
145
184
$ data ['userAgent ' ] = $ this ->userAgentString ;
146
185
}
147
186
148
- if ($ deleteFileIfExists && file_exists ($ outputPath ) && is_writable ($ outputPath )) {
149
- unlink ($ outputPath );
187
+ if ($ this ->includedJsScripts ) {
188
+ $ data ['includedJsScripts ' ] = $ this ->includedJsScripts ;
189
+ }
190
+
191
+ if ($ this ->includedJsSnippets ) {
192
+ $ data ['includedJsSnippets ' ] = $ this ->includedJsSnippets ;
193
+ }
194
+
195
+ if ($ deleteFileIfExists && file_exists ($ this ->imageLocation ) && is_writable ($ this ->imageLocation )) {
196
+ unlink ($ this ->imageLocation );
150
197
}
151
198
152
199
$ jobName = md5 (json_encode ($ data ));
@@ -161,14 +208,14 @@ public function save($imageLocation, $deleteFileIfExists = true)
161
208
$ command = sprintf ("%sphantomjs %s " , $ this ->binPath , $ jobPath );
162
209
$ result = exec (escapeshellcmd ($ command ));
163
210
164
- return file_exists ($ outputPath );
211
+ return file_exists ($ this -> imageLocation );
165
212
}
166
213
167
214
private function getTemplateResult ($ templateName , array $ args )
168
215
{
169
216
$ templatePath = $ this ->templatePath . DIRECTORY_SEPARATOR . $ templateName . '.php ' ;
170
217
if (!file_exists ($ templatePath )) {
171
- throw new \ Exception ( " The template { $ templateName} does not exist! " );
218
+ throw new TemplateNotFoundException ( $ templateName );
172
219
}
173
220
ob_start ();
174
221
extract ($ args );
@@ -200,19 +247,7 @@ public function setBinPath($binPath)
200
247
*/
201
248
public function setUrl ($ url )
202
249
{
203
- // Prepend http:// if the url doesn't contain it
204
- if (!stristr ($ url , 'http:// ' ) && !stristr ($ url , 'https:// ' )) {
205
- $ url = 'http:// ' . $ url ;
206
- }
207
-
208
- if (!$ url || !filter_var ($ url , FILTER_VALIDATE_URL )) {
209
- throw new \Exception ("Invalid URL " );
210
- }
211
-
212
- $ url = str_replace (array ('; ' , '" ' , '<? ' ), '' , strip_tags ($ url ));
213
- $ url = str_replace (array ('\077 ' , '\'' ), array (' ' , '/ ' ), $ url );
214
-
215
- $ this ->url = $ url ;
250
+ $ this ->url = new Url ($ url );
216
251
}
217
252
218
253
/**
@@ -286,49 +321,37 @@ public function setBackgroundColor($backgroundColor)
286
321
}
287
322
288
323
/**
289
- * Sets the image format
324
+ * Sets the image type
290
325
*
291
- * @param string $format 'jpg' | 'png'
326
+ * @param string $type 'jpg', 'png', etc...
292
327
*
293
328
* @return Capture
294
329
*/
295
- public function setFormat ( $ format )
330
+ public function setImageType ( $ type )
296
331
{
297
- $ format = strtolower ($ format );
298
- if (!in_array ($ format , ['jpg ' , 'png ' ])) {
299
- throw new Exception (
300
- "Invalid image format ' {$ format }'. " .
301
- "Allowed formats are 'jpg' and 'png' "
302
- );
303
- }
304
-
305
- $ this ->format = $ format ;
332
+ $ this ->imageType = Types::getClass ($ type );
306
333
307
334
return $ this ;
308
335
}
309
336
310
337
/**
311
- * Gets the image format
338
+ * Returns the image type instance
312
339
*
313
- * @return string
340
+ * @return Type
314
341
*/
315
- public function getFormat ()
342
+ public function getImageType ()
316
343
{
317
- return $ this ->format ;
344
+ return $ this ->imageType ;
318
345
}
319
346
320
347
/**
321
- * Gets the MIME type of resulted image
348
+ * Returns the location where the screenshot file was written
322
349
*
323
350
* @return string
324
351
*/
325
- public function getMimeType ()
352
+ public function getImageLocation ()
326
353
{
327
- if ($ this ->format === 'png ' ) {
328
- return 'image/png ' ;
329
- }
330
-
331
- return 'image/jpeg ' ;
354
+ return $ this ->imageLocation ;
332
355
}
333
356
334
357
/**
@@ -344,4 +367,22 @@ public function setUserAgentString($userAgentString)
344
367
345
368
return $ this ;
346
369
}
370
+
371
+ /**
372
+ * Adds a JS script or snippet to the screen shot script
373
+ *
374
+ * @param string|URL $script Script to include
375
+ *
376
+ * @return Capture
377
+ */
378
+ public function includeJs ($ script )
379
+ {
380
+ if (is_a ($ script , Url::class)) {
381
+ $ this ->includedJsScripts [] = $ script ;
382
+ } else {
383
+ $ this ->includedJsSnippets [] = $ script ;
384
+ }
385
+
386
+ return $ this ;
387
+ }
347
388
}
0 commit comments