Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezgifimageanalyzer.php
Go to the documentation of this file.
1 <?php
18 class eZGIFImageAnalyzer
19 {
23 function process( $mimeData, $parameters = array() )
24 {
25 $printInfo = false;
26 if ( isset( $parameters['print_info'] ) )
27 $printInfo = $parameters['print_info'];
28
29 $filename = $mimeData['url'];
30 $fd = fopen( $filename, 'rb' );
31 if ( $fd )
32 {
33 $blockTypes = array( 0x21 => 'Exension Introducer',
34 0x2c => 'Image Descriptor',
35 0x3b => 'Trailer' );
36 $extensionLabels = array( 0xf9 => 'Graphical Control Extension',
37 0xff => 'Application Extension',
38 0xfe => 'Comment Extension' );
39 // Read GIF header
40 $data = fread( $fd, 6 );
41 if ( $data == 'GIF87a' or
42 $data == 'GIF89a' )
43 {
44 $info = array();
45
46 $info['version'] = substr( $data, 3 );
47 $info['frame_count'] = 0;
48 $info['mode'] = eZImageAnalyzer::MODE_INDEXED;
49 $offset = 6;
50
51 $info['animation_timer'] = false;
52 $info['animation_timer_type'] = eZImageAnalyzer::TIMER_HUNDRETHS_OF_A_SECOND;
53
54 $info['comment_list'] = array();
55
56 $info['transparency_type'] = eZImageAnalyzer::TRANSPARENCY_OPAQUE;
57
58 // Read Logical Screen Descriptor
59 $data = fread( $fd, 7 );
60 $offset += 7;
61
62 $lsdFields = ord( $data[4] );
63
64 $globalColorCount = 0;
65 $globalColorTableSize = 0;
66 if ( $lsdFields >> 7 )
67 {
68 $globalColorCount = ( 1 << ( ( $lsdFields & 0x07 ) + 1) );
69 $globalColorTableSize = $globalColorCount * 3;
70 }
71
72 $info['color_count'] = $globalColorCount;
73
74 $info['width'] = ( ord( $data[1] ) << 8 ) + ord( $data[0] );
75 $info['height'] = ( ord( $data[3] ) << 8 ) + ord( $data[2] );
76
77
78 if ( $globalColorTableSize )
79 {
80 fseek( $fd, $globalColorTableSize, SEEK_CUR );
81 $offset += $globalColorTableSize;
82 }
83 if ( $printInfo )
84 print( "Global color table size=$globalColorTableSize\n" );
85
86 $done = false;
87 while ( !$done )
88 {
89 $data = fread( $fd, 1 );
90 $offset += 1;
91 $blockType = ord( $data[0] );
92 if ( $printInfo )
93 {
94 print( "Block type=0x" . dechex( $blockType ) );
95 if ( isset( $blockTypes[$blockType] ) )
96 print( " (" . $blockTypes[$blockType] . ")" );
97 print( "\n" );
98 }
99 if ( $blockType == 0x21 ) // Extension Introducer
100 {
101 $data .= fread( $fd, 1 );
102 $offset += 1;
103 $extensionLabel = ord( $data[1] );
104 if ( $printInfo )
105 {
106 print( "> " . "Extension label=0x" . dechex( $extensionLabel ) );
107 if ( isset( $extensionLabels[$extensionLabel] ) )
108 print( " (" . $extensionLabels[$extensionLabel] . ")" );
109 print( "\n" );
110 }
111 if ( $extensionLabel == 0xf9 ) // Graphical Control Extension
112 {
113 $data = fread( $fd, 5 + 1 );
114 $info['animation_timer'] = ( ord( $data[3] ) << 8 ) + ord( $data[2] );
115 $gceFlags = ord( $data[1] );
116 if ( $gceFlags & 0x01 )
117 $info['transparency_type'] = eZImageAnalyzer::TRANSPARENCY_TRANSPARENT;
118 $offset += 5 + 1;
119 }
120 else if ( $extensionLabel == 0xff ) // Application Extension
121 {
122 $data = fread( $fd, 12 );
123 if ( $printInfo )
124 {
125 $applicationIdentifier = substr( $data, 1, 8 );
126 $applicationAuthentication = substr( $data, 1 + 8, 3 );
127 print( ">> Application identifier: $applicationIdentifier\n" );
128 print( ">> Application authentication: $applicationAuthentication\n" );
129 }
130 $offset += 12;
131
132 $dataBlockDone = false;
133 while ( !$dataBlockDone )
134 {
135 $data = fread( $fd, 1 );
136 $offset += 1;
137 $blockBytes = ord( $data[0] );
138 if ( $printInfo )
139 print( ">> Application Block size=$blockBytes\n" );
140 if ( $blockBytes )
141 {
142 fseek( $fd, $blockBytes, SEEK_CUR );
143 $offset += $blockBytes;
144 }
145 else
146 {
147 if ( $printInfo )
148 print( ">> GIF application blocks terminated by 0 image block\n" );
149 $dataBlockDone = true;
150 }
151 }
152 }
153 else if ( $extensionLabel == 0xfe ) // Comment Extension
154 {
155 $commentBlockDone = false;
156 $comment = false;
157 while ( !$commentBlockDone )
158 {
159 $data = fread( $fd, 1 );
160 $offset += 1;
161 $blockBytes = ord( $data[0] );
162 if ( $printInfo )
163 print( ">> Comment Block size=$blockBytes\n" );
164 if ( $blockBytes )
165 {
166 $data = fread( $fd, $blockBytes );
167 if ( $printInfo )
168 print( $data );
169 $comment .= $data;
170 $offset += $blockBytes;
171 }
172 else
173 {
174 if ( $printInfo )
175 print( ">> GIF comment blocks terminated by 0 image block\n" );
176 $commentBlockDone = true;
177 }
178 }
179 if ( $comment )
180 $info['comment_list'][] = $comment;
181 }
182 else
183 {
184 if ( $printInfo )
185 print( "> Unknown extension label, aborting\n" );
186 $done = true;
187 }
188 }
189 else if ( $blockType == 0x2c ) // Image Descriptor
190 {
191 $info['frame_count'] += 1;
192 $data .= fread( $fd, 9 );
193 $localColorTableSize = 0;
194 $localColorCount = 0;
195 $idFields = ord( $data[9] );
196 if ( $idFields >> 7 ) // Local Color Table
197 {
198 $localColorCount = ( 1 << ( ( $idFields & 0x07 ) + 1) );
199 $localColorTableSize = $localColorCount * 3;
200 }
201 if ( $localColorCount > $globalColorCount )
202 $info['color_count'] = $localColorCount;
203
204 if ( $localColorTableSize )
205 {
206 fseek( $fd, $localColorTableSize, SEEK_CUR );
207 $offset += $localColorTableSize;
208 }
209 if ( $printInfo )
210 print( ">> Local color table size=$localColorTableSize\n" );
211
212 $lzwCodeSize = fread( $fd, 1 ); // LZW Minimum Code Size
213 $offset += 1;
214
215 $dataBlockDone = false;
216 while ( !$dataBlockDone )
217 {
218 $data = fread( $fd, 1 );
219 $offset += 1;
220 $blockBytes = ord( $data[0] );
221 if ( $printInfo )
222 print( ">> Block size=$blockBytes\n" );
223 if ( $blockBytes )
224 {
225 fseek( $fd, $blockBytes, SEEK_CUR );
226 $offset += $blockBytes;
227 }
228 else
229 {
230 if ( $printInfo )
231 print( ">> GIF image blocks terminated by 0 image block\n" );
232 $dataBlockDone = true;
233 }
234 }
235 }
236 else if ( $blockType == 0x3b ) // Trailer, end of stream
237 {
238 if ( $printInfo )
239 print( "GIF stream terminated by Trailer block\n" );
240 $done = true;
241 }
242 if ( feof( $fd ) )
243 $done = true;
244 }
245 }
246 else
247 {
248 if ( $printInfo )
249 print( "Not GIF\n" );
250 eZDebug::writeError( "The image file $filename is not a GIF file, cannot analyze it", __METHOD__ );
251 }
252 fclose( $fd );
253 $info['is_animated'] = $info['frame_count'] > 1;
254 return $info;
255 }
256 return false;
257 }
258
259 }
260
261 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZGIFImageAnalyzer
The class eZGIFImageAnalyzer does.
Definition: ezgifimageanalyzer.php:19
eZGIFImageAnalyzer\process
process( $mimeData, $parameters=array())
Definition: ezgifimageanalyzer.php:23
eZImageAnalyzer\TRANSPARENCY_OPAQUE
const TRANSPARENCY_OPAQUE
Definition: ezimageanalyzer.php:29
eZImageAnalyzer\MODE_INDEXED
const MODE_INDEXED
Definition: ezimageanalyzer.php:24
eZImageAnalyzer\TRANSPARENCY_TRANSPARENT
const TRANSPARENCY_TRANSPARENT
Definition: ezimageanalyzer.php:30
eZImageAnalyzer\TIMER_HUNDRETHS_OF_A_SECOND
const TIMER_HUNDRETHS_OF_A_SECOND
Definition: ezimageanalyzer.php:27
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.