Completed
Pull Request — master (#263)
by
unknown
05:02
created

Gd   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 158
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setImageCompressionQuality() 0 5 1
B __construct() 0 22 4
B cropThumbnailImage() 0 41 5
C writeImage() 0 26 8
A __destruct() 0 4 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Image;
11
12
/**
13
 * Wrapper over Gd for support some Imagick functions
14
 *
15
 * @category Application
16
 * @package  Library
17
 *
18
 * @author   Anton Shevchuk
19
 * @created  1/10/14 8:43 AM
20
 */
21
class Gd
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $file;
27
28
    /**
29
     * @var resource
30
     */
31
    protected $image;
32
33
    /**
34
     * @var integer
35
     */
36
    protected $width;
37
38
    /**
39
     * @var integer
40
     */
41
    protected $height;
42
43
    /**
44
     * Compression quality for JPEG
45
     * @var integer
46
     */
47
    protected $quality = 86;
48
49
    /**
50
     * Constructor of Gd
51
     *
52
     * @param string $file
53
     * @throws Exception
54
     */
55
    public function __construct($file)
56
    {
57
        if (!file_exists($file)) {
58
            throw new Exception("Image `$file` file not found");
59
        }
60
        $this->file = $file;
61
62
        // Retrieve image information
63
        list($this->width, $this->height, $type) = getimagesize($file);
64
65
        // Check support of file type
66
        if (!(imagetypes() & $type)) {
67
            throw new Exception("Server does not support `$type` image type");
68
        }
69
70
        // Using imagecreatefromstring will automatically detect the file type
71
        $this->image = imagecreatefromstring(file_get_contents($file));
72
73
        if (!$this->image) {
74
            throw new Exception("Could not load image");
75
        }
76
    }
77
78
    /**
79
     * @param integer $quality
80
     * @return bool
81
     */
82
    public function setImageCompressionQuality($quality)
83
    {
84
        $this->quality = $quality;
85
        return true;
86
    }
87
88
    /**
89
     * Create a crop thumbnail image from source image
90
     * cropped by smaller side
91
     *
92
     * @param integer $width
93
     * @param integer $height
94
     * @return bool
95
     */
96
    public function cropThumbnailImage($width, $height)
97
    {
98
        // Compare image size with required thumbnail size
99
        if (($this->width < $width) &&
100
            ($this->height < $height)) {
101
            return true;
102
        }
103
104
        $widthScale = round($this->width / $width);
105
        $heightScale = round($this->height / $height);
106
107
        if ($heightScale < $widthScale) {
108
            // Crop width
109
            $cropWidth = $heightScale * $width;
110
            $cropHeight = $this->height;
111
            $srcX = round(($this->width - $cropWidth)/2);
112
            $srcY = 0;
113
        } else {
114
            // Crop height
115
            $cropWidth = $this->width;
116
            $cropHeight = $widthScale * $height;
117
            $srcX = 0;
118
            $srcY = round(($this->height - $cropHeight)/2);
119
        }
120
121
        $thumb = imagecreatetruecolor($width, $height);
122
123
        // Copy resampled makes a smooth thumbnail
124
        imagecopyresampled($thumb, $this->image, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight);
125
        imagedestroy($this->image);
126
127
        $this->width = $width;
128
        $this->height = $height;
129
        $this->image = $thumb;
130
131
        if ($thumb) {
132
            return true;
133
        } else {
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * Save the image to a file. Type is determined from the extension
140
     *
141
     * @param string $fileName
142
     * @return bool
143
     */
144
    public function writeImage($fileName)
145
    {
146
        if (!$this->image || file_exists($fileName)) {
147
            return false;
148
        }
149
150
        $ext = strtolower(substr($fileName, strrpos($fileName, '.')));
151
152
        switch ($ext) {
153
            case '.gif':
154
                return imagegif($this->image, $fileName);
155
                // break
156
            case '.jpg':
157
            case '.jpeg':
158
                return imagejpeg($this->image, $fileName, $this->quality);
159
                // break
160
            case '.png':
161
                return imagepng($this->image, $fileName);
162
                // break
163
            case '.bmp':
164
                return imagewbmp($this->image, $fileName);
165
                // break
166
            default:
167
                return false;
168
        }
169
    }
170
171
    /**
172
     * Destroy image source
173
     */
174
    public function __destruct()
175
    {
176
        imagedestroy($this->image);
177
    }
178
}
179