1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* m2brimagem.class.php |
4
|
|
|
* |
5
|
|
|
* Classe para manipulação de imagens |
6
|
|
|
* |
7
|
|
|
* @package m2brnet admin v2 [www.m2brnet.com] |
8
|
|
|
* @author Davi Ferreira <[email protected]> |
9
|
|
|
* @version 0.8 $ 2010-02-26 20:22:13 $ |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class m2brimagem { |
13
|
|
|
|
14
|
|
|
// arquivos |
15
|
|
|
private $origem, $img, $img_temp; |
|
|
|
|
16
|
|
|
// dimensões |
17
|
|
|
private $largura, $altura, $nova_largura, $nova_altura, $tamanho_html; |
|
|
|
|
18
|
|
|
// dados do arquivo |
19
|
|
|
private $formato, $extensao, $tamanho, $arquivo, $diretorio; |
|
|
|
|
20
|
|
|
// extensões válidas |
21
|
|
|
private $extensoes_validas; |
22
|
|
|
// cor de fundo para preenchimento |
23
|
|
|
private $rgb; |
24
|
|
|
// posicionamento do crop |
25
|
|
|
private $posicao_crop; |
26
|
|
|
// mensagem de erro |
27
|
|
|
private $erro; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construtor |
31
|
|
|
* @param $string caminho da imagem a ser carregada |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function m2brimagem( $origem = '', $extensoes_validas = array( 'jpg', 'jpeg', 'jpe', 'gif', 'bmp', 'png' ) ) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
$this->origem = $origem; |
38
|
|
|
$this->extensoes_validas = $extensoes_validas; |
39
|
|
|
|
40
|
|
|
if ( $this->origem ) |
41
|
|
|
{ |
42
|
|
|
$this->dados(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->rgb( 0, 0, 0 ); |
46
|
|
|
|
47
|
|
|
} // fim construtor |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retorna dados da imagem |
51
|
|
|
* @param |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
private function dados() |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
// mensagem padrão, sem erro |
58
|
|
|
$this->erro = 'OK'; |
59
|
|
|
|
60
|
|
|
// verifica se imagem existe |
61
|
|
|
if ( !is_file( $this->origem ) ) |
62
|
|
|
{ |
63
|
|
|
$this->erro = 'Erro: Arquivo de imagem não encontrado!'; |
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
// dados do arquivo |
68
|
|
|
$this->dadosArquivo(); |
69
|
|
|
|
70
|
|
|
// verifica se é imagem |
71
|
|
|
if (!$this->eImagem()) |
72
|
|
|
{ |
73
|
|
|
$this->erro = 'Erro: Arquivo '.$this->origem.' não é uma imagem!'; |
74
|
|
|
} |
75
|
|
|
else |
76
|
|
|
{ |
77
|
|
|
// pega dimensões |
78
|
|
|
$this->dimensoes(); |
79
|
|
|
|
80
|
|
|
// cria imagem para php |
81
|
|
|
$this->criaImagem(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} // fim dadosImagem |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retorna validação da imagem |
89
|
|
|
* @param |
90
|
|
|
* @return String string com erro de mensagem ou 'OK' para imagem válida |
91
|
|
|
*/ |
92
|
|
|
public function valida() |
93
|
|
|
{ |
94
|
|
|
return $this->erro; |
95
|
|
|
} // fim valida |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Carrega uma nova imagem, fora do construtor |
99
|
|
|
* @param String caminho da imagem a ser carregada |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function carrega( $origem = '' ) |
103
|
|
|
{ |
104
|
|
|
$this->origem = $origem; |
105
|
|
|
$this->dados(); |
106
|
|
|
} // fim carrega |
107
|
|
|
|
108
|
|
|
//------------------------------------------------------------------------------ |
109
|
|
|
// dados da imagem |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Busca dimensões e formato real da imagem |
113
|
|
|
* @param |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
private function dimensoes() |
117
|
|
|
{ |
118
|
|
|
$dimensoes = getimagesize( $this->origem ); |
119
|
|
|
$this->largura = $dimensoes[0]; |
120
|
|
|
$this->altura = $dimensoes[1]; |
121
|
|
|
// 1 = gif, 2 = jpeg, 3 = png, 6 = BMP |
122
|
|
|
// http://br2.php.net/manual/en/function.exif-imagetype.php |
123
|
|
|
$this->formato = $dimensoes[2]; |
124
|
|
|
$this->tamanho_html = $dimensoes[3]; |
125
|
|
|
} // fim dimensoes |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Busca dados do arquivo |
129
|
|
|
* @param |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
private function dadosArquivo() |
133
|
|
|
{ |
134
|
|
|
// imagem de origem |
135
|
|
|
$pathinfo = pathinfo( $this->origem ); |
136
|
|
|
$this->extensao = strtolower( $pathinfo['extension'] ); |
137
|
|
|
$this->arquivo = $pathinfo['basename']; |
138
|
|
|
$this->diretorio = $pathinfo['dirname']; |
139
|
|
|
$this->tamanho = filesize( $this->origem ); |
140
|
|
|
} // fim dadosArquivo |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Verifica se o arquivo indicado é uma imagem |
144
|
|
|
* @param |
145
|
|
|
* @return Boolean true/false |
146
|
|
|
*/ |
147
|
|
|
private function eImagem() |
148
|
|
|
{ |
149
|
|
|
// filtra extensão |
150
|
|
|
if ( !in_array( $this->extensao, $this->extensoes_validas ) ) |
151
|
|
|
{ |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
else |
155
|
|
|
{ |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} // fim validaImagem |
159
|
|
|
|
160
|
|
|
//------------------------------------------------------------------------------ |
161
|
|
|
// manipulação da imagem |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Cria objeto de imagem para manipulação no GD |
165
|
|
|
* @param |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
private function criaImagem() |
169
|
|
|
{ |
170
|
|
|
switch ( $this->formato ) |
171
|
|
|
{ |
172
|
|
|
case 1: |
173
|
|
|
$this->img = imagecreatefromgif( $this->origem ); |
174
|
|
|
$this->extensao = 'gif'; |
175
|
|
|
break; |
176
|
|
|
case 2: |
177
|
|
|
$this->img = imagecreatefromjpeg( $this->origem ); |
178
|
|
|
$this->extensao = 'jpg'; |
179
|
|
|
break; |
180
|
|
|
case 3: |
181
|
|
|
$this->img = imagecreatefrompng( $this->origem ); |
182
|
|
|
$this->extensao = 'png'; |
183
|
|
|
break; |
184
|
|
|
case 6: |
185
|
|
|
$this->img = imagecreatefrombmp( $this->origem ); |
186
|
|
|
$this->extensao = 'bmp'; |
187
|
|
|
break; |
188
|
|
|
default: |
189
|
|
|
trigger_error( 'Arquivo inválido!', E_USER_WARNING ); |
190
|
|
|
break; |
191
|
|
|
} |
192
|
|
|
} // fim criaImagem |
193
|
|
|
|
194
|
|
|
//------------------------------------------------------------------------------ |
195
|
|
|
// funções para redimensionamento |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Armazena os valores RGB para redimensionamento com fill |
199
|
|
|
* @param Valores R, G e B |
200
|
|
|
* @return Void |
201
|
|
|
*/ |
202
|
|
|
public function rgb( $r, $g, $b ) |
203
|
|
|
{ |
204
|
|
|
$this->rgb = array( $r, $g, $b ); |
205
|
|
|
} // fim rgb |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Armazena posições x e y para crop |
209
|
|
|
* @param Array valores x e y |
210
|
|
|
* @return Void |
211
|
|
|
*/ |
212
|
|
|
public function posicaoCrop( $x, $y ) |
213
|
|
|
{ |
214
|
|
|
$this->posicao_crop = array( $x, $y, $this->largura, $this->altura ); |
215
|
|
|
} // fim posicao_crop |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Redimensiona imagem |
219
|
|
|
* @param Int $nova_largura valor em pixels da nova largura da imagem |
220
|
|
|
* @param Int $nova_altura valor em pixels da nova altura da imagem |
221
|
|
|
* @param String $tipo método para redimensionamento (padrão [vazio], 'fill' [preenchimento] ou 'crop') |
222
|
|
|
* @return Boolean/void |
|
|
|
|
223
|
|
|
*/ |
224
|
|
|
public function redimensiona( $nova_largura = 0, $nova_altura = 0, $tipo = '' ) |
225
|
|
|
{ |
226
|
|
|
|
227
|
|
|
// seta variáveis passadas via parâmetro |
228
|
|
|
$this->nova_largura = $nova_largura; |
229
|
|
|
$this->nova_altura = $nova_altura; |
230
|
|
|
|
231
|
|
|
// verifica se passou altura ou largura como porcentagem |
232
|
|
|
// largura % |
233
|
|
|
$pos = strpos( $this->nova_largura, '%' ); |
234
|
|
|
if( $pos !== false && $pos > 0 ) |
235
|
|
|
{ |
236
|
|
|
$porcentagem = ( ( int ) str_replace( '%', '', $this->nova_largura ) ) / 100; |
237
|
|
|
$this->nova_largura = round( $this->largura * $porcentagem ); |
238
|
|
|
} |
239
|
|
|
// altura % |
240
|
|
|
$pos = strpos( $this->nova_altura, '%' ); |
241
|
|
|
if( $pos !== false && $pos > 0 ) |
242
|
|
|
{ |
243
|
|
|
$porcentagem = ( ( int ) str_replace( '%', '', $this->nova_altura ) ) / 100; |
244
|
|
|
$this->nova_altura = $this->altura * $porcentagem; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// define se só passou nova largura ou altura |
248
|
|
|
if ( !$this->nova_largura && !$this->nova_altura ) |
249
|
|
|
{ |
250
|
|
|
return false; |
251
|
|
|
} |
252
|
|
|
// só passou altura |
253
|
|
|
elseif ( !$this->nova_largura ) |
254
|
|
|
{ |
255
|
|
|
$this->nova_largura = $this->largura / ( $this->altura/$this->nova_altura ); |
256
|
|
|
} |
257
|
|
|
// só passou largura |
258
|
|
|
elseif ( !$this->nova_altura ) |
259
|
|
|
{ |
260
|
|
|
$this->nova_altura = $this->altura / ( $this->largura/$this->nova_largura ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// redimensiona de acordo com tipo |
264
|
|
|
switch( $tipo ) |
265
|
|
|
{ |
266
|
|
|
case 'crop': |
267
|
|
|
$this->resizeCrop(); |
268
|
|
|
break; |
269
|
|
|
case 'fill': |
270
|
|
|
$this->resizeFill(); |
271
|
|
|
break; |
272
|
|
|
default: |
273
|
|
|
$this->resize(); |
274
|
|
|
break; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// atualiza dimensões da imagem |
278
|
|
|
$this->altura = $this->nova_altura; |
279
|
|
|
$this->largura = $this->nova_largura; |
280
|
|
|
|
281
|
|
|
} // fim redimensiona |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Redimensiona imagem, modo padrão, sem crop ou fill (distorcendo) |
285
|
|
|
* @param |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
|
|
private function resize() |
289
|
|
|
{ |
290
|
|
|
// cria imagem de destino temporária |
291
|
|
|
$this->img_temp = imagecreatetruecolor( $this->nova_largura, $this->nova_altura ); |
292
|
|
|
|
293
|
|
|
imagecopyresampled( $this->img_temp, $this->img, 0, 0, 0, 0, $this->nova_largura, $this->nova_altura, $this->largura, $this->altura ); |
294
|
|
|
$this->img = $this->img_temp; |
295
|
|
|
} // fim resize() |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Adiciona cor de fundo à imagem |
299
|
|
|
* @param |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
|
private function preencheImagem() |
303
|
|
|
{ |
304
|
|
|
$corfundo = imagecolorallocate( $this->img_temp, $this->rgb[0], $this->rgb[1], $this->rgb[2] ); |
305
|
|
|
imagefill( $this->img_temp, 0, 0, $corfundo ); |
306
|
|
|
} // fim preencheImagem |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Redimensiona imagem sem cropar, proporcionalmente, |
310
|
|
|
* preenchendo espaço vazio com cor rgb especificada |
311
|
|
|
* @param |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
private function resizeFill() |
315
|
|
|
{ |
316
|
|
|
// cria imagem de destino temporária |
317
|
|
|
$this->img_temp = imagecreatetruecolor( $this->nova_largura, $this->nova_altura ); |
318
|
|
|
|
319
|
|
|
// adiciona cor de fundo à nova imagem |
320
|
|
|
$this->preencheImagem(); |
321
|
|
|
|
322
|
|
|
// salva variáveis para centralização |
323
|
|
|
$dif_y = $this->nova_altura; |
324
|
|
|
$dif_x = $this->nova_largura; |
325
|
|
|
|
326
|
|
|
// verifica altura e largura |
327
|
|
|
if ( $this->largura > $this->altura ) |
328
|
|
|
{ |
329
|
|
|
$this->nova_altura = ( ( $this->altura * $this->nova_largura ) / $this->largura ); |
330
|
|
|
} |
331
|
|
|
elseif ( $this->largura <= $this->altura ) |
332
|
|
|
{ |
333
|
|
|
$this->nova_largura = ( ( $this->largura * $this->nova_altura ) / $this->altura ); |
334
|
|
|
} // fim do if verifica altura largura |
335
|
|
|
|
336
|
|
|
// copia com o novo tamanho, centralizando |
337
|
|
|
$dif_x = ( $dif_x - $this->nova_largura ) / 2; |
338
|
|
|
$dif_y = ( $dif_y - $this->nova_altura ) / 2; |
339
|
|
|
imagecopyresampled( $this->img_temp, $this->img, $dif_x, $dif_y, 0, 0, $this->nova_largura, $this->nova_altura, $this->largura, $this->altura ); |
340
|
|
|
$this->img = $this->img_temp; |
341
|
|
|
} // fim resizeFill() |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Calcula a posição do crop |
345
|
|
|
* Os índices 0 e 1 correspondem à posição x e y do crop na imagem |
346
|
|
|
* Os índices 2 e 3 correspondem ao tamanho do crop |
347
|
|
|
* @param |
348
|
|
|
* @return void |
349
|
|
|
*/ |
350
|
|
|
private function calculaPosicaoCrop() |
351
|
|
|
{ |
352
|
|
|
// média altura/largura |
353
|
|
|
$hm = $this->altura / $this->nova_altura; |
354
|
|
|
$wm = $this->largura / $this->nova_largura; |
355
|
|
|
|
356
|
|
|
// 50% para cálculo do crop |
357
|
|
|
$h_height = $this->nova_altura / 2; |
358
|
|
|
$h_width = $this->nova_largura / 2; |
359
|
|
|
|
360
|
|
|
// calcula novas largura e altura |
361
|
|
|
if( !is_array( $this->posicao_crop ) ) |
362
|
|
|
{ |
363
|
|
|
if ( $wm > $hm ) |
364
|
|
|
{ |
365
|
|
|
$this->posicao_crop[2] = $this->largura / $hm; |
366
|
|
|
$this->posicao_crop[3] = $this->nova_altura; |
367
|
|
|
$this->posicao_crop[0] = ( $this->posicao_crop[2] / 2 ) - $h_width; |
368
|
|
|
$this->posicao_crop[1] = 0; |
369
|
|
|
} |
370
|
|
|
// largura <= altura |
371
|
|
|
elseif ( ( $wm <= $hm ) ) |
372
|
|
|
{ |
373
|
|
|
$this->posicao_crop[2] = $this->nova_largura; |
374
|
|
|
$this->posicao_crop[3] = $this->altura / $wm; |
375
|
|
|
$this->posicao_crop[0] = 0; |
376
|
|
|
$this->posicao_crop[1] = ( $this->posicao_crop[3] / 2 ) - $h_height; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
} // fim calculaPosicaoCrop |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Redimensiona imagem, cropando para encaixar no novo tamanho, sem sobras |
383
|
|
|
* baseado no script original de Noah Winecoff |
384
|
|
|
* http://www.findmotive.com/2006/12/13/php-crop-image/ |
385
|
|
|
* atualizado para receber o posicionamento X e Y do crop na imagem |
386
|
|
|
* @return void |
387
|
|
|
*/ |
388
|
|
|
private function resizeCrop() |
389
|
|
|
{ |
390
|
|
|
// calcula posicionamento do crop |
391
|
|
|
$this->calculaPosicaoCrop(); |
392
|
|
|
|
393
|
|
|
// cria imagem de destino temporária |
394
|
|
|
$this->img_temp = imagecreatetruecolor( $this->nova_largura, $this->nova_altura ); |
395
|
|
|
|
396
|
|
|
// adiciona cor de fundo à nova imagem |
397
|
|
|
$this->preencheImagem(); |
398
|
|
|
|
399
|
|
|
imagecopyresampled( $this->img_temp, $this->img, -$this->posicao_crop[0], -$this->posicao_crop[1], 0, 0, $this->posicao_crop[2], $this->posicao_crop[3], $this->largura, $this->altura ); |
400
|
|
|
|
401
|
|
|
$this->img = $this->img_temp; |
402
|
|
|
} // fim resizeCrop |
403
|
|
|
|
404
|
|
|
//------------------------------------------------------------------------------ |
405
|
|
|
// funções de manipulação da imagem |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* flipa/inverte imagem |
409
|
|
|
* baseado no script original de Noah Winecoff |
410
|
|
|
* http://www.php.net/manual/en/ref.image.php#62029 |
411
|
|
|
* @param String $tipo tipo de espelhamento: h - horizontal, v - vertical |
412
|
|
|
* @return void |
413
|
|
|
*/ |
414
|
|
|
public function flip( $tipo = 'h' ) |
415
|
|
|
{ |
416
|
|
|
$w = imagesx( $this->img ); |
417
|
|
|
$h = imagesy( $this->img ); |
418
|
|
|
|
419
|
|
|
$this->img_temp = imagecreatetruecolor( $w, $h ); |
420
|
|
|
|
421
|
|
|
// vertical |
422
|
|
View Code Duplication |
if ( 'v' == $tipo ) |
423
|
|
|
{ |
424
|
|
|
for ( $y = 0; $y < $h; $y++ ) |
425
|
|
|
{ |
426
|
|
|
imagecopy( $this->img_temp, $this->img, 0, $y, 0, $h - $y - 1, $w, 1 ); |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
// horizontal |
431
|
|
View Code Duplication |
if ( 'h' == $tipo ) |
432
|
|
|
{ |
433
|
|
|
for ( $x = 0; $x < $w; $x++ ) |
434
|
|
|
{ |
435
|
|
|
imagecopy( $this->img_temp, $this->img, $x, 0, $w - $x - 1, 0, 1, $h ); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$this->img = $this->img_temp; |
440
|
|
|
|
441
|
|
|
} // fim flip |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* gira imagem |
445
|
|
|
* @param Int $graus grau para giro |
446
|
|
|
* @param Array $rgb cor RGB para preenchimento |
447
|
|
|
* @return void |
448
|
|
|
*/ |
449
|
|
|
public function girar( $graus, $rgb = array( 255,255,255 ) ) |
450
|
|
|
{ |
451
|
|
|
$corfundo = imagecolorallocate( $this->img, $rgb[0], $rgb[1], $rgb[2] ); |
452
|
|
|
$this->img = imagerotate( $this->img, $graus, $corfundo ); |
453
|
|
|
} // fim girar |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* adiciona texto à imagem |
457
|
|
|
* @param String $texto texto a ser inserido |
458
|
|
|
* @param Int $tamanho tamanho da fonte |
459
|
|
|
* @param Int $x posição x do texto na imagem |
460
|
|
|
* @param Int $y posição y do texto na imagem |
461
|
|
|
* @param Array $rgb cor do texto |
462
|
|
|
* @param Boolean $truetype true para utilizar fonte truetype, false para fonte do sistema |
463
|
|
|
* @param String $fonte nome da fonte truetype a ser utilizada |
464
|
|
|
* @return void |
465
|
|
|
*/ |
466
|
|
|
public function legenda( $texto, $tamanho = 10, $x = 0, $y = 0, $rgb = array( 255,255,255 ), $truetype = false, $fonte = '' ) |
467
|
|
|
{ |
468
|
|
|
$cortexto = imagecolorallocate( $this->img, $rgb[0], $rgb[1], $rgb[2] ); |
469
|
|
|
|
470
|
|
|
// truetype ou fonte do sistema? |
471
|
|
|
if ( $truetype === true ) |
472
|
|
|
{ |
473
|
|
|
imagettftext( $this->img, $tamanho, 0, $x, $y, $cortexto, $fonte, $texto ); |
474
|
|
|
} |
475
|
|
|
else |
476
|
|
|
{ |
477
|
|
|
imagestring( $this->img, $tamanho, $x, $y, $texto, $cortexto ); |
478
|
|
|
} |
479
|
|
|
} // fim legenda |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* adiciona imagem de marca d'água |
483
|
|
|
* @param String $imagem caminho da imagem de marca d'água |
484
|
|
|
* @param Int $x posição x da marca na imagem |
485
|
|
|
* @param Int $y posição y da marca na imagem |
486
|
|
|
* @return Boolean true/false dependendo do resultado da operação |
487
|
|
|
* @param Int $alfa valor para transparência (0-100) |
488
|
|
|
-> se utilizar alfa, a função imagecopymerge não preserva |
489
|
|
|
-> o alfa nativo do PNG |
490
|
|
|
*/ |
491
|
|
|
public function marca( $imagem, $x = 0, $y = 0, $alfa = 100 ) |
492
|
|
|
{ |
493
|
|
|
// cria imagem temporária para merge |
494
|
|
|
if ( $imagem ) { |
495
|
|
|
$pathinfo = pathinfo( $imagem ); |
496
|
|
|
switch( strtolower( $pathinfo['extension'] ) ) |
497
|
|
|
{ |
498
|
|
|
case 'jpg': |
499
|
|
|
case 'jpeg': |
500
|
|
|
$marcadagua = imagecreatefromjpeg( $imagem ); |
501
|
|
|
break; |
502
|
|
|
case 'png': |
503
|
|
|
$marcadagua = imagecreatefrompng( $imagem ); |
504
|
|
|
break; |
505
|
|
|
case 'gif': |
506
|
|
|
$marcadagua = imagecreatefromgif( $imagem ); |
507
|
|
|
break; |
508
|
|
|
case 'bmp': |
509
|
|
|
$marcadagua = imagecreatefrombmp( $imagem ); |
510
|
|
|
break; |
511
|
|
|
default: |
512
|
|
|
$this->erro = 'Arquivo de marca d\'água inválido.'; |
513
|
|
|
return false; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
else |
517
|
|
|
{ |
518
|
|
|
return false; |
519
|
|
|
} |
520
|
|
|
// dimensões |
521
|
|
|
$marca_w = imagesx( $marcadagua ); |
522
|
|
|
$marca_h = imagesy( $marcadagua ); |
523
|
|
|
// retorna imagens com marca d'água |
524
|
|
|
if ( is_numeric( $alfa ) && ( ( $alfa > 0 ) && ( $alfa < 100 ) ) ) { |
525
|
|
|
imagecopymerge( $this->img, $marcadagua, $x, $y, 0, 0, $marca_w, $marca_h, $alfa ); |
526
|
|
|
} else { |
527
|
|
|
imagecopy( $this->img, $marcadagua, $x, $y, 0, 0, $marca_w, $marca_h ); |
528
|
|
|
} |
529
|
|
|
return true; |
530
|
|
|
} // fim marca |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* adiciona imagem de marca d'água, com valores fixos |
534
|
|
|
* ex: topo_esquerda, topo_direita etc. |
535
|
|
|
* Implementação original por Giolvani <[email protected]> |
536
|
|
|
* @param String $imagem caminho da imagem de marca d'água |
537
|
|
|
* @param String $posicao posição/orientação fixa da marca d'água |
538
|
|
|
* [topo, meio, baixo] + [esquerda, centro, direita] |
539
|
|
|
* @param Int $alfa valor para transparência (0-100) |
540
|
|
|
* @return void |
541
|
|
|
*/ |
542
|
|
|
public function marcaFixa( $imagem, $posicao, $alfa = 100 ) |
543
|
|
|
{ |
544
|
|
|
|
545
|
|
|
// dimensões da marca d'água |
546
|
|
|
list( $marca_w, $marca_h ) = getimagesize( $imagem ); |
547
|
|
|
|
548
|
|
|
// define X e Y para posicionamento |
549
|
|
|
switch( $posicao ) |
550
|
|
|
{ |
551
|
|
|
case 'topo_esquerda': |
552
|
|
|
$x = 0; |
553
|
|
|
$y = 0; |
554
|
|
|
break; |
555
|
|
|
case 'topo_centro': |
556
|
|
|
$x = ( $this->largura - $marca_w ) / 2; |
557
|
|
|
$y = 0; |
558
|
|
|
break; |
559
|
|
|
case 'topo_direita': |
560
|
|
|
$x = $this->largura - $marca_w; |
561
|
|
|
$y = 0; |
562
|
|
|
break; |
563
|
|
|
case 'meio_esquerda': |
564
|
|
|
$x = 0; |
565
|
|
|
$y = ( $this->altura / 2 ) - ( $marca_h / 2 ); |
566
|
|
|
break; |
567
|
|
View Code Duplication |
case 'meio_centro': |
568
|
|
|
$x = ( $this->largura - $marca_w ) / 2; |
569
|
|
|
$y = ( $this->altura / 2 ) - ( $marca_h / 2 ); |
570
|
|
|
break; |
571
|
|
View Code Duplication |
case 'meio_direita': |
572
|
|
|
$x = $this->largura - $marca_w; |
573
|
|
|
$y = ( $this->altura / 2) - ( $marca_h / 2 ); |
574
|
|
|
break; |
575
|
|
|
case 'baixo_esquerda': |
576
|
|
|
$x = 0; |
577
|
|
|
$y = $this->altura - $marca_h; |
578
|
|
|
break; |
579
|
|
View Code Duplication |
case 'baixo_centro': |
580
|
|
|
$x = ( $this->largura - $marca_w ) / 2; |
581
|
|
|
$y = $this->altura - $marca_h; |
582
|
|
|
break; |
583
|
|
|
case 'baixo_direita': |
584
|
|
|
$x = $this->largura - $marca_w; |
585
|
|
|
$y = $this->altura - $marca_h; |
586
|
|
|
break; |
587
|
|
|
default: |
588
|
|
|
return false; |
589
|
|
|
break; |
|
|
|
|
590
|
|
|
} // end switch posicao |
591
|
|
|
|
592
|
|
|
// cria marca |
593
|
|
|
$this->marca( $imagem, $x, $y, $alfa ); |
594
|
|
|
|
595
|
|
|
} // fim marcaFixa |
596
|
|
|
|
597
|
|
|
|
598
|
|
|
//------------------------------------------------------------------------------ |
599
|
|
|
// gera imagem de saída |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* retorna saída para tela ou arquivo |
603
|
|
|
* @param String $destino caminho e nome do arquivo a serem criados |
604
|
|
|
* @param Int $qualidade qualidade da imagem no caso de JPEG (0-100) |
605
|
|
|
* @return void |
606
|
|
|
*/ |
607
|
|
|
public function grava( $destino='', $qualidade = 100 ) |
608
|
|
|
{ |
609
|
|
|
// dados do arquivo de destino |
610
|
|
|
if ( $destino ) |
611
|
|
|
{ |
612
|
|
|
$pathinfo = pathinfo( $destino ); |
613
|
|
|
$dir_destino = $pathinfo['dirname']; |
614
|
|
|
$extensao_destino = strtolower( $pathinfo['extension'] ); |
615
|
|
|
|
616
|
|
|
// valida diretório |
617
|
|
|
if ( !is_dir( $dir_destino ) ) |
618
|
|
|
{ |
619
|
|
|
$this->erro = 'Diretório de destino inválido ou inexistente'; |
620
|
|
|
return false; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
// valida extensão de destino |
626
|
|
|
if ( !isset( $extensao_destino ) ) |
627
|
|
|
{ |
628
|
|
|
$extensao_destino = $this->extensao; |
629
|
|
|
} |
630
|
|
|
else |
631
|
|
|
{ |
632
|
|
|
if ( !in_array( $extensao_destino, $this->extensoes_validas ) ) |
633
|
|
|
{ |
634
|
|
|
$this->erro = 'Extensão inválida para o arquivo de destino'; |
635
|
|
|
return false; |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
switch( $extensao_destino ) |
640
|
|
|
{ |
641
|
|
|
case 'jpg': |
642
|
|
|
case 'jpeg': |
643
|
|
View Code Duplication |
case 'bmp': |
644
|
|
|
if ( $destino ) |
645
|
|
|
{ |
646
|
|
|
imagejpeg( $this->img, $destino, $qualidade ); |
647
|
|
|
} |
648
|
|
|
else |
649
|
|
|
{ |
650
|
|
|
header( "Content-type: image/jpeg" ); |
651
|
|
|
imagejpeg( $this->img, NULL, $qualidade ); |
652
|
|
|
imagedestroy( $this->img ); |
653
|
|
|
exit; |
|
|
|
|
654
|
|
|
} |
655
|
|
|
break; |
656
|
|
View Code Duplication |
case 'png': |
657
|
|
|
if ( $destino ) |
658
|
|
|
{ |
659
|
|
|
imagepng( $this->img, $destino ); |
660
|
|
|
} |
661
|
|
|
else |
662
|
|
|
{ |
663
|
|
|
header( "Content-type: image/png" ); |
664
|
|
|
imagepng( $this->img ); |
665
|
|
|
imagedestroy( $this->img ); |
666
|
|
|
exit; |
|
|
|
|
667
|
|
|
} |
668
|
|
|
break; |
669
|
|
View Code Duplication |
case 'gif': |
670
|
|
|
if ( $destino ) |
671
|
|
|
{ |
672
|
|
|
imagegif( $this->img, $destino ); |
673
|
|
|
} |
674
|
|
|
else |
675
|
|
|
{ |
676
|
|
|
header( "Content-type: image/gif" ); |
677
|
|
|
imagegif( $this->img ); |
678
|
|
|
imagedestroy( $this->img ); |
679
|
|
|
exit; |
|
|
|
|
680
|
|
|
} |
681
|
|
|
break; |
682
|
|
|
default: |
683
|
|
|
return false; |
684
|
|
|
break; |
|
|
|
|
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
} // fim grava |
688
|
|
|
|
689
|
|
|
//------------------------------------------------------------------------------ |
690
|
|
|
// fim da classe |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
//------------------------------------------------------------------------------ |
694
|
|
|
// suporte para a manipulação de arquivos BMP |
695
|
|
|
|
696
|
|
|
/*********************************************/ |
697
|
|
|
/* Function: ImageCreateFromBMP */ |
698
|
|
|
/* Author: DHKold */ |
699
|
|
|
/* Contact: [email protected] */ |
700
|
|
|
/* Date: The 15th of June 2005 */ |
701
|
|
|
/* Version: 2.0B */ |
702
|
|
|
/*********************************************/ |
703
|
|
|
|
704
|
|
|
function imagecreatefrombmp($filename) { |
705
|
|
|
//Ouverture du fichier en mode binaire |
706
|
|
|
if (! $f1 = fopen($filename,"rb")) return FALSE; |
707
|
|
|
|
708
|
|
|
//1 : Chargement des ent?tes FICHIER |
709
|
|
|
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14)); |
710
|
|
|
if ($FILE['file_type'] != 19778) return FALSE; |
711
|
|
|
|
712
|
|
|
//2 : Chargement des ent?tes BMP |
713
|
|
|
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. |
714
|
|
|
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'. |
715
|
|
|
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40)); |
716
|
|
|
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']); |
717
|
|
|
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; |
718
|
|
|
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; |
719
|
|
|
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']); |
720
|
|
|
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4); |
721
|
|
|
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4); |
722
|
|
|
$BMP['decal'] = 4-(4*$BMP['decal']); |
723
|
|
|
if ($BMP['decal'] == 4) $BMP['decal'] = 0; |
724
|
|
|
|
725
|
|
|
//3 : Chargement des couleurs de la palette |
726
|
|
|
$PALETTE = array(); |
727
|
|
|
if ($BMP['colors'] < 16777216) |
728
|
|
|
{ |
729
|
|
|
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4)); |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
//4 : Cr?ation de l'image |
733
|
|
|
$IMG = fread($f1,$BMP['size_bitmap']); |
734
|
|
|
$VIDE = chr(0); |
735
|
|
|
|
736
|
|
|
$res = imagecreatetruecolor($BMP['width'],$BMP['height']); |
737
|
|
|
$P = 0; |
738
|
|
|
$Y = $BMP['height']-1; |
739
|
|
|
while ($Y >= 0) |
740
|
|
|
{ |
741
|
|
|
$X=0; |
742
|
|
|
while ($X < $BMP['width']) |
743
|
|
|
{ |
744
|
|
|
if ($BMP['bits_per_pixel'] == 24) |
745
|
|
|
$COLOR = @unpack("V",substr($IMG,$P,3).$VIDE); |
746
|
|
View Code Duplication |
elseif ($BMP['bits_per_pixel'] == 16) |
747
|
|
|
{ |
748
|
|
|
$COLOR = @unpack("n",substr($IMG,$P,2)); |
749
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1]; |
750
|
|
|
} |
751
|
|
View Code Duplication |
elseif ($BMP['bits_per_pixel'] == 8) |
752
|
|
|
{ |
753
|
|
|
$COLOR = @unpack("n",$VIDE.substr($IMG,$P,1)); |
754
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1]; |
755
|
|
|
} |
756
|
|
|
elseif ($BMP['bits_per_pixel'] == 4) |
757
|
|
|
{ |
758
|
|
|
$COLOR = @unpack("n",$VIDE.substr($IMG,floor($P),1)); |
759
|
|
|
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F); |
760
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1]; |
761
|
|
|
} |
762
|
|
|
elseif ($BMP['bits_per_pixel'] == 1) |
763
|
|
|
{ |
764
|
|
|
$COLOR = @unpack("n",$VIDE.substr($IMG,floor($P),1)); |
765
|
|
|
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7; |
766
|
|
View Code Duplication |
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6; |
767
|
|
View Code Duplication |
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5; |
768
|
|
View Code Duplication |
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4; |
769
|
|
View Code Duplication |
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3; |
770
|
|
View Code Duplication |
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2; |
771
|
|
View Code Duplication |
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1; |
772
|
|
View Code Duplication |
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1); |
773
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1]; |
774
|
|
|
} |
775
|
|
|
else |
776
|
|
|
return FALSE; |
777
|
|
|
imagesetpixel($res,$X,$Y,$COLOR[1]); |
778
|
|
|
$X++; |
779
|
|
|
$P += $BMP['bytes_per_pixel']; |
780
|
|
|
} |
781
|
|
|
$Y--; |
782
|
|
|
$P+=$BMP['decal']; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
//Fermeture du fichier |
786
|
|
|
fclose($f1); |
787
|
|
|
|
788
|
|
|
return $res; |
789
|
|
|
|
790
|
|
|
} // fim function image from BMP |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.