-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress.php
More file actions
192 lines (153 loc) · 5.67 KB
/
Copy pathCompress.php
File metadata and controls
192 lines (153 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Lambda\Dataform;
class Compress {
// @var file_url
protected $file_url;
// @var new_name_image
protected $new_name_image;
// @var quality
protected $quality;
// @var quality
protected $pngQuality;
// @var destination
protected $destination;
// @var image_size
protected $image_size;
// @var image_data
protected $image_data;
// @var image_mime
protected $image_mime;
// @var array_img_types
protected $array_img_types;
public function __construct($file_url, $new_name_image, $quality, $pngQuality, $destination = null) {
$this->set_file_url($file_url);
$this->set_new_name_image($new_name_image);
$this->set_quality($quality);
$this->set_destination($destination);
}
function get_file_url() {
return $this->file_url;
}
function get_new_name_image() {
return $this->new_name_image;
}
function get_quality() {
return $this->quality;
}
function get_pngQuality() {
return $this->pngQuality;
}
function set_file_url($file_url) {
$this->file_url = $file_url;
}
function set_new_name_image($new_name_image) {
$this->new_name_image = $new_name_image;
}
function set_quality($quality) {
$this->quality = $quality;
}
function set_pngQuality($pngQuality) {
$this->pngQuality = $pngQuality;
}
function get_destination() {
return $this->destination;
}
function set_destination($destination) {
$this->destination = $destination;
}
/**
* Function to compress image
* @return boolean
* @throws Exception
*/
public function compress_image(){
//Send image array
$array_img_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
$new_image = null;
$last_char = null;
$image_extension = null;
$destination_extension = null;
$png_compression = null;
$maxsize = 5245330;
try{
//If not found the file
if(empty($this->file_url) && !file_exists($this->file_url)){
throw new Exception('Please inform the image!');
return false;
}
//Get image width, height, mimetype, etc..
$image_data = getimagesize($this->file_url);
//Set MimeType on variable
$image_mime = $image_data['mime'];
//Verifiy if the file is a image
if(!in_array($image_mime, $array_img_types)){
throw new Exception('Please send a image!');
return false;
}
//Get file size
$image_size = filesize($this->file_url);
//if image size is bigger than 5mb
if($image_size >= $maxsize){
throw new Exception('Please send a imagem smaller than 5mb!');
return false;
}
//If not found the destination
if(empty($this->new_name_image)){
throw new Exception('Please inform the destination name of image!');
return false;
}
//If not found the quality
if(empty($this->quality)){
throw new Exception('Please inform the quality!');
return false;
}
//If not found the png quality
$png_compression = (!empty($this->pngQuality)) ? $this->pngQuality : 9 ;
$image_extension = pathinfo($this->file_url, PATHINFO_EXTENSION);
//Verify if is sended a destination file name with extension
$destination_extension = pathinfo($this->new_name_image, PATHINFO_EXTENSION);
//if empty
if(empty($destination_extension)){
$this->new_name_image = $this->new_name_image.'.'.$image_extension;
}
//Verify if folder destination isn´t empty
if(!empty($this->destination)){
//And verify the last one element of value
$last_char = substr($this->destination, -1);
if($last_char !== '/'){
$this->destination = $this->destination.'/';
}
}
//Switch to find the file type
switch ($image_mime){
//if is JPG and siblings
case 'image/jpeg':
case 'image/pjpeg':
//Create a new jpg image
$new_image = imagecreatefromjpeg($this->file_url);
imagejpeg($new_image, $this->destination.$this->new_name_image, $this->quality);
break;
//if is PNG and siblings
case 'image/png':
case 'image/x-png':
//Create a new png image
$new_image = imagecreatefrompng('conteudo/'.$this->file_url);
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
imagepng($new_image, $this->destination.$this->new_name_image, $png_compression);
break;
// if is GIF
case 'image/gif':
//Create a new gif image
$new_image = imagecreatefromgif('conteudo/'.$this->file_url);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagegif($new_image, $this->destination.$this->new_name_image);
}
} catch (Exception $ex) {
return $ex->getMessage();
}
//Return the new image resized
return $this->new_name_image;
}
}