SnapchatAgent::isMedia PHP Method

isMedia() public method

Checks to see if a blob looks like a media file.
public isMedia ( data $data ) : boolean
$data data The blob data (or just the header).
return boolean TRUE if the blob looks like a media file, FALSE otherwise.
    function isMedia($data)
    {
        // Check for a JPG header.
        if ($data[0] == chr(0xff) && $data[1] == chr(0xd8)) {
            return TRUE;
        }
        // Check for a MP4 header.
        if ($data[0] == chr(0x0) && $data[1] == chr(0x0)) {
            return TRUE;
        }
        return FALSE;
    }

Usage Example

コード例 #1
0
ファイル: snapchat.php プロジェクト: krew25/SC-API
 /**
  * Downloads a snap.
  *
  * @param string $id
  *   The snap ID.
  *
  * @return mixed
  *   The snap data or FALSE on failure.
  *     Snap data can returned as an Array of more than one file.
  * 	array(
  * 		overlay~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0    => overlay_file_data,
  *		media~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0	    => m4v_file_data
  * 	)
  */
 function getMedia($id, $from = null, $time = null, $subdir = null)
 {
     // Make sure we're logged in and have a valid access token.
     if (!$this->auth_token || !$this->username) {
         return FALSE;
     }
     if ($subdir == null) {
         $subdir = $this->username;
     }
     $path = __DIR__ . DIRECTORY_SEPARATOR . "snaps" . DIRECTORY_SEPARATOR . $subdir . DIRECTORY_SEPARATOR . $from;
     if (!file_exists($path)) {
         mkdir($path, 0777, true);
     }
     $file = $path . DIRECTORY_SEPARATOR . date("Y-m-d H-i-s", (int) ($time / 1000));
     $extensions = array(".jpg", ".png", ".mp4", "");
     foreach ($extensions as $ext) {
         if (file_exists($file . $ext)) {
             return false;
         }
     }
     $timestamp = parent::timestamp();
     $result = parent::post('/bq/blob', array('id' => $id, 'timestamp' => $timestamp, 'username' => $this->username), array($this->auth_token, $timestamp), $multipart = false, $debug = $this->debug);
     if (!parent::isMedia(substr($result, 0, 2))) {
         //When a snapchat video is sent with "text" or overlay
         //the overlay is a transparent PNG file Zipped together
         //with the M4V file.
         //First two bytes are "PK" x50x4B; thus the previous media check
         //will fail and would've returned a FALSE on an available media.
         if (parent::isCompressed(substr($result, 0, 2))) {
             //Uncompress
             $result = parent::unCompress($result);
             //Return Media and Overlay
         }
     }
     if ($from != null && $time != null) {
         if (is_array($result)) {
             foreach ($result as $key => $value) {
                 $this->writeToFile($file, $value);
             }
         } else {
             $this->writeToFile($file, $result);
         }
     }
     return $result;
 }
All Usage Examples Of SnapchatAgent::isMedia