Image::getHeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Image\Adapter\Gd;
4
5
use AmaTeam\Image\Projection\API\Image\ImageInterface;
6
use AmaTeam\Image\Projection\Image\EncodingOptions;
7
use AmaTeam\Image\Projection\API\Image\Format;
8
9
class Image implements ImageInterface
10
{
11
    /**
12
     * @var resource
13
     */
14
    private $resource;
15
    /**
16
     * @var int
17
     */
18
    private $width;
19
    /**
20
     * @var int
21
     */
22
    private $height;
23
24
    /**
25
     * @param resource $resource
26
     */
27
    public function __construct($resource)
28
    {
29
        $this->resource = $resource;
30
        $this->width = imagesx($resource);
31
        $this->height = imagesy($resource);
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function getWidth()
38
    {
39
        return $this->width;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getHeight()
46
    {
47
        return $this->height;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function getColorAt($x, $y)
54
    {
55
        $color = imagecolorat($this->resource, $x, $y);
56
        // shifting 23 is like shifting 24 but multiplied by 2, so 127
57
        // converts to 254
58
        return (($color & 0xFFFFFF) << 8) | ((~$color >> 23) & 0xFF);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function setColorAt($x, $y, $color)
65
    {
66
        // setting last alpha channel bit to 0, so alpha looks like that:
67
        // ?? ?? ?? ?0
68
        // then shifting it 23 bits left, effectively dividing it by 2
69
        $encoded = ($color >> 8) | ((~$color & 0xFE)) << 23;
70
        imagesetpixel($this->resource, $x, $y, $encoded);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function getBinary($format, EncodingOptions $options = null)
77
    {
78
        $options = $options ?: EncodingOptions::defaults();
79
        switch ($format) {
80
            case Format::JPEG:
81
                $quality = (int) ($options->getQuality() * 100);
82
                ob_start();
83
                imagejpeg($this->resource, null, $quality);
84
                return ob_get_clean();
85
            case Format::PNG:
86
                $compression = (int) ($options->getCompression() * 9);
87
                ob_start();
88
                imagepng($this->resource, null, $compression);
89
                return ob_get_clean();
90
            default:
91
                $message = "Unknown image format $format";
92
                throw new \BadMethodCallException($message);
93
        }
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getResource()
100
    {
101
        return $this->resource;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function __destruct()
108
    {
109
        imagedestroy($this->resource);
110
    }
111
}
112