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 ;
10
5
use Screen \Location \Jobs ;
11
6
use Screen \Location \Output ;
12
7
@@ -62,11 +57,11 @@ class Capture
62
57
protected $ backgroundColor = '' ;
63
58
64
59
/**
65
- * Image Type, default is jpeg
60
+ * Image format
66
61
*
67
- * @var Type
62
+ * @var string
68
63
*/
69
- protected $ imageType ;
64
+ protected $ format = ' jpg ' ;
70
65
71
66
/**
72
67
* User Agent String used on the page request
@@ -103,27 +98,6 @@ class Capture
103
98
*/
104
99
public $ output ;
105
100
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
-
127
101
/**
128
102
* Capture constructor.
129
103
*/
@@ -138,31 +112,18 @@ public function __construct($url = null)
138
112
139
113
$ this ->jobs = new Jobs ();
140
114
$ this ->output = new Output ();
141
-
142
- $ this ->setImageType (Types \Jpg::FORMAT );
143
115
}
144
116
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
- */
153
117
public function save ($ imageLocation , $ deleteFileIfExists = true )
154
118
{
155
- $ this ->imageLocation = $ this ->output ->getLocation () . $ imageLocation ;
156
-
157
- if (!pathinfo ($ this ->imageLocation , PATHINFO_EXTENSION )) {
158
- $ this ->imageLocation .= '. ' . $ this ->getImageType ()->getFormat ();
159
- }
119
+ $ outputPath = $ this ->output ->getLocation () . $ imageLocation ;
160
120
161
121
$ data = array (
162
122
'url ' => $ this ->url ,
163
123
'width ' => $ this ->width ,
164
124
'height ' => $ this ->height ,
165
- 'imageLocation ' => LocalPath::sanitize ($ this ->imageLocation ),
125
+ // If used on windows the \ char needs to be handled to be used on a js string
126
+ 'imageLocation ' => str_replace ("\\" , "\\\\" , $ outputPath ),
166
127
);
167
128
168
129
if ($ this ->clipWidth && $ this ->clipHeight ) {
@@ -174,7 +135,7 @@ public function save($imageLocation, $deleteFileIfExists = true)
174
135
175
136
if ($ this ->backgroundColor ) {
176
137
$ data ['backgroundColor ' ] = $ this ->backgroundColor ;
177
- } elseif ($ this ->getImageType ()-> getFormat () == Types \Jpg:: FORMAT ) {
138
+ } elseif ($ this ->getFormat () == ' jpg ' ) {
178
139
// If there is no background color set, and it's a jpeg
179
140
// we need to set a bg color, otherwise the background will be black
180
141
$ data ['backgroundColor ' ] = '#FFFFFF ' ;
@@ -184,16 +145,8 @@ public function save($imageLocation, $deleteFileIfExists = true)
184
145
$ data ['userAgent ' ] = $ this ->userAgentString ;
185
146
}
186
147
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 );
148
+ if ($ deleteFileIfExists && file_exists ($ outputPath ) && is_writable ($ outputPath )) {
149
+ unlink ($ outputPath );
197
150
}
198
151
199
152
$ jobName = md5 (json_encode ($ data ));
@@ -208,14 +161,14 @@ public function save($imageLocation, $deleteFileIfExists = true)
208
161
$ command = sprintf ("%sphantomjs %s " , $ this ->binPath , $ jobPath );
209
162
$ result = exec (escapeshellcmd ($ command ));
210
163
211
- return file_exists ($ this -> imageLocation );
164
+ return file_exists ($ outputPath );
212
165
}
213
166
214
167
private function getTemplateResult ($ templateName , array $ args )
215
168
{
216
169
$ templatePath = $ this ->templatePath . DIRECTORY_SEPARATOR . $ templateName . '.php ' ;
217
170
if (!file_exists ($ templatePath )) {
218
- throw new TemplateNotFoundException ( $ templateName );
171
+ throw new \ Exception ( " The template { $ templateName} does not exist! " );
219
172
}
220
173
ob_start ();
221
174
extract ($ args );
@@ -247,7 +200,19 @@ public function setBinPath($binPath)
247
200
*/
248
201
public function setUrl ($ url )
249
202
{
250
- $ this ->url = new Url ($ url );
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 ;
251
216
}
252
217
253
218
/**
@@ -321,37 +286,49 @@ public function setBackgroundColor($backgroundColor)
321
286
}
322
287
323
288
/**
324
- * Sets the image type
289
+ * Sets the image format
325
290
*
326
- * @param string $type 'jpg', 'png', etc...
291
+ * @param string $format 'jpg' | 'png'
327
292
*
328
293
* @return Capture
329
294
*/
330
- public function setImageType ( $ type )
295
+ public function setFormat ( $ format )
331
296
{
332
- $ this ->imageType = Types::getClass ($ type );
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 ;
333
306
334
307
return $ this ;
335
308
}
336
309
337
310
/**
338
- * Returns the image type instance
311
+ * Gets the image format
339
312
*
340
- * @return Type
313
+ * @return string
341
314
*/
342
- public function getImageType ()
315
+ public function getFormat ()
343
316
{
344
- return $ this ->imageType ;
317
+ return $ this ->format ;
345
318
}
346
319
347
320
/**
348
- * Returns the location where the screenshot file was written
321
+ * Gets the MIME type of resulted image
349
322
*
350
323
* @return string
351
324
*/
352
- public function getImageLocation ()
325
+ public function getMimeType ()
353
326
{
354
- return $ this ->imageLocation ;
327
+ if ($ this ->format === 'png ' ) {
328
+ return 'image/png ' ;
329
+ }
330
+
331
+ return 'image/jpeg ' ;
355
332
}
356
333
357
334
/**
@@ -367,22 +344,4 @@ public function setUserAgentString($userAgentString)
367
344
368
345
return $ this ;
369
346
}
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
- }
388
347
}
0 commit comments