source: extensions/charlies_content/getid3/getid3/module.lib.image_size.php @ 3318

Last change on this file since 3318 was 3318, checked in by vdigital, 15 years ago

+ Add Charlies' content to depository

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP version 5                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 2 of the GPL license,         |
8// | that is bundled with this package in the file license.txt and is     |
9// | available through the world-wide-web at the following url:           |
10// | http://www.gnu.org/copyleft/gpl.html                                 |
11// +----------------------------------------------------------------------+
12// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13// +----------------------------------------------------------------------+
14// | Authors: James Heinrich <infoØgetid3*org>                            |
15// |          Allan Hansen <ahØartemis*dk>                                |
16// +----------------------------------------------------------------------+
17// | module.lib.data-hash.php                                             |
18// | getID3() library file.                                               |
19// | dependencies: NONE.                                                  |
20// +----------------------------------------------------------------------+
21//
22// $Id: module.lib.image_size.php 3318 2009-05-20 21:54:10Z vdigital $
23
24
25
26class getid3_lib_image_size
27{
28   
29    const GIF_SIG   = "\x47\x49\x46";                           // 'GIF'
30    const PNG_SIG   = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
31    const JPG_SIG   = "\xFF\xD8\xFF";
32    const JPG_SOS   = "\xDA";                                   // Start Of Scan - image data start
33    const JPG_SOF0  = "\xC0";                                   // Start Of Frame N
34    const JPG_SOF1  = "\xC1";                                   // N indicates which compression process
35    const JPG_SOF2  = "\xC2";                                   // Only SOF0-SOF2 are now in common use
36    const JPG_SOF3  = "\xC3";                                   // NB: codes C4 and CC are *not* SOF markers
37    const JPG_SOF5  = "\xC5";                                   
38    const JPG_SOF6  = "\xC6";                                   
39    const JPG_SOF7  = "\xC7";                                   
40    const JPG_SOF9  = "\xC9";                                   
41    const JPG_SOF10 = "\xCA";                                   
42    const JPG_SOF11 = "\xCB";                                   // NB: codes C4 and CC are *not* SOF markers
43    const JPG_SOF13 = "\xCD";                                   
44    const JPG_SOF14 = "\xCE";                                   
45    const JPG_SOF15 = "\xCF";                                   
46    const JPG_EOI   = "\xD9";                                   // End Of Image (end of datastream)
47
48
49    static public function get($img_data) {
50
51        $height = $width  = $type   = '';
52       
53        if ((substr($img_data, 0, 3) == getid3_lib_image_size::GIF_SIG) && (strlen($img_data) > 10)) {
54           
55            $dim = unpack('v2dim', substr($img_data, 6, 4));
56            $width  = $dim['dim1'];
57            $height = $dim['dim2'];
58            $type = 1;
59           
60        } elseif ((substr($img_data, 0, 8) == getid3_lib_image_size::PNG_SIG) && (strlen($img_data) > 24)) {
61           
62            $dim = unpack('N2dim', substr($img_data, 16, 8));
63            $width  = $dim['dim1'];
64            $height = $dim['dim2'];
65            $type = 3;
66           
67        } elseif ((substr($img_data, 0, 3) == getid3_lib_image_size::JPG_SIG) && (strlen($img_data) > 4)) {
68           
69            ///////////////// JPG CHUNK SCAN ////////////////////
70            $img_pos = $type = 2;
71            $buffer = strlen($img_data) - 2;
72            while ($img_pos < strlen($img_data)) {
73           
74                // synchronize to the marker 0xFF
75                $img_pos = strpos($img_data, 0xFF, $img_pos) + 1;
76                $marker = $img_data[$img_pos];
77                do {
78                    $marker = ord($img_data[$img_pos++]);
79                } while ($marker == 255);
80           
81                // find dimensions of block
82                switch (chr($marker)) {
83           
84                    // Grab width/height from SOF segment (these are acceptable chunk types)
85                    case getid3_lib_image_size::JPG_SOF0:
86                    case getid3_lib_image_size::JPG_SOF1:
87                    case getid3_lib_image_size::JPG_SOF2:
88                    case getid3_lib_image_size::JPG_SOF3:
89                    case getid3_lib_image_size::JPG_SOF5:
90                    case getid3_lib_image_size::JPG_SOF6:
91                    case getid3_lib_image_size::JPG_SOF7:
92                    case getid3_lib_image_size::JPG_SOF9:
93                    case getid3_lib_image_size::JPG_SOF10:
94                    case getid3_lib_image_size::JPG_SOF11:
95                    case getid3_lib_image_size::JPG_SOF13:
96                    case getid3_lib_image_size::JPG_SOF14:
97                    case getid3_lib_image_size::JPG_SOF15:
98                        $dim = unpack('n2dim', substr($img_data, $img_pos + 3, 4));
99                        $height = $dim['dim1'];
100                        $width  = $dim['dim2'];
101                        break 2; // found it so exit
102           
103                    case getid3_lib_image_size::JPG_EOI:
104                    case getid3_lib_image_size::JPG_SOS:
105                        return false;     
106           
107                    default:   // We're not interested in other markers
108                        $skiplen = (ord($img_data[$img_pos++]) << 8) + ord($img_data[$img_pos++]) - 2;
109                        // if the skip is more than what we've read in, read more
110                        $buffer -= $skiplen;
111                        if ($buffer < 512) { // if the buffer of data is too low, read more file.
112                            return false; 
113                        }
114                        $img_pos += $skiplen;
115                        break;
116                } 
117            } 
118        } 
119
120        return array ($width, $height, $type);
121    } // end function
122
123
124}
125
126?>
Note: See TracBrowser for help on using the repository browser.