/[drupal]/contributions/modules/drawing/modules/drawing_gd/drawing_gd_gif89a.inc
ViewVC logotype

Diff of /contributions/modules/drawing/modules/drawing_gd/drawing_gd_gif89a.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Sat May 9 05:56:28 2009 UTC revision 1.1.2.1, Sat May 9 05:56:28 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * The drawing_gd_gif89a class.  Builds animated gifs from source gifs.
7     * Based on gifEncoder Version 2.0 by László Zsidi, http://gifs.hu.
8     */
9    
10    class drawing_gd_gif89a {
11      var $gif = "gif89a"; // gif header 6 bytes
12    
13      var $buffer = Array ();
14      var $loop =  0;
15      var $disposal = 2;
16      var $color = -1;
17      var $image = -1;
18      var $error = FALSE;
19    
20      var $version = "drawing_gd_gif98a: ";
21      var $errors = Array (
22        'e0' => $version ."function not supported for only one image.",
23        'e1' => $version ."source is not a gif image.",
24        'e2' => $version ."unintelligible flag.",
25        'e3' => $version ."cannot make animation from animated gif source.",
26     );
27    
28      /**
29       * drawing_gd_gif89a().
30       */
31      function drawing_gd_gif89a($gif_sources, $gif_delay, $gif_loop, $gif_disposal, $gif_red, $gif_green, $gif_blue, $gif_mode) {
32        if (!is_array($gif_sources) && !is_array($gif_delay)) {
33            $this->error = drupal_set_message(t($this->errors['e0']), 'error');
34            return FALSE;
35        }
36        $this->loop = ($gif_loop > -1) ? $gif_loop : 0;
37        $this->disposal = ($gif_disposal > -1) ? (($gif_disposal < 3) ? $gif_disposal : 3) : 2;
38        $this->color = ($gif_red > -1 && $gif_green > -1 && $gif_blue > -1) ?
39                        ($gif_red | ($gif_green << 8) | ($gif_blue << 16)) : -1;
40    
41        for ($i = 0; $i < count($gif_sources); $i++) {
42          if (strtolower($gif_mode) == "url") {
43            $this->buffer[] = fread(fopen($gif_sources[$i], "rb"), filesize($gif_sources[$i]));
44          }
45          else if (strtolower($gif_mode) == "bin") {
46            $this->buffer[] = $gif_sources[$i];
47          }
48          else {
49            $this->error = drupal_set_message(t($this->errors['e2'] .' (!arg)', array('!arg' => $gif_mode)), 'error');
50            return FALSE;
51          }
52          if (substr($this->buffer[$i], 0, 6) != "gif87a" && substr($this->buffer[$i], 0, 6) != "gif89a") {
53            $this->error = drupal_set_message(t($this->errors['e1'] .' (!arg)', array('!arg' => $i)), 'error');
54            return FALSE;
55          }
56          for ($j = (13 + 3 * (2 << (ord($this->buffer[$i]{10}) & 0x07))), $k = TRUE; $k; $j++) {
57            switch ($this->buffer[$i]{$j}) {
58              case "!":
59                if ((substr($this->buffer[$i], ($j + 3), 8)) == "NETSCAPE") {
60                  $this->error = drupal_set_message(t($this->errors['e3'] .' (!arg)', array('!arg' => ($i + 1))), 'error');
61                  return FALSE;
62                }
63                break;
64              case ";":
65                $k = FALSE;
66                break;
67            }
68          }
69        }
70        drawing_gd_gif89a::gif_add_header();
71        for ($i = 0; $i < count($this->buffer); $i++) {
72          drawing_gd_gif89a::gif_add_frames($i, $gif_delay[$i]);
73        }
74        drawing_gd_gif89a::gif_add_footer();
75      }
76    
77      /**
78       * gif_add_header().
79       */
80      function gif_add_header() {
81        $cmap = 0;
82        if (ord($this->buffer[0]{10}) & 0x80) {
83            $cmap = 3 * (2 << (ord($this->buffer[0]{10}) & 0x07));
84            $this->gif .= substr($this->buffer[0], 6, 7);
85            $this->gif .= substr($this->buffer[0], 13, $cmap);
86            $this->gif .= "!\377\13NETSCAPE2.0\3\1" . drawing_gd_gif89a::gif_word($this->loop) ."\0";
87        }
88      }
89    
90      /**
91       * gif_add_frames().
92       */
93      function gif_add_frames($i, $d) {
94        $locals_str = 13 + 3 * (2 << (ord($this->buffer[$i]{10}) & 0x07));
95        $locals_end = strlen($this->buffer[$i]) - $locals_str - 1;
96        $locals_tmp = substr($this->buffer[$i], $locals_str, $locals_end);
97        $global_len = 2 << (ord($this->buffer[0]{10}) & 0x07);
98        $locals_len = 2 << (ord($this->buffer[$i]{10}) & 0x07);
99        $global_rgb = substr($this->buffer[0], 13, 3 * 2 << (
100          ord($this->buffer[0]{10}) & 0x07)));
101        $locals_rgb = substr($this->buffer[$i], 13, 3 * (2 << (
102          ord($this->buffer[$i]{10}) & 0x07)));
103        $locals_ext = "!\xF9\x04". chr(($this->disposal << 2) + 0) .
104          chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) ."\x0\x0";
105        if ($this->color > -1 && ord($this->buffer[$i]{10}) & 0x80) {
106          for ($j = 0; $j < (2 << (ord($this->buffer[$i]{10}) & 0x07)); $j++) {
107            if (ord($locals_rgb {3 * $j + 0}) == (($this->color >> 16) & 0xFF) &&
108            ord($locals_rgb {3 * $j + 1}) == (($this->color >>  8) & 0xFF) &&
109            ord($locals_rgb {3 * $j + 2}) == (($this->color >>  0) & 0xFF)) {
110              $locals_ext = "!\xF9\x04" . chr(($this->disposal << 2) + 1) .
111                chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) ."\x0";
112              break;
113            }
114          }
115        }
116        switch ($locals_tmp{0}) {
117          case "!":
118            $locals_img = substr($locals_tmp, 8, 10);
119            $locals_tmp = substr($locals_tmp, 18, strlen($locals_tmp) - 18);
120            break;
121          case ",":
122            $locals_img = substr($locals_tmp, 0, 10);
123            $locals_tmp = substr($locals_tmp, 10, strlen($locals_tmp) - 10);
124            break;
125        }
126        if (ord($this->buffer[$i]{10}) & 0x80 && $this->image > -1) {
127          if ($global_len == $locals_len) {
128            if (drawing_gd_gif89a::gif_block_compare($global_rgb, $locals_rgb, $global_len)) {
129              $this->gif .= ($locals_ext . $locals_img . $locals_tmp);
130            }
131            else {
132              $byte  = ord($locals_img {9});
133              $byte |= 0x80;
134              $byte &= 0xF8;
135              $byte |= (ord($this->buffer[0]{10}) & 0x07);
136              $locals_img{9} = chr($byte);
137              $this->gif .= ($locals_ext . $locals_img . $locals_rgb . $locals_tmp);
138            }
139          }
140          else {
141            $byte = ord($locals_img{9});
142            $byte |= 0x80;
143            $byte &= 0xF8;
144            $byte |= (ord($this->buffer[$i]{10}) & 0x07);
145            $locals_img{9} = chr($byte);
146            $this->gif .= ($locals_ext . $locals_img . $locals_rgb . $locals_tmp);
147          }
148        }
149        else {
150          $this->gif .= ($locals_ext . $locals_img . $locals_tmp);
151        }
152        $this->image = 1;
153      }
154    
155      /**
156       * gif_add_footer().
157       */
158      function gif_add_footer() {
159        $this->gif .= ";";
160      }
161    
162      /**
163       * gif_block_compare().
164       */
165      function gif_block_compare($global_block, $local_block, $length) {
166        for ($i = 0; $i < $length; $i++) {
167          if ($global_block {3 * $i + 0} != $local_block {3 * $i + 0} ||
168          $global_block {3 * $i + 1} != $local_block {3 * $i + 1} ||
169          $global_block {3 * $i + 2} != $local_block {3 * $i + 2}) {
170            return FALSE;
171          }
172        }
173        return TRUE;
174      }
175    
176      /**
177       * gif_word().
178       */
179      function gif_word($int) {
180        return (chr($int & 0xFF) . chr(($int >> 8) & 0xFF));
181      }
182    
183      /**
184       * get_animation().
185       */
186      function get_animation() {
187        return ($this->gif);
188      }
189    
190    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2