Faster_Image_B52f1a8_Image_Parser::parseSize PHP Метод

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

public parseSize ( ) : array | boolean | null
Результат array | boolean | null
    public function parseSize()
    {
        $this->stream->resetPointer();
        switch ($this->type) {
            case 'png':
                return $this->parseSizeForPNG();
            case 'ico':
            case 'cur':
                return $this->parseSizeForIco();
            case 'gif':
                return $this->parseSizeForGIF();
            case 'bmp':
                return $this->parseSizeForBMP();
            case 'jpeg':
                return $this->parseSizeForJPEG();
            case 'tiff':
                return $this->parseSizeForTiff();
            case 'psd':
                return $this->parseSizeForPSD();
            case 'webp':
                return $this->parseSizeForWebp();
        }
        return null;
    }

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;
 }