6
6
7
7
namespace Ilch ;
8
8
9
+ use Thumb \Thumbnail ;
10
+
9
11
/**
10
12
* Ilch/Upload class.
11
13
*/
@@ -77,7 +79,7 @@ public function __construct()
77
79
/**
78
80
* Resets
79
81
*/
80
- public function reset ()
82
+ public function reset (): Upload
81
83
{
82
84
$ this ->file = null ;
83
85
$ this ->ending = null ;
@@ -94,9 +96,9 @@ public function reset()
94
96
/**
95
97
* @param string $file
96
98
*
97
- * @return string File
99
+ * @return Upload File
98
100
*/
99
- public function setFile ($ file )
101
+ public function setFile (string $ file ): Upload
100
102
{
101
103
$ this ->file = $ file ;
102
104
@@ -106,17 +108,17 @@ public function setFile($file)
106
108
/**
107
109
* @return string
108
110
*/
109
- public function getFile ()
111
+ public function getFile (): string
110
112
{
111
113
return $ this ->file ;
112
114
}
113
115
114
116
/**
115
117
* @param string $ending
116
118
*
117
- * @return string Ending
119
+ * @return Upload Ending
118
120
*/
119
- public function setEnding ($ ending )
121
+ public function setEnding (string $ ending ): Upload
120
122
{
121
123
$ this ->ending = $ ending ;
122
124
@@ -126,7 +128,7 @@ public function setEnding($ending)
126
128
/**
127
129
* @return string
128
130
*/
129
- public function getEnding ()
131
+ public function getEnding (): string
130
132
{
131
133
$ this ->ending = strtolower (pathinfo ($ this ->file , PATHINFO_EXTENSION ));
132
134
@@ -136,9 +138,9 @@ public function getEnding()
136
138
/**
137
139
* @param string $name
138
140
*
139
- * @return string Name
141
+ * @return Upload Name
140
142
*/
141
- public function setName ($ name )
143
+ public function setName (string $ name ): Upload
142
144
{
143
145
$ this ->name = $ name ;
144
146
@@ -148,7 +150,7 @@ public function setName($name)
148
150
/**
149
151
* @return string
150
152
*/
151
- public function getName ()
153
+ public function getName (): string
152
154
{
153
155
$ this ->name = pathinfo ($ this ->file , PATHINFO_FILENAME );
154
156
@@ -158,9 +160,9 @@ public function getName()
158
160
/**
159
161
* @param string $fileName
160
162
*
161
- * @return string fileName
163
+ * @return Upload fileName
162
164
*/
163
- public function setFileName ($ fileName )
165
+ public function setFileName (string $ fileName ): Upload
164
166
{
165
167
$ this ->fileName = $ fileName ;
166
168
@@ -170,17 +172,17 @@ public function setFileName($fileName)
170
172
/**
171
173
* @return string
172
174
*/
173
- public function getFileName ()
175
+ public function getFileName (): string
174
176
{
175
177
return $ this ->fileName ;
176
178
}
177
179
178
180
/**
179
181
* @param string $url
180
182
*
181
- * @return string url
183
+ * @return Upload url
182
184
*/
183
- public function setUrl ($ url )
185
+ public function setUrl (string $ url ): Upload
184
186
{
185
187
$ this ->url = $ url ;
186
188
@@ -190,17 +192,17 @@ public function setUrl($url)
190
192
/**
191
193
* @return string
192
194
*/
193
- public function getUrl ()
195
+ public function getUrl (): string
194
196
{
195
197
return $ this ->url ;
196
198
}
197
199
198
200
/**
199
201
* @param string $types
200
202
*
201
- * @return string types
203
+ * @return Upload types
202
204
*/
203
- public function setTypes ($ types )
205
+ public function setTypes (string $ types ): Upload
204
206
{
205
207
$ this ->types = $ types ;
206
208
@@ -210,17 +212,17 @@ public function setTypes($types)
210
212
/**
211
213
* @return string
212
214
*/
213
- public function getTypes ()
215
+ public function getTypes (): string
214
216
{
215
217
return $ this ->types ;
216
218
}
217
219
218
220
/**
219
221
* @param string $allowedExtensions
220
222
*
221
- * @return string allowedExtensions
223
+ * @return Upload allowedExtensions
222
224
*/
223
- public function setAllowedExtensions ($ allowedExtensions )
225
+ public function setAllowedExtensions (string $ allowedExtensions ): Upload
224
226
{
225
227
$ this ->allowedExtensions = $ allowedExtensions ;
226
228
@@ -230,17 +232,17 @@ public function setAllowedExtensions($allowedExtensions)
230
232
/**
231
233
* @return string
232
234
*/
233
- public function getAllowedExtensions ()
235
+ public function getAllowedExtensions (): string
234
236
{
235
237
return $ this ->allowedExtensions ;
236
238
}
237
239
238
240
/**
239
241
* @param string $path
240
242
*
241
- * @return string path
243
+ * @return Upload path
242
244
*/
243
- public function setPath ($ path )
245
+ public function setPath (string $ path ): Upload
244
246
{
245
247
$ this ->path = $ path ;
246
248
@@ -250,17 +252,17 @@ public function setPath($path)
250
252
/**
251
253
* @return string
252
254
*/
253
- public function getPath ()
255
+ public function getPath (): string
254
256
{
255
257
return $ this ->path ;
256
258
}
257
259
258
260
/**
259
261
* @param string $urlThumb
260
262
*
261
- * @return string urlThumb
263
+ * @return Upload urlThumb
262
264
*/
263
- public function setUrlThumb ($ urlThumb )
265
+ public function setUrlThumb (string $ urlThumb ): Upload
264
266
{
265
267
$ this ->urlThumb = $ urlThumb ;
266
268
@@ -270,63 +272,77 @@ public function setUrlThumb($urlThumb)
270
272
/**
271
273
* @return string
272
274
*/
273
- public function getUrlThumb ()
275
+ public function getUrlThumb (): string
274
276
{
275
277
return $ this ->urlThumb ;
276
278
}
277
279
278
280
/**
281
+ * Get size of the file in a human readable form.
282
+ *
279
283
* @return string
284
+ * @since 2.0.0
280
285
*/
281
- public function getSize ()
286
+ public function getSize (): string
282
287
{
283
288
$ bytes = sprintf ('%u ' , filesize ($ this ->file ));
284
289
285
290
if ($ bytes > 0 ) {
286
- $ unit = (int )log ($ bytes , 1024 );
287
- $ units = ['B ' , 'KB ' , 'MB ' , 'GB ' ];
288
-
289
- if (array_key_exists ($ unit , $ units ) === true ) {
290
- return sprintf ('%d %s ' , $ bytes / pow (1024 , $ unit ), $ units [$ unit ]);
291
- }
291
+ return formatBytes ($ bytes );
292
292
}
293
293
294
294
return $ bytes ;
295
295
}
296
296
297
297
/**
298
298
* Take an educated guess on how big the image is going to be in memory.
299
+ *
299
300
* @param string $imageFilePath
300
- * @return int required memory in bytes
301
+ * @return int|false required memory in bytes or false in case of an error.
302
+ * @since 2.0.0
303
+ * @since 2.1.54 returns false in case of an error.
301
304
*/
302
- public function guessRequiredMemory ($ imageFilePath )
305
+ public function guessRequiredMemory (string $ imageFilePath )
303
306
{
304
307
$ imageInfo = getimagesize ($ imageFilePath );
308
+
309
+ if ($ imageInfo === false ) {
310
+ return false ;
311
+ }
312
+
305
313
if (empty ($ imageInfo ['channels ' ])) {
306
314
$ imageInfo ['channels ' ] = 4 ;
307
315
}
308
316
// (width * height * bits / 8) * channels * tweak-factor
309
317
// channels will be 3 for RGB pictures and 4 for CMYK pictures
310
318
// bits is the number of bits for each color.
311
319
// The tweak-factor might be overly careful and could therefore be lowered if necessary.
320
+ // After testing with >10k pictures the tweak factor got lowered to 1.9 (90 %) from 2.5 (150 %)
321
+ // With a tweak factor of 1.9 there were still no out of memory errors.
312
322
return ($ imageInfo [0 ] * $ imageInfo [1 ] * ($ imageInfo ['bits ' ] / 8 ) * $ imageInfo ['channels ' ] * 1.9 );
313
323
}
314
324
315
325
/**
316
326
* Returns if there would likely be enough free memory for the image.
317
327
* Returns true in case of memory_limit = -1 e.g. no memory limit.
328
+ * Returns false if not an image or an error occured.
318
329
*
319
330
* @param string $imageFilePath
320
331
* @return bool
321
332
* @since 2.1.20
333
+ * @since 2.1.54 Returns false if not an image or an error occured.
322
334
*/
323
- public function enoughFreeMemory ($ imageFilePath )
335
+ public function enoughFreeMemory (string $ imageFilePath ): bool
324
336
{
325
337
$ memoryLimit = ini_get ('memory_limit ' );
326
338
if ($ memoryLimit == '-1 ' ) {
327
339
return true ;
328
340
}
329
341
342
+ if (getimagesize ($ imageFilePath ) === false ) {
343
+ return false ;
344
+ }
345
+
330
346
if (($ this ->returnBytes ($ memoryLimit ) - memory_get_usage (true )) < $ this ->guessRequiredMemory ($ imageFilePath )) {
331
347
return false ;
332
348
}
@@ -356,18 +372,19 @@ public function upload()
356
372
*
357
373
* @return bool
358
374
*/
359
- public function isAllowedExtension ()
375
+ public function isAllowedExtension (): bool
360
376
{
361
377
return in_array ($ this ->getEnding (), explode (' ' , $ this ->getAllowedExtensions ()));
362
378
}
363
379
364
380
/**
365
381
* Convert for example the memory_limit of php (example: 128M) to bytes.
366
382
*
367
- * @param $size_str
383
+ * @param string $size_str
368
384
* @return float|int
385
+ * @since 2.0.0
369
386
*/
370
- public function returnBytes ($ size_str )
387
+ public function returnBytes (string $ size_str )
371
388
{
372
389
switch (substr ($ size_str , -1 )) {
373
390
case 'M ' :
@@ -406,14 +423,15 @@ public function save()
406
423
* Create thumbnail
407
424
*
408
425
* @return bool
426
+ * @since 2.1.20
409
427
*/
410
- public function createThumbnail ()
428
+ public function createThumbnail (): bool
411
429
{
412
430
if (!$ this ->enoughFreeMemory ($ this ->getUrl ())) {
413
431
return false ;
414
432
}
415
433
416
- $ thumb = new \ Thumb \ Thumbnail ();
434
+ $ thumb = new Thumbnail ();
417
435
$ thumb ->Thumbprefix = 'thumb_ ' ;
418
436
$ thumb ->Thumblocation = $ this ->path ;
419
437
$ thumb ->Thumbsize = 300 ;
0 commit comments