This repository has been archived by the owner on Nov 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.tx_metafeedit_export.php
3198 lines (2974 loc) · 110 KB
/
class.tx_metafeedit_export.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
/***************************************************************
* Copyright notice
*
* (c) 2006 Christophe BALISKY ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* This is a Class for generating records in some files like CSV, PDF EXCEL, it generated buttons too.
* called by fe_adminLib.php
* @author Christophe BALISKY <[email protected]>
*/
//define('FPDF_FONTPATH',t3lib_extMgm::extPath('meta_feedit').'res/fonts/');
define('EURO',chr(128));
if (t3lib_extMgm::isLoaded('fpdf')) require_once(t3lib_extMgm::extPath('fpdf').'class.tx_fpdf.php');
require_once(dirname(__FILE__).'/lib/tcpdf/tcpdf_barcodes_2d.php');
require_once(dirname(__FILE__).'/lib/tcpdf/tcpdf_barcodes_1d.php');
class tx_metafeedit_2DBarcode extends TCPDF2DBarcode {
/**
*
* @param unknown_type $w
* @param unknown_type $h
* @param unknown_type $color
*/
public function getBarcodeImage($w=3, $h=3, $color=array(0,0,0)) {
// calculate image size
//error_log(__METHOD__);
$width = ($this->barcode_array['num_cols'] * $w);
$height = ($this->barcode_array['num_rows'] * $h);
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new imagickpixel('rgb(255,255,255');
$fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
$png = new Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print barcode elements
$y = 0;
// for each row
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
$x = 0;
// for each column
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
if ($this->barcode_array['bcode'][$r][$c] == 1) {
// draw a single barcode cell
if ($imagick) {
$bar->rectangle($x, $y, ($x + $w - 1), ($y + $h - 1));
} else {
imagefilledrectangle($png, $x, $y, ($x + $w - 1), ($y + $h - 1), $fgcol);
}
}
$x += $w;
}
$y += $h;
}
$filename = 'typo3temp/Cache/Barcodes/2D' .uniqid() .'.png';
//error_log(__METHOD__.":$filename");
if ($imagick) {
$png->drawimage($bar);
} else {
imagepng($png,PATH_site .$filename);
imagedestroy($png);
return $filename;
}
}
}
class tx_metafeedit_1DBarcode extends TCPDFBarcode {
/**
* Return a PNG image representation of barcode (requires GD or Imagick library).
* @param $w (int) Width of a single bar element in pixels.
* @param $h (int) Height of a single bar element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return image or false in case of error.
* @public
*/
public function getBarcodeImage($w=2, $h=30, $color=array(0,0,0)) {
// calculate image size
$width = ($this->barcode_array['maxw'] * $w);
$height = $h;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new imagickpixel('rgb(255,255,255');
$fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
$png = new Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round(($v['w'] * $w), 3);
$bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
if ($v['t']) {
$y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
// draw a vertical bar
if ($imagick) {
$bar->rectangle($x, $y, ($x + $bw - 1), ($y + $bh - 1));
} else {
imagefilledrectangle($png, $x, $y, ($x + $bw - 1), ($y + $bh - 1), $fgcol);
}
}
$x += $bw;
}
$filename = 'typo3temp/Cache/Barcodes/1D' .uniqid() .'.png';
//error_log(__METHOD__.":$filename");
if ($imagick) {
$png->drawimage($bar);
} else {
imagepng($png,PATH_site .$filename);
imagedestroy($png);
return $filename;
}
}
}
class tx_metafeedit_pdf extends FPDF {
var $cellsize;
var $headercellsize;
var $footercellsize;
var $leftmargin;
var $topmargin;
var $rightmargin;
var $bottommargin;
var $nofooter=false;
var $caller;
var $conf;
var $javascript;
var $n_js;
function Header()
{
// Logo - present sur toutes les pages du pdf
//$logo = PATH_site.($xml->tr->td->img->dir).'logo.png';
//$this->Image($logo,1,1,17,8);
}
function Footer()
{
if (!$this->nofooter) {
$this->SetFont('Helvetica','',10);
$this->setFillColor(200,200,200);
//$this->SetY(200); //TODO
//$this->SetY(-$this->bottommargin); //TODO
$date=date('d/m/Y H:i:s');
$this->SetX($this->leftmargin);
$this->SetY(-$this->bottommargin);
//We draw line
$this->Cell(0,1,'','T',0,'C',0);
//$this->SetY(-$this->bottommargin);
$this->SetY(-$this->bottommargin+4);
$this->SetX($this->leftmargin);
$this->Cell(0,$this->footercellsize, $this->confTS[$this->pluginId.'.']['list.']['footer'], 0,0,'C',0);
$this->SetX($this->leftmargin);
$this->Cell(0,$this->footercellsize, $date, 0,0,'L');
$this->SetX($this->leftmargin);
$this->Cell(0,$this->footercellsize,'Page '.$this->PageNo().'/{nb}',0,0,'C');
$this->SetX($this->leftmargin);
$user = '';
$user = $GLOBALS['TSFE']->fe_user->user[username]?$GLOBALS['TSFE']->fe_user->user[username]:$this->caller->caller->metafeeditlib->getLL('anonymous', $this->caller->conf);
$this->Cell(0,$this->footercellsize, utf8_decode($this->caller->caller->metafeeditlib->getLL('printedby', $this->caller->conf)).$user,0,0,'R');
}
}
function IncludeJS($script) {
$this->javascript=$script;
}
function _putjavascript() {
$this->_newobj();
$this->n_js=$this->n;
$this->_out('<<');
$this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
$this->_out('>>');
$this->_out('endobj');
$this->_newobj();
$this->_out('<<');
$this->_out('/S /JavaScript');
$this->_out('/JS '.$this->_textstring($this->javascript));
$this->_out('>>');
$this->_out('endobj');
}
function _putresources() {
parent::_putresources();
if (!empty($this->javascript)) {
$this->_putjavascript();
}
}
function _putcatalog() {
parent::_putcatalog();
if (!empty($this->javascript)) {
$this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
}
}
// generates printer javascript depending on parameters and config
function generatePrintScript($print,$printer,$server) {
$dialog=true;
$autoprint=false;
//We add print dialog or not ...
switch ($print) {
case 'print':
case 'printnodialog':
$dialog=false;
$autoprint=true;
break;
case 'printdialog':
$autoprint=true;
break;
default:
break;
}
if ($autoprint) {
if ($printer && $server) {
$this->AutoPrintToNetPrinter($server, $printer, $dialog);
} elseif ($printer) {
$this->AutoPrintToPrinter( $printer, $dialog);
} else {
$this->AutoPrint($dialog);
}
}
}
/**
* We autoprint to defauklt printer
* @param bool $dialog
*/
function AutoPrint($dialog=false)
{
//Open the print dialog or start printing immediately on the standard printer
$param=($dialog ? 'true' : 'false');
$script=$dialog?"print(true);":"if(typeof JSSilentPrint != 'undefined') {JSSilentPrint(this);}else{print(false);};";
//$script=$dialog?"print(true);":"if(typeof JSSilentPrint != 'undefined') {app.alert('silent');JSSilentPrint(this);}else{app.alert('notsilent');print(false);};";
//$script=$dialog?"print(true);":"app.alert('yoi');if(typeof JSSilentPrint == 'undefined') {app.alert('notsilent');};";
$this->IncludeJS($script);
//$this->IncludeJS("app.alert('yop');".$script);
}
/**
* We autoprint to printer by printername on network
* @param string $server
* @param string $printer
* @param boolean $dialog
*/
function AutoPrintToNetPrinter($server, $printer, $dialog=false)
{
//Print on a shared printer (requires at least Acrobat 6)
if($dialog) {
$script = "var pp = getPrintParams();";
$script .= "pp.interactive = pp.constants.interactionLevel.full;";
$script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
$script .= "print(pp);";
}else{
$script="if(typeof JSSilentPrintOnNetPrinter != 'undefined') {JSSilentPrintOnNetPrinter(this,'".$printer."','".$server."');}else{";
$script .= "var pp = getPrintParams();";
$script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
$script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
$script .= "print(pp);}";
}
$this->IncludeJS($script);
}
/**
* We autoprint to printer by printername
*
* @param string $printer
* @param bool $dialog (do we try to print with dialog or not)
*/
function AutoPrintToPrinter( $printer, $dialog=false)
{
//Print on a shared printer (requires at least Acrobat 6)
if($dialog) {
$script = "var pp = getPrintParams();";
$script .= "pp.interactive = pp.constants.interactionLevel.full;";
$script .= "pp.printerName = '".$printer."';";
$script .= "print(pp);";
} else {
$script="if(typeof JSSilentPrintOnPrinter != 'undefined') {JSSilentPrintOnPrinter(this,'".$printer."');}else{";
$script .= "var pp = getPrintParams();";
$script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
$script .= "pp.printerName = '".$printer."';";
$script .= "print(pp);}";
}
$this->IncludeJS($script);
}
/**
*
* Counts line numbers for multi-cell
* @param width $w
* @param string $txt
* @return number
*/
function NbLines($w,$txt)
{
$dbg=false;//$dbg=(substr($txt,0,5)=="Adler");
if ($dbg) error_log('Nblines '.$txt." - $w");
//Calcule le nombre de lignes qu'occupe un MultiCell de largeur w
$cw=&$this->CurrentFont['cw'];
//error_log(__METHOD__.":".print_r($this->CurrentFont,true));
//error_log(__METHOD__.": ".print_r(array_values($cw),true));
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
//$wmax=$w*1400/$this->FontSize;
//$wmax=$w*1000*$this->k;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
//$wmax=($w-2*$this->cMargin)/$this->k;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
//Removes trailing carriage return
if($nb>0 and $s[$nb-1]=="\n")
$nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
//error_log(__METHOD__.": $w,$this->cMargin,$this->FontSize, $wmax");
while($i<$nb)
{
$c=$s[$i];
//Cariage return we add line
if($c=="\n")
{
$i++;
$sep=-1;
$j=$i;
$l=0;
$nl++;
//error_log('NewLine');
continue;
}
//Space seperator
if($c==' ') $sep=$i;
$l+=$cw[$c];
if ($dbg) error_log(__METHOD__.": $c,$l,$wmax");
if($l>$wmax)
{
if ($dbg) error_log('$l > $wmax');
if($sep==-1)
{
if($i==$j)
$i++;
}
else
$i=$sep+1;
$sep=-1;
$j=$i;
$l=0;
$nl++;
}
else
$i++;
}
return $nl;
}
/*******************************************************************************
* *
* Public methods *
* *
*******************************************************************************/
function Image($file,$x=Null,$y=Null,$w=0,$h=0,$type='',$link='', $isMask=false, $maskImg=0){
//Put an image on the page
if(!isset($this->images[$file])){
//First use of image, get info
if($type==''){
$pos=strrpos($file,'.');
if(!$pos)
$this->Error('Image file has no extension and no type was specified: '.$file);
$type=substr($file,$pos+1);
}
$imgInfo = getimagesize($file);
$type=strtolower($type);
$mimeType=explode('/',$imgInfo['mime']);
if (count($mimeType)==2) $type=$mimeType[1];
if(version_compare(PHP_VERSION, '5.3.0', '<')){
$mqr=get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
}
if($type=='jpg' || $type=='jpeg')
$info=$this->_parsejpg($file);
elseif($type=='png'){
$info=$this->_parsepng($file);
if ($info=='alpha') return $this->ImagePngWithAlpha($file,$x,$y,$w,$h,$link);
}else{
//Allow for additional formats
$mtd='_parse'.$type;
if(!method_exists($this,$mtd))
$this->Error('Unsupported image type: '.$type);
$info=$this->$mtd($file);
}
if(version_compare(PHP_VERSION, '5.3.0', '<')){
set_magic_quotes_runtime($mqr);
}
if ($isMask){
$info['cs']="DeviceGray"; // try to force grayscale (instead of indexed)
}
$info['i']=count($this->images)+1;
if ($maskImg>0) $info['masked'] = $maskImg;###
$this->images[$file]=$info;
}else
$info=$this->images[$file];
//Automatic width and height calculation if needed
if($w==0 && $h==0)
{
//Put image at 72 dpi
$w=$info['w']/$this->k;
$h=$info['h']/$this->k;
}
if($w==0)
$w=$h*$info['w']/$info['h'];
if($h==0)
$h=$w*$info['h']/$info['w'];
// embed hidden, ouside the canvas
if ((float)FPDF_VERSION>=1.7){
if ($isMask) $x = ($this->CurOrientation=='P'?$this->CurPageSize[0]:$this->CurPageSize[1]) + 10;
}else{
if ($isMask) $x = ($this->CurOrientation=='P'?$this->CurPageFormat[0]:$this->CurPageFormat[1]) + 10;
}
$this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
if($link)
$this->Link($x,$y,$w,$h,$link);
return $info['i'];
}
// needs GD 2.x extension
// pixel-wise operation, not very fast
function ImagePngWithAlpha($file,$x,$y,$w=0,$h=0,$link='')
{
$tmp_alpha = tempnam('.', 'mska');
$this->tmpFiles[] = $tmp_alpha;
$tmp_plain = tempnam('.', 'mskp');
$this->tmpFiles[] = $tmp_plain;
list($wpx, $hpx) = getimagesize($file);
$img = imagecreatefrompng($file);
$alpha_img = imagecreate( $wpx, $hpx );
// generate gray scale pallete
for($c=0;$c<256;$c++) ImageColorAllocate($alpha_img, $c, $c, $c);
// extract alpha channel
$xpx=0;
while ($xpx<$wpx){
$ypx = 0;
while ($ypx<$hpx){
$color_index = imagecolorat($img, $xpx, $ypx);
$alpha = 255-($color_index>>24)*255/127; // GD alpha component: 7 bit only, 0..127!
imagesetpixel($alpha_img, $xpx, $ypx, $alpha);
++$ypx;
}
++$xpx;
}
imagepng($alpha_img, $tmp_alpha);
imagedestroy($alpha_img);
// extract image without alpha channel
$plain_img = imagecreatetruecolor ( $wpx, $hpx );
imagecopy ($plain_img, $img, 0, 0, 0, 0, $wpx, $hpx );
imagepng($plain_img, $tmp_plain);
imagedestroy($plain_img);
//first embed mask image (w, h, x, will be ignored)
$maskImg = $this->Image($tmp_alpha, 0,0,0,0, 'PNG', '', true);
//embed image, masked with previously embedded mask
$this->Image($tmp_plain,$x,$y,$w,$h,'PNG',$link, false, $maskImg);
}
function Close()
{
parent::Close();
// clean up tmp files
if (is_array($this->tmpFiles)) foreach($this->tmpFiles as $tmp) @unlink($tmp);
}
/*******************************************************************************
* *
* Private methods *
* *
*******************************************************************************/
function _putimages()
{
$filter=($this->compress) ? '/Filter /FlateDecode ' : '';
reset($this->images);
while(list($file,$info)=each($this->images))
{
$this->_newobj();
$this->images[$file]['n']=$this->n;
$this->_out('<</Type /XObject');
$this->_out('/Subtype /Image');
$this->_out('/Width '.$info['w']);
$this->_out('/Height '.$info['h']);
if (isset($info["masked"])) $this->_out('/SMask '.($this->n-1).' 0 R'); ###
if($info['cs']=='Indexed')
$this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
else
{
$this->_out('/ColorSpace /'.$info['cs']);
if($info['cs']=='DeviceCMYK')
$this->_out('/Decode [1 0 1 0 1 0 1 0]');
}
$this->_out('/BitsPerComponent '.$info['bpc']);
if(isset($info['f']))
$this->_out('/Filter /'.$info['f']);
if(isset($info['parms']))
$this->_out($info['parms']);
if(isset($info['trns']) && is_array($info['trns']))
{
$trns='';
for($i=0;$i<count($info['trns']);$i++)
$trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
$this->_out('/Mask ['.$trns.']');
}
$this->_out('/Length '.strlen($info['data']).'>>');
$this->_putstream($info['data']);
unset($this->images[$file]['data']);
$this->_out('endobj');
//Palette
if($info['cs']=='Indexed')
{
$this->_newobj();
$pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
$this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
$this->_putstream($pal);
$this->_out('endobj');
}
}
}
// this method overwriing the original version is only needed to make the Image method support PNGs with alpha channels.
// if you only use the ImagePngWithAlpha method for such PNGs, you can remove it from this script.
function _parsepng($file)
{
//Extract info from a PNG file
$f=fopen($file,'rb');
if(!$f)
$this->Error('Can\'t open image file: '.$file);
//Check signature
if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
$this->Error('Not a PNG file: '.$file);
//Read header chunk
fread($f,4);
if(fread($f,4)!='IHDR')
$this->Error('Incorrect PNG file: '.$file);
$w=$this->_readint($f);
$h=$this->_readint($f);
$bpc=ord(fread($f,1));
if($bpc>8)
$this->Error('16-bit depth not supported: '.$file);
$ct=ord(fread($f,1));
if($ct==0)
$colspace='DeviceGray';
elseif($ct==2)
$colspace='DeviceRGB';
elseif($ct==3)
$colspace='Indexed';
else {
fclose($f); // the only changes are
return 'alpha'; // made in those 2 lines
}
if(ord(fread($f,1))!=0)
$this->Error('Unknown compression method: '.$file);
if(ord(fread($f,1))!=0)
$this->Error('Unknown filter method: '.$file);
if(ord(fread($f,1))!=0)
$this->Error('Interlacing not supported: '.$file);
fread($f,4);
$parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
//Scan chunks looking for palette, transparency and image data
$pal='';
$trns='';
$data='';
do
{
$n=$this->_readint($f);
$type=fread($f,4);
if($type=='PLTE')
{
//Read palette
$pal=fread($f,$n);
fread($f,4);
}
elseif($type=='tRNS')
{
//Read transparency info
$t=fread($f,$n);
if($ct==0)
$trns=array(ord(substr($t,1,1)));
elseif($ct==2)
$trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
else
{
$pos=strpos($t,chr(0));
if($pos!==false)
$trns=array($pos);
}
fread($f,4);
}
elseif($type=='IDAT')
{
//Read image data block
$data.=fread($f,$n);
fread($f,4);
}
elseif($type=='IEND')
break;
else
fread($f,$n+4);
}
while($n);
if($colspace=='Indexed' && empty($pal))
$this->Error('Missing palette in '.$file);
fclose($f);
return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
}
function ClippingText($x, $y, $txt, $outline=false)
{
$op=$outline ? 5 : 7;
$this->_out(sprintf('q BT %.2f %.2f Td %d Tr (%s) Tj 0 Tr ET',
$x*$this->k,
($this->h-$y)*$this->k,
$op,
$this->_escape($txt)));
}
function ClippingRect($x, $y, $w, $h, $outline=false)
{
$op=$outline ? 'S' : 'n';
$this->_out(sprintf('q %.2f %.2f %.2f %.2f re W %s',
$x*$this->k,
($this->h-$y)*$this->k,
$w*$this->k, -$h*$this->k,
$op));
}
function ClippingEllipse($x, $y, $rx, $ry, $outline=false)
{
$op=$outline ? 'S' : 'n';
$lx=4/3*(M_SQRT2-1)*$rx;
$ly=4/3*(M_SQRT2-1)*$ry;
$k=$this->k;
$h=$this->h;
$this->_out(sprintf('q %.2f %.2f m %.2f %.2f %.2f %.2f %.2f %.2f c',
($x+$rx)*$k, ($h-$y)*$k,
($x+$rx)*$k, ($h-($y-$ly))*$k,
($x+$lx)*$k, ($h-($y-$ry))*$k,
$x*$k, ($h-($y-$ry))*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
($x-$lx)*$k, ($h-($y-$ry))*$k,
($x-$rx)*$k, ($h-($y-$ly))*$k,
($x-$rx)*$k, ($h-$y)*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
($x-$rx)*$k, ($h-($y+$ly))*$k,
($x-$lx)*$k, ($h-($y+$ry))*$k,
$x*$k, ($h-($y+$ry))*$k));
$this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c W %s',
($x+$lx)*$k, ($h-($y+$ry))*$k,
($x+$rx)*$k, ($h-($y+$ly))*$k,
($x+$rx)*$k, ($h-$y)*$k,
$op));
}
function UnsetClipping()
{
$this->_out('Q');
}
function ClippedCell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='')
{
if($border || $fill || $this->y+$h>$this->PageBreakTrigger)
{
$this->Cell($w, $h, '', $border, 0, '', $fill);
$this->x-=$w;
}
$this->ClippingRect($this->x, $this->y, $w, $h);
$this->Cell($w, $h, $txt, '', $ln, $align,0, $link);
$this->UnsetClipping();
}
function ClippedMultiCell($w, $h=0,$lh=0, $txt='', $border=0, $align='', $fill=false) {
if($border || $fill || $this->y+$h>$this->PageBreakTrigger)
{
$this->Cell($w, $h, '', $border, 0, '', $fill);
$this->x-=$w;
}
$this->ClippingRect($this->x, $this->y, $w, $h);
//MultiCell($this->cellWidth,$this->height,$val,0,$p,false);
$this->MultiCell($w, $lh, $txt, 0, $align,$fill);
$this->UnsetClipping();
}
//**************************************************************************
// This extension adds support for windows bitmaps (*.bmp, *.rle)
//
//**************************************************************************
function _parsebmp ($file){
$fsize = filesize($file);
$f = fopen($file, 'rb');
# bmpfileheader (0..13)
$bfOffBits = $this->_fread_long_le_at($f, 10);
# bmpinfoheader (14..53)
$width = $this->_fread_long_le_at($f, 18);
$height = $this->_fread_long_le_at($f, 22);
$flip = ($height<0);
if ($flip) $height =-$height;
$biBitCount = $this->_fread_short_le_at($f, 28);
$biCompression = $this->_fread_long_le_at($f, 30); # BI_RGB = 0, BI_RLE8 = 1, BI_RLE4 = 2
$info = array('w'=>$width, 'h'=>$height);
fseek($f, 54);
if ($biBitCount<16){
$info['cs'] = 'Indexed';
$info['bpc'] = $biBitCount;
$palStr = fread($f, $bfOffBits-54); # palette
$pal = '';
$cnt = strlen($palStr)/4;
for ($i=0;$i<$cnt;$i++){
$n = 4*$i;
$pal .= $palStr{$n+2}.$palStr{$n+1}.$palStr{$n};
}
$info['pal'] = $pal;
}else{
$info['cs'] = 'DeviceRGB';
$info['bpc'] = 8;
}
# image data
switch ($biCompression){
case 0:
$str = fread($f, $fsize-$bfOffBits);
break;
case 1: # BI_RLE8
$str = $this->rle8_decode(fread($f, $fsize-$bfOffBits), $width);
break;
case 2: # BI_RLE4
$str = $this->rle4_decode(fread($f, $fsize-$bfOffBits), $width);
break;
}
$data = '';
$padCnt = (4-ceil(($width/(8/$biBitCount)))%4)%4;
switch ($biBitCount){
case 1:
case 4:
case 8:
$w = floor($width/(8/$biBitCount)) + ($width%(8/$biBitCount)?1:0);
$w_row = $w + $padCnt;
if ($flip){
for ($y=0;$y<$height;$y++){
$y0 = $y*$w_row;
for ($x=0;$x<$w;$x++)
$data .= $str{$y0+$x};
}
}else{
for ($y=$height-1;$y>=0;$y--){
$y0 = $y*$w_row;
for ($x=0;$x<$w;$x++)
$data .= $str{$y0+$x};
}
}
break;
case 16:
$w_row = $width*2 + $padCnt;
if ($flip){
for ($y=0;$y<$height;$y++){
$y0 = $y*$w_row;
for ($x=0;$x<$width;$x++){
$n = (ord( $str{$y0 + 2*$x + 1})*256 + ord( $str{$y0 + 2*$x}));
$b = ($n & 31)<<3; $g = ($n & 992)>>2; $r = ($n & 31744)>>7128;
$data .= chr($r) . chr($g) . chr($b);
}
}
}else{
for ($y=$height-1;$y>=0;$y--){
$y0 = $y*$w_row;
for ($x=0;$x<$width;$x++){
$n = (ord( $str{$y0 + 2*$x + 1})*256 + ord( $str{$y0 + 2*$x}));
$b = ($n & 31)<<3; $g = ($n & 992)>>2; $r = ($n & 31744)>>7;
$data .= chr($r) . chr($g) . chr($b);
}
}
}
break;
case 24:
case 32:
##$padCnt = $width % 4;
$byteCnt = $biBitCount/8;
$w_row = $width*$byteCnt + $padCnt;
if ($flip){
for ($y=0;$y<$height;$y++){
$y0 = $y*$w_row;
for ($x=0;$x<$width;$x++){
$i = $y0 + $x*$byteCnt ; # + 1
$data .= $str{$i+2}.$str{$i+1}.$str{$i};
}
}
}else{
for ($y=$height-1;$y>=0;$y--){
$y0 = $y*$w_row;
for ($x=0;$x<$width;$x++){
$i = $y0 + $x*$byteCnt ; # + 1
$data .= $str{$i+2}.$str{$i+1}.$str{$i};
}
}
}
break;
default:
$this->Error('Unsupported image biBitCount: '.$file);
}
# compress data
$info['f'] = 'FlateDecode';
$info['data'] = gzcompress($data);
return $info;
}
function _fread_long_le_at($f, $pos){
fseek($f, $pos);
$a=unpack('Vi', fread($f, 4));
return $a['i'];
}
function _fread_short_le_at($f, $pos){
fseek($f, $pos);
$a=unpack('vi', fread($f, 2));
return $a['i'];
}
# Decoder for RLE8 compression in windows bitmaps
# see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_6x0u.asp
function rle8_decode ($str, $width){
$lineWidth = $width + (3 - ($width-1) % 4);
$out = '';
$cnt = strlen($str);
for ($i=0;$i<$cnt;$i++){
$o = ord($str{$i});
switch ($o){
case 0: # ESCAPE
$i++;
switch (ord($str{$i})){
case 0: # NEW LINE
$padCnt = $lineWidth - strlen($out)%$lineWidth;
if ($padCnt<$lineWidth) $out .= str_repeat(chr(0), $padCnt); # pad line
break;
case 1: # END OF FILE
$padCnt = $lineWidth - strlen($out)%$lineWidth;
if ($padCnt<$lineWidth) $out .= str_repeat(chr(0), $padCnt); # pad line
break 3;
case 2: # DELTA
$i += 2;
break;
default: # ABSOLUTE MODE
$num = ord($str{$i});
for ($j=0;$j<$num;$j++)
$out .= $str{++$i};
if ($num % 2) $i++;
}
break;
default:
$out .= str_repeat($str{++$i}, $o);
}
}
return $out;
}
# Decoder for RLE4 compression in windows bitmaps
# see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_6x0u.asp
function rle4_decode ($str, $width){
$w = floor($width/2) + ($width % 2);
$lineWidth = $w + (3 - ( ($width-1) / 2) % 4);
$pixels = array();
$cnt = strlen($str);
for ($i=0;$i<$cnt;$i++){
$o = ord($str{$i});
switch ($o){
case 0: # ESCAPE
$i++;
switch (ord($str{$i})){
case 0: # NEW LINE
while (count($pixels)%$lineWidth!=0)
$pixels[]=0;
break;
case 1: # END OF FILE
while (count($pixels)%$lineWidth!=0)
$pixels[]=0;
break 3;
case 2: # DELTA
$i += 2;
break;
default: # ABSOLUTE MODE
$num = ord($str{$i});
for ($j=0;$j<$num;$j++){
if ($j%2==0){
$c = ord($str{++$i});
$pixels[] = ($c & 240)>>4;
} else
$pixels[] = $c & 15;
}
if ($num % 2) $i++;
}
break;
default:
$c = ord($str{++$i});
for ($j=0;$j<$o;$j++)
$pixels[] = ($j%2==0 ? ($c & 240)>>4 : $c & 15);
}
}
$out = '';
if (count($pixels)%2) $pixels[]=0;
$cnt = count($pixels)/2;
for ($i=0;$i<$cnt;$i++)
$out .= chr(16*$pixels[2*$i] + $pixels[2*$i+1]);
return $out;
}
}
class tx_metafeedit_export {
var $prefixId = "tx_metafeedit"; // Same as class name
var $conf; // $conf array from metafeedit_lib
var $pluginId; // ID du plugin
var $confTS; // TS du plugin
var $cObj;
var $caller;
/**