Completed
Push — master ( 370c5e...d20bc9 )
by Renato
06:13
created

Imagine::crop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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