-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImgProcess.php
150 lines (125 loc) · 4.12 KB
/
ImgProcess.php
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
<?php
class ImgCompressor {
private $setting;
public function __construct($setting) {
$this->setting = $setting;
}
private function create($image, $name, $type, $c_type, $level) {
$im_name = time() . $name;
$im_output = $this->setting['directory'] . '/' . $im_name;
$im = $this->createImage($image, $type);
$im_type = $this->compressImage($im, $im_output, $c_type, $level);
imagedestroy($im);
$im_size = filesize($im_output);
$data = [
'original' => [
'name' => $name,
'type' => $type
],
'compressed' => [
'name' => $im_name,
'type' => $im_type,
'size' => $im_size
]
];
return $data;
}
private function createImage($image, $type) {
if ($type === 'image/jpeg') {
return imagecreatefromjpeg($image);
} elseif ($type === 'image/gif') {
return imagecreatefromgif($image);
} else {
return imagecreatefrompng($image);
}
}
private function compressImage($im, $im_output, $c_type, $level) {
if ($c_type === 'jpeg' || $c_type === 'jpg') {
$im_type = 'image/jpeg';
imagejpeg($im, $im_output, 100 - ($level * 10));
} elseif ($c_type === 'gif') {
$im_type = 'image/gif';
if ($this->checkTransparent($im)) {
imagegif($im, $im_output);
} else {
imagegif($im, $im_output);
}
} elseif ($c_type === 'png') {
$im_type = 'image/png';
if ($this->checkTransparent($im)) {
imagepng($im, $im_output, $level);
} else {
imagepng($im, $im_output, $level);
}
}
return $im_type;
}
private function checkTransparent($im) {
$width = imagesx($im);
$height = imagesy($im);
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
$rgba = imagecolorat($im, $i, $j);
if (($rgba & 0x7F000000) >> 24) {
return true;
}
}
}
return false;
}
public function run($image, $c_type, $level = 0) {
$im_info = getimagesize($image);
$im_name = basename($image);
$im_type = $im_info['mime'];
$result = [];
if (in_array($c_type, ['jpeg', 'jpg', 'JPG', 'JPEG', 'gif', 'GIF', 'png', 'PNG'])) {
if (in_array($im_type, $this->setting['file_type'])) {
if ($level >= 0 && $level <= 9) {
$result['status'] = 'success';
$result['data'] = $this->create($image, $im_name, $im_type, $c_type, $level);
} else {
$result['status'] = 'error';
$result['message'] = 'Compression level: from 0 (no compression) to 9';
}
} else {
$result['status'] = 'error';
$result['message'] = 'Failed file type';
}
} else {
$result['status'] = 'error';
$result['message'] = 'Failed file type';
}
return $result;
}
}
// To Convert into Webp
class ImageToWebp {
private $source = null;
protected function getImageResource () {
// Find the extension of source image.
$extension = strtolower( strrchr ( $this->source, '.' ) );
// Convert image to resource object according to its type.
switch ( $extension ) {
case '.jpg':
case '.jpeg':
$img = @imagecreatefromjpeg( $this->source );
break;
case '.gif':
$img = @imagecreatefromgif( $this->source );
break;
case '.png':
$img = @imagecreatefrompng( $this->source );
break;
default:
$img = false;
break;
}
return $img;
}
public function convert ( $source, $destination, $quality = 80 ) {
// Set default values globally
$this->source = $source;
// Convert to webp, yey
imagewebp( $this->getImageResource(), $destination, $quality );
}
}