Passed
Push — master ( 8eb6c3...8951af )
by Sebastian
08:56
created

ImageTrimmer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage ImageHelper
5
 * @see \AppUtils\ImageHelper\ImageTrimmer
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\ImageHelper;
11
12
use AppUtils\ImageHelper;
13
use AppUtils\ImageHelper_Exception;
14
use AppUtils\RGBAColor;
15
use AppUtils\RGBAColor\ColorFactory;
16
use GdImage;
17
18
/**
19
 * Specialized class used for trimming images.
20
 *
21
 * @package Application Utils
22
 * @subpackage ImageHelper
23
 * @author Sebastian Mordziol <[email protected]>
24
 */
25
class ImageTrimmer
26
{
27
    /**
28
     * @var resource|GdImage
29
     */
30
    private $sourceImage;
31
    private RGBAColor $trimColor;
32
    private ImageHelper $helper;
33
34
    /**
35
     * @param ImageHelper $helper
36
     * @param resource|GdImage $sourceImage
37
     * @param RGBAColor|null $trimColor
38
     * @throws ImageHelper_Exception
39
     */
40
    public function __construct(ImageHelper $helper, $sourceImage, ?RGBAColor $trimColor=null)
41
    {
42
        ImageHelper::requireResource($sourceImage);
43
44
        $this->helper = $helper;
45
        $this->sourceImage = $sourceImage;
46
        $this->trimColor = $this->detectTrimColor($trimColor);
47
    }
48
49
    private function detectTrimColor(?RGBAColor $trimColor) : RGBAColor
50
    {
51
        $targetColor = ColorFactory::createAuto($trimColor);
52
53
        if($targetColor !== null)
54
        {
55
            return $targetColor;
56
        }
57
58
        return $this->helper->getIndexedColors(
59
            $this->sourceImage,
0 ignored issues
show
Bug introduced by
It seems like $this->sourceImage can also be of type GdImage; however, parameter $img of AppUtils\ImageHelper::getIndexedColors() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            /** @scrutinizer ignore-type */ $this->sourceImage,
Loading history...
60
            (int)imagecolorat($this->sourceImage, 0, 0)
61
        );
62
    }
63
64
    /**
65
     * @return resource|GdImage|null
66
     */
67
    public function trim()
68
    {
69
        $img = imagecropauto(
70
            $this->sourceImage,
71
            IMG_CROP_THRESHOLD,
72
            0.1, // Does not work at all with 0.0
73
            imagecolorclosestalpha(
74
                $this->sourceImage,
75
                $this->trimColor->getRed()->get8Bit(),
76
                $this->trimColor->getGreen()->get8Bit(),
77
                $this->trimColor->getBlue()->get8Bit(),
78
                $this->trimColor->getAlpha()->get7Bit()
79
            )
80
        );
81
82
        if($img !== false) {
83
            return $img;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $img also could return the type true which is incompatible with the documented return type GdImage|null|resource.
Loading history...
84
        }
85
86
        return null;
87
    }
88
}
89