Faster_Image_B52f1a8_Image_Parser::parseType PHP Метод

parseType() публичный Метод

Reads and returns the type of the image
public parseType ( ) : boolean | string
Результат boolean | string
    public function parseType()
    {
        if (!$this->type) {
            $this->stream->resetPointer();
            switch ($this->stream->read(2)) {
                case "BM":
                    return $this->type = 'bmp';
                case "GI":
                    return $this->type = 'gif';
                case chr(0xff) . chr(0xd8):
                    return $this->type = 'jpeg';
                case "":
                    switch ($this->readByte($this->stream->peek(1))) {
                        case 1:
                            return $this->type = 'ico';
                        case 2:
                            return $this->type = 'cur';
                    }
                    return false;
                case chr(0x89) . 'P':
                    return $this->type = 'png';
                case "RI":
                    if (substr($this->stream->read(10), 6, 4) == 'WEBP') {
                        return $this->type = 'webp';
                    }
                    return false;
                case '8B':
                    return $this->type = 'psd';
                case "II":
                case "MM":
                    return $this->type = 'tiff';
                default:
                    return false;
            }
        }
        return $this->type;
    }

Usage Example

 /**
  * Create the handle for the curl request
  *
  * @param $url
  * @param $result
  *
  * @return resource
  */
 protected function handle($url, &$result)
 {
     $stream = new Stream_17b32f3_Stream();
     $parser = new Faster_Image_B52f1a8_Image_Parser($stream);
     $result['rounds'] = 0;
     $result['bytes'] = 0;
     $result['size'] = 'failed';
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
     curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Cache-Control: max-age=0", "Connection: keep-alive", "Keep-Alive: 300", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Accept-Language: en-us,en;q=0.5", "Pragma: "));
     curl_setopt($ch, CURLOPT_ENCODING, "");
     curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $str) use(&$result, &$parser, &$stream, $url) {
         $result['rounds']++;
         $result['bytes'] += strlen($str);
         $stream->write($str);
         try {
             // store the type in the result array by looking at the bits
             $result['type'] = $parser->parseType();
             /*
              * We try here to parse the buffer of characters we already have
              * for the size.
              */
             $result['size'] = $parser->parseSize() ?: 'failed';
         } catch (Stream_17b32f3_Stream_Buffer_Too_Small_Exception $e) {
             /*
              * If this exception is thrown, we don't have enough of the stream buffered
              * so in order to tell curl to keep streaming we need to return the number
              * of bytes we have already handled
              *
              * We set the 'size' to 'failed' in the case that we've done
              * the entire image and we couldn't figure it out. Otherwise
              * it'll get overwritten with the next round.
              */
             $result['size'] = 'failed';
             return strlen($str);
         } catch (Faster_Image_B52f1a8_Invalid_Image_Exception $e) {
             /*
              * This means we've determined that we're lost and don't know
              * how to parse this image.
              *
              * We set the size to invalid and move on
              */
             $result['size'] = 'invalid';
             return -1;
         }
         /*
          * We return -1 to abort the transfer when we have enough buffered
          * to find the size
          */
         //
         // hey curl! this is an error. But really we just are stopping cause
         // we already have what we wwant
         return -1;
     });
     return $ch;
 }