forked from daviferreira/canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.php
1192 lines (1047 loc) · 39.1 KB
/
canvas.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
* Classe para manipulação de imagens utilizando a extensão GD
* e recursos avançados de filtros. Requer PHP 5 ou superior.
*
* @author Davi Ferreira <[email protected]>
* @version 1.0 $ 2010-10-17 19:11:51 $
*/
class canvas
{
/**
* Variáveis para armazenamento de arquivos/imgs
**/
private $origem, $img, $img_temp;
/**
* Armazenam as dimensões da imagem atual e da nova imagem caso exista
**/
private $largura, $altura, $nova_largura, $nova_altura, $tamanho_html;
/**
* Variáveis para o posicionamento do crop
**/
private $pos_x, $pos_y;
/**
* Informações sobre o arquivo enviado e diretório
**/
private $formato, $extensao, $tamanho, $arquivo, $diretorio;
/**
* Array RGB para resize com preenchimendo do fundo
**/
private $rgb;
/**
* Coordenadas para posicionamento do crop
**/
private $posicao_crop;
/**
* @var canvas
*/
private static $instance;
/**
* Método singleton para que apenas uma instancia da classe seja utilizada no servidor.
* @param String $origem
* @return canvas
*/
public static function Instance($origem = '')
{
if (empty(self::$instance))
self::$instance = new canvas($origem);
else
self::$instance->resetar();
return self::$instance;
}
/**
* Construtor
* @param $string caminho da imagem a ser carregada [opcional]
* @return void
**/
private function __construct($origem = '')
{
$this->origem = $origem;
if ($this->origem) {
$this->dados();
}
// RGB padrão -> branco
$this->rgb(255, 255, 255);
} // fim construtor
/**
* Reseta variáveis para poder reutilizar objeto em encadeamentos longos
* @return void
**/
public function resetar()
{
$this->origem = $this->img = $this->img_temp = $this->largura = $this->altura = $this->nova_largura = $this->nova_altura = $this->tamanho_html = $this->pos_x = $this->pos_y = $this->formato = $this->extensao = $this->tamanho = $this->arquivo = $this->diretorio = $this->posicao_crop = NULL;
$this->rgb(255, 255, 255);
} // fim resetar
/**
* Retorna dados da imagem
* @return void
**/
private function dados()
{
// verifica se imagem existe
if (is_file($this->origem)) {
// dados do arquivo
$this->dadosArquivo();
// verifica se é imagem
if (!$this->eImagem()) {
trigger_error('Erro: Arquivo ' . $this->origem . ' não é uma imagem!', E_USER_ERROR);
} else {
// busca dimensões da imagem enviada
$this->dimensoes();
// cria imagem para php
$this->criaImagem();
}
} else {
trigger_error('Erro: Arquivo de imagem não encontrado!', E_USER_ERROR);
}
} // fim dadosImagem
/**
* Carrega uma nova imagem, fora do construtor
* @param String caminho da imagem a ser carregada
* @return Object instância atual do objeto, para métodos encadeados
**/
public function carrega($origem = '')
{
$this->origem = $origem;
$this->dados();
return $this;
} // fim carrega
/**
* Busca dimensões e formato real da imagem
* @return void
**/
private function dimensoes()
{
$dimensoes = getimagesize($this->origem);
$this->largura = $dimensoes[0];
$this->altura = $dimensoes[1];
/**
* 1 = gif, 2 = jpeg, 3 = png, 6 = BMP
* http://br2.php.net/manual/en/function.exif-imagetype.php
**/
$this->formato = $dimensoes[2];
$this->tamanho_html = $dimensoes[3];
} // fim dimensoes
/**
* Busca dados do arquivo
* @return void
**/
private function dadosArquivo()
{
// imagem de origem
$pathinfo = pathinfo($this->origem);
$this->extensao = array_key_exists('extension', $pathinfo) ? strtolower($pathinfo['extension']) : strtolower(str_replace('image/', '', $obj['mime']));
$this->arquivo = $pathinfo['basename'];
$this->diretorio = $pathinfo['dirname'];
} // fim dadosArquivo
/**
* Verifica se o arquivo indicado é uma imagem
* @return Boolean true/false
**/
private function eImagem()
{
// filtra extensão
$valida = getimagesize($this->origem);
if (!is_array($valida) || empty($valida)) {
return false;
} else {
return true;
}
} // fim validaImagem
/**
* Cria uma nova imagem para ser trabalhada com textos, etc.
* OBS: a cor da imagem deve ser passada antes, via rgb() ou hex()
* @param String $largura da imagem a ser criada
* @param String $altura da imagem a ser criada
* @return Object instância atual do objeto, para métodos encadeados
**/
public function novaImagem($largura, $altura)
{
$this->largura = $largura;
$this->altura = $altura;
$this->img = imagecreatetruecolor($this->largura, $this->altura);
$cor_fundo = imagecolorallocate($this->img, $this->rgb[0], $this->rgb[1], $this->rgb[2]);
imagefill($this->img, 0, 0, $cor_fundo);
$this->extensao = 'jpg';
return $this;
} // fim novaImagem
/**
* Carrega uma imagem via URL
* OBS: depente das configurações do servidor para acesso remoto de arquivos
* @param String $url endereço da imagem
* @return Object instância atual do objeto, para métodos encadeados
**/
public function carregaUrl($url)
{
$this->origem = $url;
$pathinfo = pathinfo($this->origem);
$this->extensao = strtolower($pathinfo['extension']);
switch ($this->extensao) {
case 'jpg':
case 'jpeg':
$this->formato = 2;
break;
case 'gif':
$this->formato = 1;
break;
case 'png':
$this->formato = 3;
break;
case 'bmp':
$this->formato = 6;
break;
default:
break;
}
$this->criaImagem();
$this->largura = imagesx($this->img);
$this->altura = imagesy($this->img);
return $this;
} // fim carregaUrl
/**
* Cria objeto de imagem para manipulação no GD
* @return void
**/
private function criaImagem()
{
switch ($this->formato) {
case 1:
$this->img = imagecreatefromgif($this->origem);
$this->extensao = 'gif';
break;
case 2:
$this->img = imagecreatefromjpeg($this->origem);
$this->extensao = 'jpg';
break;
case 3:
$this->img = imagecreatefrompng($this->origem);
$this->extensao = 'png';
break;
case 6:
$this->img = imagecreatefrombmp($this->origem);
$this->extensao = 'bmp';
break;
default:
trigger_error('Arquivo inválido!', E_USER_ERROR);
break;
}
} // fim criaImagem
/**
* Armazena os valores RGB para redimensionamento com preenchimento
* @param Valores R, G e B
* @return Object instância atual do objeto, para métodos encadeados
**/
public function rgb($r, $g, $b)
{
$this->rgb = array(
$r,
$g,
$b
);
return $this;
} // fim rgb
/**
* Converte hexadecimal para RGB
* @param String $cor cor hexadecimal
* @return Object instância atual do objeto, para métodos encadeados
**/
public function hexa($cor)
{
$cor = str_replace('#', '', $cor);
if (strlen($cor) == 3)
$cor .= $cor; // #fff, #000 etc.
$this->rgb = array(
hexdec(substr($cor, 0, 2)),
hexdec(substr($cor, 2, 2)),
hexdec(substr($cor, 4, 2))
);
return $this;
} // fim hexa
/**
* Armazena posições x e y para crop
* @param Int x - posicao x do crop
* @param Int y - posicao y do crop
* @param Int w - width - larguraOrigem (by OctaAugusto)
* @param Int h - height - alturaOrigem (by OctaAugusto)
* @return Object instância atual do objeto, para métodos encadeados
**/
public function posicaoCrop($x, $y, $w = 0, $h = 0)
{
// sem largura ou altura setada manualmente, pega original da imagem
if (!$w)
$w = $this->largura;
if (!$h)
$h = $this->altura;
$this->posicao_crop = array(
$x,
$y,
$w,
$h
);
return $this;
} // fim posicao_crop
/**
* Redimensiona imagem
* @param Int $nova_largura valor em pixels da nova largura da imagem
* @param Int $nova_altura valor em pixels da nova altura da imagem
* @param String $tipo método para redimensionamento (padrão [vazio], preenchimento ou crop)
* @return Object instância atual do objeto, para métodos encadeados
**/
public function redimensiona($nova_largura = 0, $nova_altura = 0, $tipo = '')
{
// seta variáveis passadas via parâmetro
$this->nova_largura = $nova_largura;
$this->nova_altura = $nova_altura;
// verifica se passou altura ou largura como porcentagem
// largura %
$pos = strpos($this->nova_largura, '%');
if ($pos !== false && $pos > 0) {
$porcentagem = (( int ) str_replace('%', '', $this->nova_largura)) / 100;
$this->nova_largura = round($this->largura * $porcentagem);
}
// altura %
$pos = strpos($this->nova_altura, '%');
if ($pos !== false && $pos > 0) {
$porcentagem = (( int ) str_replace('%', '', $this->nova_altura)) / 100;
$this->nova_altura = $this->altura * $porcentagem;
}
// define se só passou nova largura ou altura
if (!$this->nova_largura && !$this->nova_altura) {
return false;
}
// só passou altura
elseif (!$this->nova_largura) {
$this->nova_largura = $this->largura / ($this->altura / $this->nova_altura);
}
// só passou largura
elseif (!$this->nova_altura) {
$this->nova_altura = $this->altura / ($this->largura / $this->nova_largura);
}
// redimensiona de acordo com tipo
switch ($tipo) {
case 'crop':
$this->redimensionaCrop();
break;
case 'preenchimento':
$this->redimensionaPreenchimento();
break;
case 'proporcional':
// modo proporcional sem preenchimento adicionado por Fernando VR (goo.gl/iDtmP)
$this->redimensionaProporcional();
break;
default:
$this->redimensionaSimples();
break;
}
// atualiza dimensões da imagem
$this->altura = $this->nova_altura;
$this->largura = $this->nova_largura;
return $this;
} // fim redimensiona
/**
* Redimensiona imagem, modo padrão, sem crop ou preenchimento
* (distorcendo caso tenha passado ambos altura e largura)
* @return void
**/
private function redimensionaSimples()
{
// cria imagem de destino temporária
$this->img_temp = imagecreatetruecolor($this->nova_largura, $this->nova_altura);
imagecopyresampled($this->img_temp, $this->img, 0, 0, 0, 0, $this->nova_largura, $this->nova_altura, $this->largura, $this->altura);
$this->img = $this->img_temp;
} // fim redimensiona()
/**
* Adiciona cor de fundo à imagem
* @return void
**/
private function preencheImagem()
{
$cor_fundo = imagecolorallocate($this->img_temp, $this->rgb[0], $this->rgb[1], $this->rgb[2]);
imagefill($this->img_temp, 0, 0, $cor_fundo);
} // fim preencheImagem
/**
* Redimensiona imagem sem cropar, proporcionalmente,
* preenchendo espaço vazio com cor rgb especificada
* @return void
**/
private function redimensionaPreenchimento()
{
// cria imagem de destino temporária
$this->img_temp = imagecreatetruecolor($this->nova_largura, $this->nova_altura);
// adiciona cor de fundo à nova imagem
$this->preencheImagem();
// salva variáveis para centralização
$dif_x = $dif_w = $this->nova_largura;
$dif_y = $dif_h = $this->nova_altura;
/**
* Verifica altura e largura
* Calculo corrigido por Gilton Guma <http://www.gsguma.com.br/>
*/
if (($this->largura / $this->nova_largura) > ($this->altura / $this->nova_altura)) {
$fator = $this->largura / $this->nova_largura;
} else {
$fator = $this->altura / $this->nova_altura;
}
$dif_w = $this->largura / $fator;
$dif_h = $this->altura / $fator;
// copia com o novo tamanho, centralizando
$dif_x = ($dif_x - $dif_w) / 2;
$dif_y = ($dif_y - $dif_h) / 2;
imagecopyresampled($this->img_temp, $this->img, $dif_x, $dif_y, 0, 0, $dif_w, $dif_h, $this->largura, $this->altura);
$this->img = $this->img_temp;
} // fim redimensionaPreenchimento()
/**
* Redimensiona imagem sem cropar, proporcionalmente e sem preenchimento.
* Modo proporcional adicionado por Fernando VR ( http://goo.gl/iDtmP )
* @return void
**/
private function redimensionaProporcional()
{
/**
* Verifica altura e largura proporcional.
**/
$ratio_orig = $this->largura / $this->altura;
if ($this->nova_largura / $this->nova_altura > $ratio_orig) {
$dif_w = $this->nova_altura * $ratio_orig;
$dif_h = $this->nova_altura;
} else {
$dif_w = $this->nova_largura;
$dif_h = $this->nova_largura / $ratio_orig;
}
// cria imagem de destino temporária
$this->img_temp = imagecreatetruecolor($dif_w, $dif_h);
// Resample
imagecopyresampled($this->img_temp, $this->img, 0, 0, 0, 0, $dif_w, $dif_h, $this->largura, $this->altura);
$this->img = $this->img_temp;
} // fim redimensionaProporcional()
/**
* Calcula a posição do crop
* Os índices 0 e 1 correspondem à posição x e y do crop na imagem
* Os índices 2 e 3 correspondem ao tamanho do crop
* @return void
**/
private function calculaPosicaoCrop()
{
// média altura/largura
$hm = $this->altura / $this->nova_altura;
$wm = $this->largura / $this->nova_largura;
// 50% para cálculo do crop
$h_height = $this->nova_altura / 2;
$h_width = $this->nova_largura / 2;
// calcula novas largura e altura
if (!is_array($this->posicao_crop)) {
if ($wm > $hm) {
$this->posicao_crop[2] = $this->largura / $hm;
$this->posicao_crop[3] = $this->nova_altura;
$this->posicao_crop[0] = ($this->posicao_crop[2] / 2) - $h_width;
$this->posicao_crop[1] = 0;
}
// largura <= altura
elseif (($wm <= $hm)) {
$this->posicao_crop[2] = $this->nova_largura;
$this->posicao_crop[3] = $this->altura / $wm;
$this->posicao_crop[0] = 0;
$this->posicao_crop[1] = ($this->posicao_crop[3] / 2) - $h_height;
}
}
} // fim calculaPosicaoCrop
/**
* Redimensiona imagem, cropando para encaixar no novo tamanho, sem sobras
* baseado no script original de Noah Winecoff
* http://www.findmotive.com/2006/12/13/php-crop-image/
* atualizado para receber o posicionamento X e Y e/ou Coordenadas Inteligentes
* do crop na imagem.
* Coordenadas Inteligentes implementado por Aires Gonçalves <[email protected]>
* @return void
**/
private function redimensionaCrop()
{
// calcula posicionamento do crop automaticamente
if (!is_array($this->posicao_crop)) {
$auto = 1;
$this->calculaPosicaoCrop();
}
// posicionamento do crop setado manualmente
else {
$auto = 0;
}
// cria imagem de destino temporária
$this->img_temp = imagecreatetruecolor($this->nova_largura, $this->nova_altura);
// adiciona cor de fundo à nova imagem
$this->preencheImagem();
//coordenadas inteligentes
switch ($this->posicao_crop[0]) {
case 'esquerdo':
$this->pos_x = 0;
break;
case 'direito':
$this->pos_x = $this->largura - $this->nova_largura;
break;
case 'meio':
$this->pos_x = ($this->largura - $this->nova_largura) / 2;
break;
default:
$this->pos_x = $this->posicao_crop[0];
break;
}
switch ($this->posicao_crop[1]) {
case 'topo':
$this->pos_y = 0;
break;
case 'inferior':
$this->pos_y = $this->altura - $this->nova_altura;
break;
case 'meio':
$this->pos_y = ($this->altura - $this->nova_altura) / 2;
break;
default:
$this->pos_y = $this->posicao_crop[1];
break;
}
$this->posicao_crop[0] = $this->pos_x;
$this->posicao_crop[1] = $this->pos_y;
if ($auto)
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);
else
imagecopyresampled($this->img_temp, $this->img, 0, 0, $this->posicao_crop[0], $this->posicao_crop[1], $this->nova_largura, $this->nova_altura, $this->posicao_crop[2], $this->posicao_crop[3]);
$this->img = $this->img_temp;
} // fim redimensionaCrop
/**
* flipa/inverte imagem
* baseado no script original de Noah Winecoff
* http://www.php.net/manual/en/ref.image.php#62029
* @param String $tipo tipo de espelhamento: h - horizontal, v - vertical
* @return Object instância atual do objeto, para métodos encadeados
**/
public function flip($tipo = 'h')
{
$w = imagesx($this->img);
$h = imagesy($this->img);
$this->img_temp = imagecreatetruecolor($w, $h);
// vertical
if ('v' == $tipo) {
for ($y = 0; $y < $h; $y++) {
imagecopy($this->img_temp, $this->img, 0, $y, 0, $h - $y - 1, $w, 1);
}
}
// horizontal
elseif ('h' == $tipo) {
for ($x = 0; $x < $w; $x++) {
imagecopy($this->img_temp, $this->img, $x, 0, $w - $x - 1, 0, 1, $h);
}
}
$this->img = $this->img_temp;
return $this;
} // fim flip
/**
* gira imagem
* @param Int $graus grau para giro
* @return Object instância atual do objeto, para métodos encadeados
**/
public function gira($graus)
{
$cor_fundo = imagecolorallocate($this->img, $this->rgb[0], $this->rgb[1], $this->rgb[2]);
$this->img = imagerotate($this->img, $graus, $cor_fundo);
imagealphablending($this->img, true);
imagesavealpha($this->img, true);
$this->largura = imagesx($this->img);
$this->altura = imagesx($this->img);
return $this;
} // fim girar
/**
* adiciona texto à imagem
* @param String $texto texto a ser inserido
* @param Int $tamanho tamanho da fonte
* Ver: http://br2.php.net/imagestring
* @param Int $x posição x do texto na imagem
* @param Int $y posição y do texto na imagem
* @param Array/String $cor_fundo array com cores RGB ou string com cor hexadecimal
* @param Boolean $truetype true para utilizar fonte truetype, false para fonte do sistema
* @param String $fonte nome da fonte truetype a ser utilizada
* @return void
**/
public function legenda($texto, $tamanho = 5, $x = 0, $y = 0, $cor_fundo = '', $truetype = false, $fonte = '')
{
$cor_texto = imagecolorallocate($this->img, $this->rgb[0], $this->rgb[1], $this->rgb[2]);
/**
* Define tamanho da legenda para posições fixas e fundo da legenda
**/
if ($truetype === true) {
$dimensoes_texto = imagettfbbox($tamanho, 0, $fonte, $texto);
$largura_texto = $dimensoes_texto[4];
$altura_texto = $tamanho;
} else {
if ($tamanho > 5)
$tamanho = 5;
$largura_texto = imagefontwidth($tamanho) * strlen($texto);
$altura_texto = imagefontheight($tamanho);
}
if (is_string($x) && is_string($y)) {
list($x, $y) = $this->calculaPosicaoLegenda($x . '_' . $y, $largura_texto, $altura_texto);
}
/**
* Cria uma nova imagem para usar de fundo da legenda
**/
if ($cor_fundo) {
if (is_array($cor_fundo)) {
$this->rgb = $cor_fundo;
} elseif (strlen($cor_fundo) > 3) {
$this->hexa($cor_fundo);
}
$this->img_temp = imagecreatetruecolor($largura_texto, $altura_texto);
$cor_fundo = imagecolorallocate($this->img_temp, $this->rgb[0], $this->rgb[1], $this->rgb[2]);
imagefill($this->img_temp, 0, 0, $cor_fundo);
imagecopy($this->img, $this->img_temp, $x, $y, 0, 0, $largura_texto, $altura_texto);
}
// truetype ou fonte do sistema?
if ($truetype === true) {
$y = $y + $tamanho;
imagettftext($this->img, $tamanho, 0, $x, $y, $cor_texto, $fonte, $texto);
} else {
imagestring($this->img, $tamanho, $x, $y, $texto, $cor_texto);
}
return $this;
} // fim legenda
/**
* Calcula a posição da legenda de acordo com string passada via parâmetro
*
* @param String $posicao valores pré-definidos (topo_esquerda, meio_centro etc.)
* @param Integer $largura largura da imagem
* @param Integer $altura altura da imagem
* @return void
**/
private function calculaPosicaoLegenda($posicao, $largura, $altura)
{
// define X e Y para posicionamento
switch ($posicao) {
case 'topo_esquerda':
$x = 0;
$y = 0;
break;
case 'topo_centro':
$x = ($this->largura - $largura) / 2;
$y = 0;
break;
case 'topo_direita':
$x = $this->largura - $largura;
$y = 0;
break;
case 'meio_esquerda':
$x = 0;
$y = ($this->altura / 2) - ($altura / 2);
break;
case 'meio_centro':
$x = ($this->largura - $largura) / 2;
$y = ($this->altura - $altura) / 2;
break;
case 'meio_direita':
$x = $this->largura - $largura;
$y = ($this->altura / 2) - ($altura / 2);
break;
case 'baixo_esquerda':
$x = 0;
$y = $this->altura - $altura;
break;
case 'baixo_centro':
$x = ($this->largura - $largura) / 2;
$y = $this->altura - $altura;
break;
case 'baixo_direita':
$x = $this->largura - $largura;
$y = $this->altura - $altura;
break;
default:
return false;
break;
} // end switch posicao
return array(
$x,
$y
);
} // fim calculaPosicaoLegenda
/**
* adiciona imagem de marca d'água
* @param String $imagem caminho da imagem de marca d'água
* @param Int/String $x posição x da marca na imagem ou constante para marcaFixa()
* @param Int/Sring $y posição y da marca na imagem ou constante para marcaFixa()
* @return Boolean true/false dependendo do resultado da operação
* @param Int $alfa valor para transparência (0-100)
* -> se utilizar alfa, a função imagecopymerge não preserva
* -> o alfa nativo do PNG
* @return Object instância atual do objeto, para métodos encadeados
**/
public function marca($imagem, $x = 0, $y = 0, $alfa = 100)
{
// cria imagem temporária para merge
if ($imagem) {
if (is_string($x) && is_string($y)) {
return $this->marcaFixa($imagem, $x . '_' . $y, $alfa);
}
$pathinfo = pathinfo($imagem);
switch (strtolower($pathinfo['extension'])) {
case 'jpg':
case 'jpeg':
$marcadagua = imagecreatefromjpeg($imagem);
break;
case 'png':
$marcadagua = imagecreatefrompng($imagem);
break;
case 'gif':
$marcadagua = imagecreatefromgif($imagem);
break;
case 'bmp':
$marcadagua = imagecreatefrombmp($imagem);
break;
default:
trigger_error('Arquivo de marca d\'água inválido.', E_USER_ERROR);
return false;
}
} else {
return false;
}
// dimensões
$marca_w = imagesx($marcadagua);
$marca_h = imagesy($marcadagua);
// retorna imagens com marca d'água
if (is_numeric($alfa) && (($alfa > 0) && ($alfa < 100))) {
imagecopymerge($this->img, $marcadagua, $x, $y, 0, 0, $marca_w, $marca_h, $alfa);
} else {
imagecopy($this->img, $marcadagua, $x, $y, 0, 0, $marca_w, $marca_h);
}
return $this;
} // fim marca
/**
* adiciona imagem de marca d'água, com valores fixos
* ex: topo_esquerda, topo_direita etc.
* Implementação original por Giolvani <[email protected]>
* @param String $imagem caminho da imagem de marca d'água
* @param String $posicao posição/orientação fixa da marca d'água
* [topo, meio, baixo] + [esquerda, centro, direita]
* @param Int $alfa valor para transparência (0-100)
* @return void
**/
private function marcaFixa($imagem, $posicao, $alfa = 100)
{
// dimensões da marca d'água
list($marca_w, $marca_h) = getimagesize($imagem);
// define X e Y para posicionamento
switch ($posicao) {
case 'topo_esquerda':
$x = 0;
$y = 0;
break;
case 'topo_centro':
$x = ($this->largura - $marca_w) / 2;
$y = 0;
break;
case 'topo_direita':
$x = $this->largura - $marca_w;
$y = 0;
break;
case 'meio_esquerda':
$x = 0;
$y = ($this->altura / 2) - ($marca_h / 2);
break;
case 'meio_centro':
$x = ($this->largura - $marca_w) / 2;
$y = ($this->altura / 2) - ($marca_h / 2);
break;
case 'meio_direita':
$x = $this->largura - $marca_w;
$y = ($this->altura / 2) - ($marca_h / 2);
break;
case 'baixo_esquerda':
$x = 0;
$y = $this->altura - $marca_h;
break;
case 'baixo_centro':
$x = ($this->largura - $marca_w) / 2;
$y = $this->altura - $marca_h;
break;
case 'baixo_direita':
$x = $this->largura - $marca_w;
$y = $this->altura - $marca_h;
break;
default:
return false;
break;
} // end switch posicao
// cria marca
$this->marca($imagem, $x, $y, $alfa);
return $this;
} // fim marcaFixa
/**
* Aplica filtros avançados como brilho, contraste, pixelate, blur
* Requer o GD compilado com a função imagefilter()
* http://br.php.net/imagefilter
* @param String $filtro constante/nome do filtro
* @param Integer $quantidade número de vezes que o filtro deve ser aplicado
* utilizado em blur, edge, emboss, pixel e rascunho
* @param $arg1, $arg2 e $arg3 - ver manual da função imagefilter
* @return Object instância atual do objeto, para métodos encadeados
**/
public function filtra($filtro, $quantidade = 1, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL)
{
switch ($filtro) {
case 'blur':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_GAUSSIAN_BLUR);
}
} else {
imagefilter($this->img, IMG_FILTER_GAUSSIAN_BLUR);
}
break;
case 'blur2':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_SELECTIVE_BLUR);
}
} else {
imagefilter($this->img, IMG_FILTER_SELECTIVE_BLUR);
}
break;
case 'brilho':
imagefilter($this->img, IMG_FILTER_BRIGHTNESS, $arg1);
break;
case 'cinzas':
imagefilter($this->img, IMG_FILTER_GRAYSCALE);
break;
case 'colorir':
imagefilter($this->img, IMG_FILTER_COLORIZE, $arg1, $arg2, $arg3, $arg4);
break;
case 'contraste':
imagefilter($this->img, IMG_FILTER_CONTRAST, $arg1);
break;
case 'edge':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_EDGEDETECT);
}
} else {
imagefilter($this->img, IMG_FILTER_EDGEDETECT);
}
break;
case 'emboss':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_EMBOSS);
}
} else {
imagefilter($this->img, IMG_FILTER_EMBOSS);
}
break;
case 'negativo':
imagefilter($this->img, IMG_FILTER_NEGATE);
break;
case 'ruido':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_MEAN_REMOVAL);
}
} else {
imagefilter($this->img, IMG_FILTER_MEAN_REMOVAL);
}
break;
case 'suave':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_SMOOTH, $arg1);
}
} else {
imagefilter($this->img, IMG_FILTER_SMOOTH, $arg1);
}
break;
// SOMENTE 5.3 ou superior
case 'pixel':
if (is_numeric($quantidade) && $quantidade > 1) {
for ($i = 1; $i <= $quantidade; $i++) {
imagefilter($this->img, IMG_FILTER_PIXELATE, $arg1, $arg2);
}
} else {
imagefilter($this->img, IMG_FILTER_PIXELATE, $arg1, $arg2);
}
break;
default:
break;
}
return $this;
} // fim filtrar
/**
Adiciona o melhor filtro para as imagens o sharpen | Jefferson Oliveira
Usa GD image objects
**/
function imagesharpen()
{
$qualidade = array(