class.img.php
6.94 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
class img
{
private $source_name = false;
private $source_w = 0;
private $source_h = 0;
private $source_type = false;
private $source_mime = false;
private $error_message = false;
private $rst_w = 0;
private $rst_h = 0;
private $rst_type = false;
private $rst_mime = false;
private $crop_x = 0; // êîðäèíàòà x íà÷àëüíîé òî÷êè íà èñõîäíîì èçîáðàæåíèè
private $crop_y = 0; // êîðäèíàòà y íà÷àëüíîé òî÷êè íà èñõîäíîì èçîáðàæåíèè
private $crop_w = 0; // øèðèíà íîâîãî èçîáðàæåíèÿ
private $crop_h = 0; // âûñîòà íîâîãî èçîáðàæåíèÿ
private $flag_resize = false;
private $flag_crop = false;
private $flag_watermark = false;
private $FILE_MIMES = array(
'image/pjpeg'=>"jpeg",
'image/jpeg'=>"jpeg",
'image/jpg'=>"jpeg",
'image/png'=>"png",
'image/x-png'=>"png",
'image/gif'=>"gif");
//private $alpha = 100;
private $quality = 100;
// var $rst_name =false;
//var $more_resize = false; // óâåëè÷èâàòü èçîáðàæåíèå
// çàãðóæàåì èçîáðàæåíèå
public function __construct($name){
// ôàéëà íåò, âîçâðàùàåì îáúåêò ñ îøèáêîé
if ( !file_exists($name) ){
$this -> make_error( sprintf(E_IMG_FILE_NOT_FOUND,$name) );
return false;
}
// ôàéë íå èçîáðàæåíèå, âîçâðàùàåì îáúåêò ñ îøèáêîé
$info = getimagesize($name);
if ( empty( $info[0]) ){
$this -> make_error( sprintf(E_IMG_FILE_IS_NO_IMAGE,$name) );
return false;
}
// ñ òàêèì èçîáðàæåíèåì ðàáîòàòü ñèñòåìà íå ìîæåò
$type = $this -> mime_to_type($info['mime']);
if ( !function_exists('imagecreatefrom'.$type) ){
$this -> make_error( sprintf(E_IMG_FILE_BAD_TYPE,$name) );
return false;
}
// óñòàíàâëèâàåì ïàðàìåòðû ïî óìîë÷àíèþ
$this -> source_name = $name;
$this -> width = $this -> source_w = $this -> rst_w = $info[0];
$this -> height = $this -> source_h = $this -> rst_h = $info[1];
$this -> mime = $this -> rst_mime = $this -> source_mime = $info['mime'];
$this -> type = $this -> source_type = $type;
$this -> rst_type = $type;
}
// òèï íîâîãî ôàéëà
public function set_type($v){
if ( empty($v) ){
$this -> make_error( E_IMG_EMPTY_TYPE );
return false;
}
if ( !function_exists('image'.$v) ){
$this -> make_error( sprintf(E_IMG_FILE_BAD_TYPE,$v) );
return false;
}
$this -> rst_mime = $this -> type_to_mime($v);
if ( !function_exists('image'.$v) ){
$this -> make_error( sprintf(E_IMG_BAD_MIME,$v) );
return false;
}
$this -> rst_type = $v;
return true;
}
// èìÿ íîâîãî ôàéëà
public function set_name($v){
if ( empty($v) ){
$this -> make_error( E_IMG_EMPTY_NAME );
return false;
}
if ( preg_match('#[^a-z0-9\-=._]#i',$v,$res) ){
$this -> make_error(E_IMG_BAD_NAME);
return false;
}
$this -> rst_name = $v;
return true;
}
// çàäà¸ì ïàïêó äëÿ ñîõðàíåíèÿ ðåçóëüòàòà
public function set_path($v){
if ( empty($v) ){
$this -> make_error( E_IMG_EMPRY_PATH );
return false;
}
if ( !is_dir($v) ){
$this -> make_error( sprintf(E_IMG_BAD_PATH,$v) );
return false;
}
if ( !is_writable($v) ){
$this -> make_error( sprintf(E_IMG_PATH_WRITE_ACCESS,$v) );
return false;
}
$this -> rst_path = $v;
return true;
}
/**
* Âû÷èñëÿåò ðàçìåðû íîâîãî èçîáðàæåíèÿ
*
* @project cms
* @class img
* @name resize
* @param w int øèðèíà íîâîãî èçîáðàæåíèÿ
* @param h int âûñîòà íîâîãî èçîáðàæåíèÿ
* @param strict bool æ¸ñòêîå èëè ïðîïîðöèîíàëüíîå âû÷èñëåíèå ðàçìåðîâ èçîáðàæåíèÿ
* @return bool
*/
public function resize($w=0,$h=0,$strict = false){
$this -> flag_resize = true;
// æ¸ñòêèé ðåñàéç
if ( $strict ){
$w = intval($w);
$h = intval($h);
// øî çà ðåñàéç áåç ðàçìåðîâ èçîáðàæåíèÿ
if ( empty($w) || empty($h) ){
$this -> make_error(E_IMG_EMPTY_SIZE);
return false;
}
$this -> rst_w = $w;
$this -> rst_h = $h;
return true;
}
$w = empty($w) ? $this -> source_w : intval($w);
$h = empty($h) ? $this -> source_h : intval($h);
// ðàçìåðû íîâîãî èçîáðàæåíèÿ áóäóò ïðåâûøàòü ðàçìåðû òåêóùåãî
if ( $w>=$this -> source_w && $h>=$this -> source_h ){
return false;
}
// âûÿñíèì íà ñêîëüêî íóæíî ñæàòü èçîáðàæåíèå
$koof_w = round($this -> source_w / $w );
$koof_h = round($this -> source_h / $h );
$koof_max = max($koof_w,$koof_h);
$this -> rst_w = round( $this -> source_w / $koof_max);
$this -> rst_h = round( $this -> source_h / $koof_max);
// ðåñàéç íå òðåáóåòñÿ
if ( $this -> rst_w==$this -> source_w && $this -> rst_h==$this -> source_h ){
return false;
}
return true;
}
public function crop($x,$y,$w,$h){
$this -> crop_x = intval($x);
$this -> crop_y = intval($y);
$this -> crop_w = intval($w);
$this -> crop_h = intval($h);
$this -> flag_crop = true;
return true;
}
// ñîõðàíÿåì ðåçóëüòàò
public function save(){
// óñòàíîâêà ïàðàìåòðîâ ïðèâåëà ê îøèáêå
if ( false!==$this -> error() ){
return false;
}
// çàãðóæàåì èñõîäíèê äëÿ ðàáîòû
$func_name = 'imagecreatefrom'.$this -> source_type;
$source = $func_name($this -> source_name);
// äåëàåì ðåñàéç
if ( false!==$this -> flag_resize ){
$target = imagecreatetruecolor($this -> rst_w , $this -> rst_h);
// äîáàâèì ïèíãàì íåìíîãî ïðîçðà÷íîñòè
imagealphablending($target, false);
imagesavealpha($target, true);
imagecopyresampled(
$target,
$source, 0,0,0,0,
$this -> rst_w,$this -> rst_h,$this -> source_w,$this -> source_h
);
$source = $target;
}
// äåëàåì êðîï
if ( false!==$this -> flag_crop ){
$target = imagecreatetruecolor($this -> crop_w , $this -> crop_h);
imagecopy(
$target,
$source, 0, 0,$this -> crop_x, $this -> crop_y, $this -> crop_w, $this -> crop_h
);
$source = $target;
}
// äåëàåì âîäÿíîé çíàê
if ( false!==$this -> flag_watermark ){
// òóò íàíîñèì âîäÿíûå çíàêè íà èçîáðàæåíèå
}
// âûâîäèì ðåçóëüòàò
$func_name = 'image'.$this -> rst_type;
if ( $this -> rst_type=='png' ){
$this -> quality = round($this -> quality / 10)-1;
}
if ( empty($this -> rst_name) ){
header("Content-type: ".$this -> rst_mime);
if ( !$func_name($target,false,$this -> quality) ){
$this -> make_error( E_IMG_CREATE_FALSE );
}
}else{
if ( !$func_name($target,$this -> rst_path.'/'.$this -> rst_name,$this -> quality) ){
$this -> make_error( E_IMG_CREATE_FALSE );
}
}
imagedestroy($target);
//imagedestroy($source);
return true;
}
public function make_error($msg){
$this -> error_message = $msg;
}
public function error(){
return $this -> error_message;
}
// ïåðåâîäèì mime-type èçîáðàæåíèÿ â åãî ôîðìàò
private function mime_to_type($v){
if (!empty($this -> FILE_MIMES[$v]) ){
return $this -> FILE_MIMES[$v];
}else{
return false;
}
}
// ïîëó÷àåì mime-type èçîáðàæåíèÿ ïî åãî ðàñøèðåíèþ
private function type_to_mime($i){
foreach( $this -> FILE_MIMES as $k=>$v ){
if ( $i==$v ){
return $k;
}
}
return false;
}
}
?>