source: trunk/spip/esqueleto-redcta/plugins/article_pdf/pdf/GifSplit.class.php @ 30

Last change on this file since 30 was 30, checked in by sebas, 17 years ago

nueva importacion del codigo del esqueleto de redcta con los plugins

File size: 14.8 KB
Line 
1<?php
2class GifSplit
3{
4/*===========================================*/
5/*== V A R I A B L E S ==*/
6/*===========================================*/
7var $gs_image_count = 0;
8var $gs_buffer = array();
9var $gs_global_data = array();
10var $gs_fileframe = array();
11var $gs_gif = array(0x47, 0x49, 0x46);
12var $gs_header = "\x47\x49\x46\x38\x39\x61"; //GIF89a
13var $gs_logical_screen_size;
14var $gs_logical_screen_descriptor;
15var $gs_global_color_table_size;
16var $gs_global_color_table_code;
17var $gs_global_color_table_flag;
18var $gs_global_image_data;
19var $gs_extension_lenght;
20var $gs_extension_type;
21var $gs_image_descriptor;
22var $gs_global_sorted;
23var $gs_fin;
24var $gs_fou;
25var $gs_sp;
26var $gs_fm;
27var $gs_es;
28var $gs_imnbr;
29var $gs_rsz;
30
31function GifSplit($image, $format, $name, $max_im_index='0', $resize='1')
32{
33error_reporting(0);
34$this->gs_fm = $format;
35$this->gs_sp = $name;
36$this->gs_imnbr = ($max_im_index=='')? '-1' : $max_im_index ; // maximal image index (from 0 to n)
37$this->gs_rsz = ($resize=='')? '0' : $resize ; //0: no change, 1: resize logical screen size to image size
38if ($this->gs_fm != 'GIF') $this->gs_rsz =1;
39
40if($this->gs_fin = fopen($image, "rb"))
41{
42$this->getbytes(6);
43if(!$this->arrcmp($this->gs_buffer, $this->gs_gif, 3))
44{
45$this->gs_es = "error #1";
46return(0);
47}
48/*étude du Logical Screen Descriptor
49      7 6 5 4 3 2 1 0        Field Name                    Type
50     +---------------+
51  0  |               |       Logical Screen Width          Unsigned
52     +-             -+
53  1  |               |
54     +---------------+
55  2  |               |       Logical Screen Height         Unsigned
56     +-             -+
57  3  |               |
58     +---------------+
59  4  | |     | |     |       <Packed Fields>               See below
60     +---------------+
61  5  |               |       Background Color Index        Byte
62     +---------------+
63  6  |               |       Pixel Aspect Ratio            Byte
64     +---------------+
65     <Packed Fields>  =      Global Color Table Flag       1 Bit
66                             Color Resolution              3 Bits
67                             Sort Flag                     1 Bit
68                             Size of Global Color Table    3 Bits
69*/
70//echo "début </br>" ;
71$this->getbytes(4);
72$this->gs_logical_screen_size = $this->gs_buffer;
73
74//$this->gs_buffer = array();
75$this->getbytes(3);
76$this->gs_logical_screen_descriptor = $this->gs_buffer;
77
78$this->gs_global_color_table_flag = ($this->gs_buffer[0] & 0x80) ? TRUE : FALSE;
79$this->gs_global_color_table_code = ($this->gs_buffer[0] & 0x07);
80$this->gs_global_color_table_size = pow(2,($this->gs_global_color_table_code+1));
81//$this->gs_global_color_table_size = 2 << $this->gs_global_color_table_code;
82$this->gs_global_sorted = ($this->gs_buffer[4] & 0x08) ? TRUE : FALSE;
83if($this->gs_global_color_table_flag)
84{
85$this->getbytes(3 * $this->gs_global_color_table_size);
86for($i = 0; $i < ((3 * $this->gs_global_color_table_size)); $i++)
87$this->gs_global_data[$i] = $this->gs_buffer[$i];
88}
89
90
91$this->gs_fou = '';
92
93for($loop = true; $loop; )
94{
95$this->getbytes(1);
96switch($this->gs_buffer[0])
97{
98case 0x21:
99$this->read_extension();
100break;
101case 0x2C:
102$this->read_image_descriptor();
103break;
104case 0x3B:
105$loop = false;
106break;
107default:
108$this->gs_es = sprintf("Unrecognized byte code %u\n<br>", $this->gs_buffer[0]); 
109}
110if (($this->gs_image_count > $this->gs_imnbr)and($this->gs_imnbr > -1))
111{
112$loop = false;
113}
114
115}
116fclose($this->gs_fin);
117}
118else
119{
120$this->gs_es = "error #2";
121return(0);
122}
123$this->gs_es = "ok";
124
125}
126/*///////////////////////////////////////////////*/
127/*// Function :: read_extension() //*/
128/*///////////////////////////////////////////////*/
129function read_extension()
130{
131/* Reset global variables */
132/* 0x21 puis 4 bytes discriminants:
133  0xF9 : Graphic Control Extension puis 3ème byte Block Size
134  0xFE : Comment Extension
135  0x01 : Plain Text Extension puis 3ème byte Block Size
136  0xFF : Application Extension puis 3ème byte Block Size
137      7 6 5 4 3 2 1 0        Field Name                    Type
138     +---------------+
139  0  |     0x21      |       
140     +-             -+
141  1  |     ????      |    |  0xF9  | 0xFE  |  0x01  |
142     +---------------+
143  2  |               |    | Taille | data  | Taille |
144     +-             -+
145  3  |               |    |  Data  | Data  |  Data  |
146     |               |     
147     |               |       
148     +---------------+
149     |     0x00      |     Block Terminator 
150     +---------------+
151
152      7 6 5 4 3 2 1 0        Field Name                 
153     +---------------+
154  0  |     0x21      |       
155     +-             -+
156  1  |     0xFF      |     
157     +---------------+
158  2  |     Taille    |      Taille 1ère extension
159     +-             -+
160  3  |               |       
161     |               |        Data
162     |               |       
163     +-             -+
164     |     Taille    |       Taille 2ème extension (structure à confirmer)
165     +-             -+
166     |               |       
167     |               |        Data
168     |               |     
169     +---------------+
170     |     0x00      |       Block Terminator
171     +---------------+
172
173*/ 
174$this->gs_fou .="\x21";
175$this->gs_buffer = array();
176$this->getbytes(2);
177$this->putbytes($this->gs_buffer, 2);
178$this->gs_extension_type = $this->gs_buffer[0];
179$this->gs_extension_lenght=$this->gs_buffer[1]; 
180if (array_search($this->gs_extension_type, array (1=>0xF9,2=>0x01,3=>0xFF)))
181{
182$this->getbytes($this->gs_extension_lenght);
183$this->putbytes($this->gs_buffer, $this->gs_extension_lenght);
184if ($this->gs_extension_type == 0xFF)
185  {
186        $this->getbytes(1);
187        $this->putbytes($this->gs_buffer, 1);
188  $this->gs_extension_lenght=$this->gs_buffer[0]; 
189        $this->getbytes($this->gs_extension_lenght);
190  $this->putbytes($this->gs_buffer, $this->gs_extension_lenght);
191  }
192}
193 for(;;)
194  {
195        $this->getbytes(1);
196  $this->putbytes($this->gs_buffer, 1);
197// byte == 0 : fin du data de l'extension
198  if ($this->gs_buffer[0] == 0)
199  {
200  break ;
201  }
202 }
203 
204
205}
206
207/*///////////////////////////////////////////////*/
208/*// Function :: read_image_descriptor() //*/
209/*///////////////////////////////////////////////*/
210function read_image_descriptor()
211{
212/* Reset global variables */
213$this->gs_buffer = array();
214
215//Lecture du descripteur de l'image: Image Descriptor
216/*
217      7 6 5 4 3 2 1 0        Field Name                    Type
218     +---------------+
219  0  |     0x2C      |       Image Separator               Byte
220     +---------------+
221  1  |               |       Image Left Position           Unsigned
222     +-             -+
223  2  |               |
224     +---------------+
225  3  |               |       Image Top Position            Unsigned
226     +-             -+
227  4  |               |
228     +---------------+
229  5  |               |       Image Width                   Unsigned
230     +-             -+
231  6  |               |
232     +---------------+
233  7  |               |       Image Height                  Unsigned
234     +-             -+
235  8  |               |
236     +---------------+
237  9  | | | |0 0|     |       <Packed Fields>               See below
238     +---------------+
239     <Packed Fields>  =      Local Color Table Flag        1 Bit
240                             Interlace Flag                1 Bit
241                             Sort Flag                     1 Bit
242                             Reserved                      2 Bits
243                             Size of Local Color Table     3 Bits
244*/
245
246$this->gs_fou .="\x2C";
247
248$this->getbytes(9);
249for($i = 0; $i < 9; $i++)
250{
251$this->gs_image_descriptor[$i] = $this->gs_buffer[$i];
252}
253
254if ($this->gs_rsz==1) // new screen sizes and image edges
255{
256// new logical screen size
257$this->gs_logical_screen_size[0] = $this->gs_image_descriptor[4];
258$this->gs_logical_screen_size[1] = $this->gs_image_descriptor[5];
259$this->gs_logical_screen_size[2] = $this->gs_image_descriptor[6];
260$this->gs_logical_screen_size[3] = $this->gs_image_descriptor[7];
261// reset position
262$this->gs_image_descriptor[0] = $this->gs_image_descriptor[1] = $this->gs_image_descriptor[2] = $this->gs_image_descriptor[3] = 0;
263}
264$this->putbytes($this->gs_image_descriptor, 9);
265
266$local_color_table_flag = ($this->gs_buffer[8] & 0x80) ? TRUE : FALSE;
267
268if($local_color_table_flag)
269{
270//il y a une table locale des couleurs
271$code = ($this->gs_buffer[8] & 0x07);
272$sorted = ($this->gs_buffer[8] & 0x20) ? TRUE : FALSE;
273$size = pow(2,($code+1));
274}
275
276if($local_color_table_flag)
277{
278$this->getbytes(3 * $size);
279$this->putbytes($this->gs_buffer, 3 * $size);
280}
281/* LZW minimum code size */
282$this->getbytes(1);
283$this->putbytes($this->gs_buffer, 1);
284
285/* Image Data */
286for(;;)
287{
288$this->getbytes(1);
289$this->putbytes($this->gs_buffer, 1);
290if(($u = $this->gs_buffer[0]) == 0)
291break;
292$this->getbytes($u);
293$this->putbytes($this->gs_buffer, $u);
294}
295
296$this->gs_global_image_data = $this->gs_fou;
297
298//Construction de la structure de tête du fichier
299
300// Header -> GIF89a //
301$this->gs_fou = $this->gs_header;
302
303//logical_screen_descriptor//
304$this->putbytes($this->gs_logical_screen_size,4);
305$this->putbytes($this->gs_logical_screen_descriptor,3);
306
307//Global Color Table//
308$this->putbytes($this->gs_global_data, $this->gs_global_color_table_size*3);
309
310//Global_image_data
311
312$this->gs_fou .= $this->gs_global_image_data;
313
314/* trailer */
315$this->gs_fou .= "\x3B";
316
317
318
319
320/* Write to file */
321switch($this->gs_fm)
322{
323case "GIF":
324//Enregistrement du fichier gif
325$framename = $this->gs_sp . $this->gs_image_count . ".gif";
326 if (!$handle = fopen($framename, 'w')) {
327         echo "Impossible d'ouvrir le fichier ($framename)";
328         exit;
329    }
330
331if(!fwrite($handle,$this->gs_fou))
332{
333$this->gs_es = "error #3";
334return(0);
335}
336
337$this->gs_fileframe[]=$framename;
338break;
339/* Write as BMP */
340case "BMP":
341$im = imageCreateFromString($this->gs_fou);
342$framename = $this->gs_sp . $this->gs_image_count . ".bmp";
343if(!$this->imageBmp($im, $framename))
344{
345$this->gs_es = "error #3";
346return(0);
347}
348imageDestroy($im);
349break;
350/* Write as PNG */
351case "PNG":
352$im = imageCreateFromString($this->gs_fou);
353$framename = $this->gs_sp . $this->gs_image_count . ".png";
354if(!imagePng($im, $framename))
355{
356$this->gs_es = "error #3";
357return(0);
358}
359imageDestroy($im);
360break;
361/* Write as JPG */
362case "JPG":
363$im = imageCreateFromString($this->gs_fou);
364$framename = $this->gs_sp . $this->gs_image_count . ".jpg";
365if(!imageJpeg($im, $framename))
366{
367$this->gs_es = "error #3";
368return(0);
369}
370imageDestroy($im);
371break;
372/* Write as GIF */
373case "GIF":
374$im = imageCreateFromString($this->gs_fou);
375
376imageDestroy($im);
377
378break;
379}
380$this->gs_image_count++;
381$this->gs_fou = '';
382}
383/*///////////////////////////////////////////////*/
384/*// BMP creation group //*/
385/*///////////////////////////////////////////////*/
386/* ImageBMP */
387function imageBmp($img, $file, $RLE=0)
388{
389$ColorCount = imagecolorstotal($img);
390$Transparent = imagecolortransparent($img);
391$IsTransparent = $Transparent != -1;
392if($IsTransparent)
393$ColorCount--;
394if($ColorCount == 0)
395{
396$ColorCount = 0;
397$BitCount = 24;
398}
399if(($ColorCount > 0) && ($ColorCount <= 2))
400{
401$ColorCount = 2;
402$BitCount = 1;
403}
404if(($ColorCount > 2) && ($ColorCount <= 16))
405{
406$ColorCount = 16;
407$BitCount = 4;
408}
409if(($ColorCount > 16) && ($ColorCount <= 256))
410{
411$ColorCount = 0;
412$BitCount = 8;
413}
414$Width = imageSX($img);
415$Height = imageSY($img);
416$Zbytek = (4 - ($Width / (8 / $BitCount)) % 4) % 4;
417if($BitCount < 24)
418$palsize = pow(2, $BitCount) * 4;
419$size = (floor($Width / (8 / $BitCount)) + $Zbytek) * $Height + 54;
420$size += $palsize;
421$offset = 54 + $palsize;
422// Bitmap File Header
423$ret = 'BM';
424$ret .= $this->int_to_dword($size);
425$ret .= $this->int_to_dword(0);
426$ret .= $this->int_to_dword($offset);
427// Bitmap Info Header
428$ret .= $this->int_to_dword(40);
429$ret .= $this->int_to_dword($Width);
430$ret .= $this->int_to_dword($Height);
431$ret .= $this->int_to_word(1);
432$ret .= $this->int_to_word($BitCount);
433$ret .= $this->int_to_dword($RLE);
434$ret .= $this->int_to_dword(0);
435$ret .= $this->int_to_dword(0);
436$ret .= $this->int_to_dword(0);
437$ret .= $this->int_to_dword(0);
438$ret .= $this->int_to_dword(0);
439// image data
440$CC = $ColorCount;
441$sl1 = strlen($ret);
442if($CC == 0)
443$CC = 256;
444if($BitCount < 24)
445{
446$ColorTotal = imagecolorstotal($img);
447if($IsTransparent)
448$ColorTotal--;
449for($p = 0; $p < $ColorTotal; $p++)
450{
451$color = imagecolorsforindex($img, $p);
452$ret .= $this->inttobyte($color["blue"]);
453$ret .= $this->inttobyte($color["green"]);
454$ret .= $this->inttobyte($color["red"]);
455$ret .= $this->inttobyte(0);
456}
457$CT = $ColorTotal;
458for($p = $ColorTotal; $p < $CC; $p++)
459{
460$ret .= $this->inttobyte(0);
461$ret .= $this->inttobyte(0);
462$ret .= $this->inttobyte(0);
463$ret .= $this->inttobyte(0);
464}
465}
466if($BitCount <= 8)
467{
468for($y = $Height - 1; $y >= 0; $y--)
469{
470$bWrite = "";
471for($x = 0; $x < $Width; $x++)
472{
473$color = imagecolorat($img, $x, $y);
474$bWrite .= $this->decbinx($color, $BitCount);
475if(strlen($bWrite) == 8)
476{
477$retd .= $this->inttobyte(bindec($bWrite));
478$bWrite = "";
479}
480}
481if((strlen($bWrite) < 8) and (strlen($bWrite) != 0))
482{
483$sl = strlen($bWrite);
484for($t = 0; $t < 8 - $sl; $t++)
485$sl .= "0";
486$retd .= $this->inttobyte(bindec($bWrite));
487}
488for($z = 0; $z < $Zbytek; $z++)
489$retd .= $this->inttobyte(0);
490}
491}
492if(($RLE == 1) and ($BitCount == 8))
493{
494for($t = 0; $t < strlen($retd); $t += 4)
495{
496if($t != 0)
497if(($t) % $Width == 0)
498$ret .= chr(0).chr(0);
499if(($t + 5) % $Width == 0)
500{
501$ret .= chr(0).chr(5).substr($retd, $t, 5).chr(0);
502$t += 1;
503}
504if(($t + 6) % $Width == 0)
505{
506$ret .= chr(0).chr(6).substr($retd, $t, 6);
507$t += 2;
508}
509else
510$ret .= chr(0).chr(4).substr($retd, $t, 4);
511}
512$ret .= chr(0).chr(1);
513}
514else
515$ret .= $retd;
516if($BitCount == 24)
517{
518for($z = 0; $z < $Zbytek; $z++)
519$Dopl .= chr(0);
520for($y = $Height - 1; $y >= 0; $y--)
521{
522for($x = 0; $x < $Width; $x++)
523{
524$color = imagecolorsforindex($img, ImageColorAt($img, $x, $y));
525$ret .= chr($color["blue"]).chr($color["green"]).chr($color["red"]);
526}
527$ret .= $Dopl;
528}
529}
530if(fwrite(fopen($file, "wb"), $ret))
531return true;
532else
533return false;
534}
535/* INT 2 WORD */
536function int_to_word($n)
537{
538return chr($n & 255).chr(($n >> 8) & 255);
539}
540/* INT 2 DWORD */
541function int_to_dword($n)
542{
543return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24)
544& 255); }
545/* INT 2 BYTE */
546function inttobyte($n)
547{
548return chr($n);
549}
550/* DECIMAL 2 BIN */
551function decbinx($d,$n)
552{
553$bin = decbin($d);
554$sbin = strlen($bin);
555for($j = 0; $j < $n - $sbin; $j++)
556$bin = "0$bin";
557return $bin;
558}
559/*///////////////////////////////////////////////*/
560/*// Function :: arrcmp() //*/
561/*///////////////////////////////////////////////*/
562function arrcmp($b, $s, $l)
563{
564for($i = 0; $i < $l; $i++)
565{
566if($s{$i} != $b{$i}) return false;
567}
568return true;
569}
570/*///////////////////////////////////////////////*/
571/*// Function :: getbytes() //*/
572/*///////////////////////////////////////////////*/
573function getbytes($l)
574{
575for($i = 0; $i < $l; $i++)
576{
577$bin = unpack('C*', fread($this->gs_fin, 1));
578$this->gs_buffer[$i] = $bin[1];
579}
580return $this->gs_buffer;
581}
582/*///////////////////////////////////////////////*/
583/*//           Function :: putbytes()          //*/
584/*///////////////////////////////////////////////*/
585function putbytes($s, $l)
586{
587for($i = 0; $i < $l; $i++)
588{
589$this->gs_fou .= pack('C*', $s[$i]);
590}
591}
592function getFilelist()
593{
594return $this->gs_fileframe;
595}
596
597function getReport()
598{
599return $this->gs_es;
600}
601}
602?>
Note: See TracBrowser for help on using the repository browser.