Esempio n. 1
0
 public function to()
 {
     if (!is_null($this->to)) {
         return $this->to;
     }
     $source = $this->source();
     $name = f::name($source['name']);
     $extension = f::extension($source['name']);
     $safeName = f::safeName($name);
     $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension));
     if (empty($safeExtension)) {
         $safeExtension = f::mimeToExtension(f::mime($source['tmp_name']));
     }
     return $this->to = str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . r(!empty($safeExtension), '.' . $safeExtension), 'extension' => $extension, 'safeExtension' => $safeExtension));
 }
Esempio n. 2
0
 /**
  * Returns the mime type of a file
  *
  * @param string $file
  * @return mixed
  */
 public static function mime($file)
 {
     // stop for invalid files
     if (!file_exists($file)) {
         return null;
     }
     // Fileinfo is prefered if available
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $mime = finfo_file($finfo, $file);
         finfo_close($finfo);
         return $mime;
     }
     // for older versions with mime_content_type go for that.
     if (function_exists('mime_content_type') && ($mime = @mime_content_type($file) !== false)) {
         return $mime;
     }
     // shell check
     try {
         $mime = system::execute('file', [$file, '-z', '-b', '--mime'], 'output');
         $mime = trim(str::split($mime, ';')[0]);
         if (f::mimeToExtension($mime)) {
             return $mime;
         }
     } catch (Exception $e) {
         // no mime type detectable with shell
         $mime = null;
     }
     // Mime Sniffing
     $reader = new MimeReader($file);
     $mime = $reader->get_type();
     if (!empty($mime) && f::mimeToExtension($mime)) {
         return $mime;
     }
     // guess the matching mime type by extension
     return f::extensionToMime(f::extension($file));
 }