• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / file-upload-control / 4581408359

pending completion
4581408359

Pull #103

github

GitHub
Merge ed661242b into c8546f895
Pull Request #103: Update nepada/coding-standard requirement from 7.6.0 to 7.7.0

641 of 710 relevant lines covered (90.28%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/FileUploadControl/Thumbnail/ImageThumbnailProvider.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\FileUploadControl\Thumbnail;
5

6
use Nette;
7
use Nette\Http\FileUpload;
8
use Nette\Utils\Image;
9
use Nette\Utils\Strings;
10

11
final class ImageThumbnailProvider implements ThumbnailProvider
12
{
13

14
    use Nette\SmartObject;
15

16
    public const DEFAULT_WIDTH = 200;
17
    public const DEFAULT_HEIGHT = 150;
18

19
    private ImageLoader $imageLoader;
20

21
    private int $width;
22

23
    private int $height;
24

25
    public function __construct(ImageLoader $imageLoader, int $width = self::DEFAULT_WIDTH, int $height = self::DEFAULT_HEIGHT)
1✔
26
    {
27
        $this->imageLoader = $imageLoader;
1✔
28
        $this->width = $width;
1✔
29
        $this->height = $height;
1✔
30
    }
1✔
31

32
    public function isSupported(FileUpload $fileUpload): bool
1✔
33
    {
34
        return $fileUpload->isImage();
1✔
35
    }
36

37
    public function createThumbnail(FileUpload $fileUpload): ThumbnailResponse
1✔
38
    {
39
        if (! $this->isSupported($fileUpload)) {
1✔
40
            throw new \InvalidArgumentException('Only image file uploads are supported.');
1✔
41
        }
42

43
        $image = $this->imageLoader->load($fileUpload->getTemporaryFile());
1✔
44
        $image->resize($this->width, $this->height, Image::SHRINK_ONLY);
1✔
45

46
        $name = $fileUpload->getUntrustedName();
1✔
47
        if (in_array($fileUpload->getContentType(), ['image/gif', 'image/png'], true)) {
1✔
48
            $contents = $image->toString(Image::PNG);
1✔
49
            $contentType = 'image/png';
1✔
50
            if (! Strings::endsWith($name, '.png')) {
1✔
51
                $name .= '.png';
1✔
52
            }
53

54
        } else {
55
            $contents = $image->toString(Image::JPEG);
1✔
56
            $contentType = 'image/jpeg';
1✔
57
            if (! Strings::endsWith($name, '.jpg')) {
1✔
58
                $name .= '.jpg';
×
59
            }
60
        }
61

62
        return new ThumbnailResponse($contents, $name, $contentType);
1✔
63
    }
64

65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc