Skip to content

Commit 95276f8

Browse files
authored
Fix errors handling non-image file (#807)
Don't try to create a thumbnail if getimagesize returns false. Either damaged image or no image at all.
1 parent 84db416 commit 95276f8

File tree

1 file changed

+61
-43
lines changed

1 file changed

+61
-43
lines changed

application/libraries/Ilch/Upload.php

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Ilch;
88

9+
use Thumb\Thumbnail;
10+
911
/**
1012
* Ilch/Upload class.
1113
*/
@@ -77,7 +79,7 @@ public function __construct()
7779
/**
7880
* Resets
7981
*/
80-
public function reset()
82+
public function reset(): Upload
8183
{
8284
$this->file = null;
8385
$this->ending = null;
@@ -94,9 +96,9 @@ public function reset()
9496
/**
9597
* @param string $file
9698
*
97-
* @return string File
99+
* @return Upload File
98100
*/
99-
public function setFile($file)
101+
public function setFile(string $file): Upload
100102
{
101103
$this->file = $file;
102104

@@ -106,17 +108,17 @@ public function setFile($file)
106108
/**
107109
* @return string
108110
*/
109-
public function getFile()
111+
public function getFile(): string
110112
{
111113
return $this->file;
112114
}
113115

114116
/**
115117
* @param string $ending
116118
*
117-
* @return string Ending
119+
* @return Upload Ending
118120
*/
119-
public function setEnding($ending)
121+
public function setEnding(string $ending): Upload
120122
{
121123
$this->ending = $ending;
122124

@@ -126,7 +128,7 @@ public function setEnding($ending)
126128
/**
127129
* @return string
128130
*/
129-
public function getEnding()
131+
public function getEnding(): string
130132
{
131133
$this->ending = strtolower(pathinfo($this->file, PATHINFO_EXTENSION));
132134

@@ -136,9 +138,9 @@ public function getEnding()
136138
/**
137139
* @param string $name
138140
*
139-
* @return string Name
141+
* @return Upload Name
140142
*/
141-
public function setName($name)
143+
public function setName(string $name): Upload
142144
{
143145
$this->name = $name;
144146

@@ -148,7 +150,7 @@ public function setName($name)
148150
/**
149151
* @return string
150152
*/
151-
public function getName()
153+
public function getName(): string
152154
{
153155
$this->name = pathinfo($this->file, PATHINFO_FILENAME);
154156

@@ -158,9 +160,9 @@ public function getName()
158160
/**
159161
* @param string $fileName
160162
*
161-
* @return string fileName
163+
* @return Upload fileName
162164
*/
163-
public function setFileName($fileName)
165+
public function setFileName(string $fileName): Upload
164166
{
165167
$this->fileName = $fileName;
166168

@@ -170,17 +172,17 @@ public function setFileName($fileName)
170172
/**
171173
* @return string
172174
*/
173-
public function getFileName()
175+
public function getFileName(): string
174176
{
175177
return $this->fileName;
176178
}
177179

178180
/**
179181
* @param string $url
180182
*
181-
* @return string url
183+
* @return Upload url
182184
*/
183-
public function setUrl($url)
185+
public function setUrl(string $url): Upload
184186
{
185187
$this->url = $url;
186188

@@ -190,17 +192,17 @@ public function setUrl($url)
190192
/**
191193
* @return string
192194
*/
193-
public function getUrl()
195+
public function getUrl(): string
194196
{
195197
return $this->url;
196198
}
197199

198200
/**
199201
* @param string $types
200202
*
201-
* @return string types
203+
* @return Upload types
202204
*/
203-
public function setTypes($types)
205+
public function setTypes(string $types): Upload
204206
{
205207
$this->types = $types;
206208

@@ -210,17 +212,17 @@ public function setTypes($types)
210212
/**
211213
* @return string
212214
*/
213-
public function getTypes()
215+
public function getTypes(): string
214216
{
215217
return $this->types;
216218
}
217219

218220
/**
219221
* @param string $allowedExtensions
220222
*
221-
* @return string allowedExtensions
223+
* @return Upload allowedExtensions
222224
*/
223-
public function setAllowedExtensions($allowedExtensions)
225+
public function setAllowedExtensions(string $allowedExtensions): Upload
224226
{
225227
$this->allowedExtensions = $allowedExtensions;
226228

@@ -230,17 +232,17 @@ public function setAllowedExtensions($allowedExtensions)
230232
/**
231233
* @return string
232234
*/
233-
public function getAllowedExtensions()
235+
public function getAllowedExtensions(): string
234236
{
235237
return $this->allowedExtensions;
236238
}
237239

238240
/**
239241
* @param string $path
240242
*
241-
* @return string path
243+
* @return Upload path
242244
*/
243-
public function setPath($path)
245+
public function setPath(string $path): Upload
244246
{
245247
$this->path = $path;
246248

@@ -250,17 +252,17 @@ public function setPath($path)
250252
/**
251253
* @return string
252254
*/
253-
public function getPath()
255+
public function getPath(): string
254256
{
255257
return $this->path;
256258
}
257259

258260
/**
259261
* @param string $urlThumb
260262
*
261-
* @return string urlThumb
263+
* @return Upload urlThumb
262264
*/
263-
public function setUrlThumb($urlThumb)
265+
public function setUrlThumb(string $urlThumb): Upload
264266
{
265267
$this->urlThumb = $urlThumb;
266268

@@ -270,63 +272,77 @@ public function setUrlThumb($urlThumb)
270272
/**
271273
* @return string
272274
*/
273-
public function getUrlThumb()
275+
public function getUrlThumb(): string
274276
{
275277
return $this->urlThumb;
276278
}
277279

278280
/**
281+
* Get size of the file in a human readable form.
282+
*
279283
* @return string
284+
* @since 2.0.0
280285
*/
281-
public function getSize()
286+
public function getSize(): string
282287
{
283288
$bytes = sprintf('%u', filesize($this->file));
284289

285290
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);
292292
}
293293

294294
return $bytes;
295295
}
296296

297297
/**
298298
* Take an educated guess on how big the image is going to be in memory.
299+
*
299300
* @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.
301304
*/
302-
public function guessRequiredMemory($imageFilePath)
305+
public function guessRequiredMemory(string $imageFilePath)
303306
{
304307
$imageInfo = getimagesize($imageFilePath);
308+
309+
if ($imageInfo === false) {
310+
return false;
311+
}
312+
305313
if (empty($imageInfo['channels'])) {
306314
$imageInfo['channels'] = 4;
307315
}
308316
// (width * height * bits / 8) * channels * tweak-factor
309317
// channels will be 3 for RGB pictures and 4 for CMYK pictures
310318
// bits is the number of bits for each color.
311319
// 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.
312322
return ($imageInfo[0] * $imageInfo[1] * ($imageInfo['bits'] / 8) * $imageInfo['channels'] * 1.9);
313323
}
314324

315325
/**
316326
* Returns if there would likely be enough free memory for the image.
317327
* Returns true in case of memory_limit = -1 e.g. no memory limit.
328+
* Returns false if not an image or an error occured.
318329
*
319330
* @param string $imageFilePath
320331
* @return bool
321332
* @since 2.1.20
333+
* @since 2.1.54 Returns false if not an image or an error occured.
322334
*/
323-
public function enoughFreeMemory($imageFilePath)
335+
public function enoughFreeMemory(string $imageFilePath): bool
324336
{
325337
$memoryLimit = ini_get('memory_limit');
326338
if ($memoryLimit == '-1') {
327339
return true;
328340
}
329341

342+
if (getimagesize($imageFilePath) === false) {
343+
return false;
344+
}
345+
330346
if (($this->returnBytes($memoryLimit) - memory_get_usage(true)) < $this->guessRequiredMemory($imageFilePath)) {
331347
return false;
332348
}
@@ -356,18 +372,19 @@ public function upload()
356372
*
357373
* @return bool
358374
*/
359-
public function isAllowedExtension()
375+
public function isAllowedExtension(): bool
360376
{
361377
return in_array($this->getEnding(), explode(' ', $this->getAllowedExtensions()));
362378
}
363379

364380
/**
365381
* Convert for example the memory_limit of php (example: 128M) to bytes.
366382
*
367-
* @param $size_str
383+
* @param string $size_str
368384
* @return float|int
385+
* @since 2.0.0
369386
*/
370-
public function returnBytes($size_str)
387+
public function returnBytes(string $size_str)
371388
{
372389
switch (substr($size_str, -1)) {
373390
case 'M':
@@ -406,14 +423,15 @@ public function save()
406423
* Create thumbnail
407424
*
408425
* @return bool
426+
* @since 2.1.20
409427
*/
410-
public function createThumbnail()
428+
public function createThumbnail(): bool
411429
{
412430
if (!$this->enoughFreeMemory($this->getUrl())) {
413431
return false;
414432
}
415433

416-
$thumb = new \Thumb\Thumbnail();
434+
$thumb = new Thumbnail();
417435
$thumb->Thumbprefix = 'thumb_';
418436
$thumb->Thumblocation = $this->path;
419437
$thumb->Thumbsize = 300;

0 commit comments

Comments
 (0)