Verzeichnisstruktur phpBB-3.3.16
- Veröffentlicht
- 27.04.2026
So funktioniert es
|
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
TypeJpeg.php
001 <?php
002
003 /**
004 * fast-image-size image type jpeg
005 * @package fast-image-size
006 * @copyright (c) Marc Alexander <admin@m-a-styles.de>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace FastImageSize\Type;
013
014 use FastImageSize\ImageReader;
015
016 class TypeJpeg extends TypeBase
017 {
018 /** @var int JPEG max header size. Headers can be bigger, but we'll abort
019 * going through the header after this */
020 const JPEG_MAX_HEADER_SIZE = 786432; // = 768 kiB
021
022 /** @var string JPEG header */
023 const JPEG_HEADER = "\xFF\xD8";
024
025 /** @var string Start of frame marker */
026 const SOF_START_MARKER = "\xFF";
027
028 /** @var string End of image (EOI) marker */
029 const JPEG_EOI_MARKER = "\xD9";
030
031 /** @var array JPEG SOF markers */
032 protected $sofMarkers = array(
033 "\xC0",
034 "\xC1",
035 "\xC2",
036 "\xC3",
037 "\xC5",
038 "\xC6",
039 "\xC7",
040 "\xC9",
041 "\xCA",
042 "\xCB",
043 "\xCD",
044 "\xCE",
045 "\xCF"
046 );
047
048 /** @var string|bool JPEG data stream */
049 protected $data = '';
050
051 /** @var int Data length */
052 protected $dataLength = 0;
053
054 /**
055 * {@inheritdoc}
056 */
057 public function getSize(string $filename, ImageReader $imageReader): ?array
058 {
059 // Do not force the data length
060 $this->data = $imageReader->getImage($filename, 0, self::JPEG_MAX_HEADER_SIZE, false);
061
062 // Check if file is jpeg
063 if ($this->data === false || substr($this->data, 0, self::SHORT_SIZE) !== self::JPEG_HEADER)
064 {
065 return null;
066 }
067
068 // Look through file for SOF marker
069 $size = $this->getSizeInfo();
070 $size['type'] = IMAGETYPE_JPEG;
071
072 return $size;
073 }
074
075 /**
076 * Get size info from image data
077 *
078 * @return array An array with the image's size info or an empty array if
079 * size info couldn't be found
080 */
081 protected function getSizeInfo(): array
082 {
083 $size = array();
084 // since we check $i + 1 we need to stop one step earlier
085 $this->dataLength = strlen($this->data) - 1;
086
087 $sofStartRead = true;
088
089 // Look through file for SOF marker
090 for ($i = 2; $i < $this->dataLength; $i++)
091 {
092 $marker = $this->getNextMarker($i, $sofStartRead);
093
094 if (in_array($marker, $this->sofMarkers))
095 {
096 // Extract size info from SOF marker
097 return $this->extractSizeInfo($i);
098 }
099 else
100 {
101 // Extract length only
102 $markerLength = $this->extractMarkerLength($i);
103
104 if ($markerLength < 2)
105 {
106 return $size;
107 }
108
109 $i += $markerLength - 1;
110 continue;
111 }
112 }
113
114 return $size;
115 }
116
117 /**
118 * Extract marker length from data
119 *
120 * @param int $i Current index
121 * @return int Length of current marker
122 */
123 protected function extractMarkerLength(int $i): int
124 {
125 // Extract length only
126 list(, $unpacked) = unpack("H*", substr($this->data, $i, self::LONG_SIZE));
127
128 // Return width and height from unpacked size info
129 return hexdec(substr($unpacked, 0, 4));
130 }
131
132 /**
133 * Extract size info from data
134 *
135 * @param int $i Current index
136 * @return array Size info of current marker
137 */
138 protected function extractSizeInfo(int $i): array
139 {
140 // Extract size info from SOF marker
141 list(, $unpacked) = unpack("H*", substr($this->data, $i - 1 + self::LONG_SIZE, self::LONG_SIZE));
142
143 // Return width and height from unpacked size info
144 return [
145 'width' => hexdec(substr($unpacked, 4, 4)),
146 'height' => hexdec(substr($unpacked, 0, 4)),
147 ];
148 }
149
150 /**
151 * Get next JPEG marker in file
152 *
153 * @param int $i Current index
154 * @param bool $sofStartRead Flag whether SOF start padding was already read
155 *
156 * @return string Next JPEG marker in file
157 */
158 protected function getNextMarker(int &$i, bool &$sofStartRead): string
159 {
160 $this->skipStartPadding($i, $sofStartRead);
161
162 do
163 {
164 if ($i >= $this->dataLength)
165 {
166 return self::JPEG_EOI_MARKER;
167 }
168 $marker = $this->data[$i];
169 $i++;
170 }
171 while ($marker == self::SOF_START_MARKER);
172
173 return $marker;
174 }
175
176 /**
177 * Skip over any possible padding until we reach a byte without SOF start
178 * marker. Extraneous bytes might need to require proper treating.
179 *
180 * @param int $i Current index
181 * @param bool $sofStartRead Flag whether SOF start padding was already read
182 */
183 protected function skipStartPadding(int &$i, bool &$sofStartRead): void
184 {
185 if (!$sofStartRead)
186 {
187 while ($i < $this->dataLength && $this->data[$i] !== self::SOF_START_MARKER)
188 {
189 $i++;
190 }
191 }
192 }
193 }
194