Completed
Push — master ( d20bc9...0214e8 )
by Renato
06:15
created

Imagine::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NwLaravel\FileStorage;
4
5
use Intervention\Image\ImageManager;
6
use Intervention\Image\Image;
7
8
class Imagine
9
{
10
    /**
11
     * @var ImageManager
12
     */
13
    protected $manager;
14
15
    /**
16
     * @var Image
17
     */
18
    protected $image;
19
20
    /**
21
     * Construct
22
     *
23
     * @param string       $path
24
     * @param ImageManager $manager
25
     */
26 17
    public function __construct($path, ImageManager $manager)
27
    {
28 17
        $this->manager = $manager;
29 17
        $this->image = $this->manager->make($path);
30 17
    }
31
32
    /**
33
     * Define Resize
34
     *
35
     * @param int     $width
36
     * @param int     $height
37
     * @param boolean $force
38
     *
39
     * @return Imagine
40
     */
41 5
    public function resize($width, $height, $force = false)
42
    {
43 5
        $width = intval($width);
44 5
        $height = intval($height);
45
        $callback = function () {};
46
47 5
        if ($width > 0 || $height > 0) {
48
            // AutoScale - aspectRatio
49 4
            if (!$force) {
50
                $callback = function ($constraint) {
51 1
                    $constraint->aspectRatio();
52 1
                    $constraint->upsize();
53 1
                };
54 1
            }
55
56 4
            $width = $width?:null;
57 4
            $height = $height?:null;
58 4
            $this->image->resize($width, $height, $callback);
59 4
        }
60
61 5
        return $this;
62
    }
63
64
    /**
65
     * Opacity
66
     *
67
     * @return Imagine
68
     */
69 3
    public function opacity($opacity)
70
    {
71 3
        $opacity = intval($opacity);
72
73 3
        if ($opacity > 0 && $opacity < 100) {
74 1
            $this->image->opacity($opacity);
75 1
        }
76
77 3
        return $this;
78
    }
79
80
    /**
81
     * Watermark
82
     *
83
     * @param string  $path
84
     * @param integer $opacity
85
     *
86
     * @return Imagine
87
     */
88 4
    public function watermark($path, $position = 'center', $opacity = null)
89
    {
90 4
        if ($this->isImage($path)) {
91 3
            $watermark = $this->manager->make($path);
92
93 3
            $width = $this->image->width();
94 3
            $height = $this->image->height();
95 3
            if ($watermark->width() > $width || $watermark->height() > $height) {
96 1
                $watermark->resize($width, $height, function ($constraint) {
97 1
                    $constraint->aspectRatio();
98 1
                    $constraint->upsize();
99 1
                });
100 1
            }
101
102 3
            if (!is_null($opacity) && $opacity >= 0 && $opacity <= 100) {
103 1
                $watermark->opacity($opacity);
104 1
            }
105
106 3
            $this->image->insert($watermark, $position);
107 3
        }
108
109 4
        return $this;
110
    }
111
112
    /**
113
     * Crop
114
     *
115
     * @param integer $width
116
     * @param integer $height
117
     * @param integer $x
118
     * @param integer $y
119
     *
120
     * @return binary
121
     */
122 1
    public function crop($width, $height, $x, $y)
123
    {
124 1
        $this->image->crop($width, $height, $x, $y);
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * Is Image
131
     *
132
     * @param string $path
133
     *
134
     * @return boolean
135
     */
136 4
    protected function isImage($path)
137
    {
138 4
        return (bool) ($path && is_file($path) && strpos(mime_content_type($path), 'image/')===0);
139
    }
140
141
    /**
142
     * Encode
143
     *
144
     * @param string  $format
145
     * @param integer $quality
146
     *
147
     * @return binary
148
     */
149 1
    public function encode($format = null, $quality = null)
150
    {
151 1
        return $this->image->encode($format, $quality);
152
    }
153
154
    /**
155
     * Save
156
     *
157
     * @param string  $path
158
     * @param integer $quality
159
     *
160
     * @return binary
161
     */
162 1
    public function save($path, $quality = null)
163
    {
164 1
        return $this->image->save($path, $quality);
165
    }
166
167
    /**
168
     * Get Image
169
     *
170
     * @return Image
171
     */
172 2
    public function getImage()
173
    {
174 2
        return $this->image;
175
    }
176
}
177