CropPolygonCommand::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 24
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 45
rs 9.536
1
<?php
2
3
namespace Jackal\ImageMerge\Command;
4
5
use Jackal\ImageMerge\Builder\ImageBuilder;
6
use Jackal\ImageMerge\Command\Options\MultiCoordinateCommandOption;
7
use Jackal\ImageMerge\Exception\InvalidColorException;
8
use Jackal\ImageMerge\Model\Color;
9
use Jackal\ImageMerge\Model\Image;
10
use Jackal\ImageMerge\Utils\ColorUtils;
11
12
/**
13
 * Class CropPolygonCommand
14
 * @package Jackal\ImageMerge\Command
15
 */
16
class CropPolygonCommand extends AbstractCommand
17
{
18
    /**
19
     * CropPolygonCommand constructor.
20
     * @param MultiCoordinateCommandOption|null $options
21
     */
22
    public function __construct(MultiCoordinateCommandOption $options = null)
23
    {
24
        parent::__construct($options);
25
    }
26
27
    /**
28
     * @param Image $image
29
     * @return Image
30
     * @throws InvalidColorException
31
     */
32
    public function execute(Image $image)
33
    {
34
        /** @var MultiCoordinateCommandOption $options */
35
        $options = $this->options;
36
37
        $builder = new ImageBuilder($image);
38
39
        // Setup the merge image from the source image with scaling
40
        $mergeImage = ImageCreateTrueColor($image->getWidth(), $image->getHeight());
41
        imagecopyresampled($mergeImage, $image->getResource(), 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), imagesx($image->getResource()), imagesy($image->getResource()));
0 ignored issues
show
Bug introduced by
It seems like $mergeImage can also be of type false; however, parameter $dst_image of imagecopyresampled() 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

41
        imagecopyresampled(/** @scrutinizer ignore-type */ $mergeImage, $image->getResource(), 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), imagesx($image->getResource()), imagesy($image->getResource()));
Loading history...
42
43
        $maskPolygon = imagecreatetruecolor($image->getWidth(), $image->getHeight());
44
        $borderColor = ColorUtils::colorIdentifier($maskPolygon, new Color('01feff'));
45
        imagefill($maskPolygon, 0, 0, $borderColor);
0 ignored issues
show
Bug introduced by
It seems like $maskPolygon can also be of type false; however, parameter $image of imagefill() 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

45
        imagefill(/** @scrutinizer ignore-type */ $maskPolygon, 0, 0, $borderColor);
Loading history...
46
47
        // Add the transparent polygon mask
48
        $transparency = imagecolortransparent($maskPolygon, ColorUtils::colorIdentifier($maskPolygon, new Color('ff01fe')));
0 ignored issues
show
Bug introduced by
It seems like $maskPolygon can also be of type false; however, parameter $image of imagecolortransparent() 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

48
        $transparency = imagecolortransparent(/** @scrutinizer ignore-type */ $maskPolygon, ColorUtils::colorIdentifier($maskPolygon, new Color('ff01fe')));
Loading history...
49
        imagesavealpha($maskPolygon, true);
0 ignored issues
show
Bug introduced by
It seems like $maskPolygon can also be of type false; however, parameter $image of imagesavealpha() 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

49
        imagesavealpha(/** @scrutinizer ignore-type */ $maskPolygon, true);
Loading history...
50
        imagefilledpolygon($maskPolygon, $options->toArray(), $options->countPoints(), $transparency);
0 ignored issues
show
Bug introduced by
It seems like $maskPolygon can also be of type false; however, parameter $image of imagefilledpolygon() 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

50
        imagefilledpolygon(/** @scrutinizer ignore-type */ $maskPolygon, $options->toArray(), $options->countPoints(), $transparency);
Loading history...
51
52
        // Apply the mask
53
        imagesavealpha($mergeImage, true);
54
        imagecopymerge($mergeImage, $maskPolygon, 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), 100);
0 ignored issues
show
Bug introduced by
It seems like $mergeImage can also be of type false; however, parameter $dst_im of imagecopymerge() 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

54
        imagecopymerge(/** @scrutinizer ignore-type */ $mergeImage, $maskPolygon, 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), 100);
Loading history...
Bug introduced by
It seems like $maskPolygon can also be of type false; however, parameter $src_im of imagecopymerge() 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

54
        imagecopymerge($mergeImage, /** @scrutinizer ignore-type */ $maskPolygon, 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), 100);
Loading history...
55
56
        // Create the final image
57
        $destImage = ImageCreateTrueColor($image->getWidth(), $image->getHeight());
58
        imagesavealpha($destImage, true);
59
        imagealphablending($destImage, true);
0 ignored issues
show
Bug introduced by
It seems like $destImage can also be of type false; however, parameter $image of imagealphablending() 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
        imagealphablending(/** @scrutinizer ignore-type */ $destImage, true);
Loading history...
60
        imagecopy($destImage, $mergeImage,
0 ignored issues
show
Bug introduced by
It seems like $mergeImage can also be of type false; however, parameter $src_im of imagecopy() 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

60
        imagecopy($destImage, /** @scrutinizer ignore-type */ $mergeImage,
Loading history...
Bug introduced by
It seems like $destImage can also be of type false; however, parameter $dst_im of imagecopy() 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

60
        imagecopy(/** @scrutinizer ignore-type */ $destImage, $mergeImage,
Loading history...
61
            0, 0,
62
            0, 0,
63
            $image->getWidth(), $image->getHeight());
64
65
        // Make the the border transparent (we're assuming there's a 2px buffer on all sides)
66
67
        $borderTransparency = ColorUtils::colorIdentifier($destImage, new Color('ff01fe'), true);
68
        imagesavealpha($destImage, true);
69
        imagealphablending($destImage, true);
70
        imagefill($destImage, 0, 0, $borderTransparency);
71
72
        $image->assignResource($destImage);
73
74
        $builder->crop($options->getMinX(), $options->getMinY(), $options->getCropDimention()->getWidth(), $options->getCropDimention()->getHeight());
75
76
        return $image;
77
    }
78
}
79