Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

TypeJpeg.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 4.15 KiB


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  class TypeJpeg extends TypeBase
015  {
016      /** @var int JPEG max header size. Headers can be bigger, but we'll abort
017       *            going through the header after this */
018      const JPEG_MAX_HEADER_SIZE = 786432; // = 768 kiB
019   
020      /** @var string JPEG header */
021      const JPEG_HEADER = "\xFF\xD8";
022   
023      /** @var string Start of frame marker */
024      const SOF_START_MARKER = "\xFF";
025   
026      /** @var string End of image (EOI) marker */
027      const JPEG_EOI_MARKER = "\xD9";
028   
029      /** @var array JPEG SOF markers */
030      protected $sofMarkers = array(
031          "\xC0",
032          "\xC1",
033          "\xC2",
034          "\xC3",
035          "\xC5",
036          "\xC6",
037          "\xC7",
038          "\xC9",
039          "\xCA",
040          "\xCB",
041          "\xCD",
042          "\xCE",
043          "\xCF"
044      );
045   
046      /** @var string|bool JPEG data stream */
047      protected $data = '';
048   
049      /** @var int Data length */
050      protected $dataLength = 0;
051   
052      /**
053       * {@inheritdoc}
054       */
055      public function getSize($filename)
056      {
057          // Do not force the data length
058          $this->data = $this->fastImageSize->getImage($filename, 0, self::JPEG_MAX_HEADER_SIZE, false);
059   
060          // Check if file is jpeg
061          if ($this->data === false || substr($this->data, 0, self::SHORT_SIZE) !== self::JPEG_HEADER)
062          {
063              return;
064          }
065   
066          // Look through file for SOF marker
067          $size = $this->getSizeInfo();
068   
069          $this->fastImageSize->setSize($size);
070          $this->fastImageSize->setImageType(IMAGETYPE_JPEG);
071      }
072   
073      /**
074       * Get size info from image data
075       *
076       * @return array An array with the image's size info or an empty array if
077       *        size info couldn't be found
078       */
079      protected function getSizeInfo()
080      {
081          $size = array();
082          // since we check $i + 1 we need to stop one step earlier
083          $this->dataLength = strlen($this->data) - 1;
084   
085          $sofStartRead = true;
086   
087          // Look through file for SOF marker
088          for ($i = 2; $i < $this->dataLength; $i++)
089          {
090              $marker = $this->getNextMarker($i, $sofStartRead);
091   
092              if (in_array($marker, $this->sofMarkers))
093              {
094                  // Extract size info from SOF marker
095                  return $this->extractSizeInfo($i);
096              }
097              else
098              {
099                  // Extract length only
100                  $markerLength = $this->extractMarkerLength($i);
101   
102                  if ($markerLength < 2)
103                  {
104                      return $size;
105                  }
106   
107                  $i += $markerLength - 1;
108                  continue;
109              }
110          }
111   
112          return $size;
113      }
114   
115      /**
116       * Extract marker length from data
117       *
118       * @param int $i Current index
119       * @return int Length of current marker
120       */
121      protected function extractMarkerLength($i)
122      {
123          // Extract length only
124          list(, $unpacked) = unpack("H*", substr($this->data, $i, self::LONG_SIZE));
125   
126          // Get width and height from unpacked size info
127          $markerLength = hexdec(substr($unpacked, 0, 4));
128   
129          return $markerLength;
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($i)
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          // Get width and height from unpacked size info
144          $size = array(
145              'width'        => hexdec(substr($unpacked, 4, 4)),
146              'height'    => hexdec(substr($unpacked, 0, 4)),
147          );
148   
149          return $size;
150      }
151   
152      /**
153       * Get next JPEG marker in file
154       *
155       * @param int $i Current index
156       * @param bool $sofStartRead Flag whether SOF start padding was already read
157       *
158       * @return string Next JPEG marker in file
159       */
160      protected function getNextMarker(&$i, &$sofStartRead)
161      {
162          $this->skipStartPadding($i, $sofStartRead);
163   
164          do {
165              if ($i >= $this->dataLength)
166              {
167                  return self::JPEG_EOI_MARKER;
168              }
169              $marker = $this->data[$i];
170              $i++;
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(&$i, &$sofStartRead)
184      {
185          if (!$sofStartRead)
186          {
187              while ($this->data[$i] !== self::SOF_START_MARKER)
188              {
189                  $i++;
190              }
191          }
192      }
193  }
194