Esempio n. 1
0
 public function convert($mimeType)
 {
     if (Mime_Type::guessName($mimeType) != 'audio') {
         return true;
         // others care about inter media type conversions
     }
     $sourceType = Mime_Type::guessExtension($this->_object);
     $targetType = Mime_Type::guessExtension($mimeType);
     if ($this->_compress) {
         // do stuff...
     }
     $command = "{$this->_command} -t {$sourceType} - -t {$targetType} -";
     rewind($this->_object);
     $temporary = fopen('php://temp', 'w');
     $descr = array(0 => $this->_object, 1 => $temporary, 2 => array('pipe', 'a'));
     $process = proc_open($command, $descr, $pipes);
     fclose($pipes[2]);
     $return = proc_close($process);
     if ($return != 0) {
         //var_dump(stream_get_contents($temporary, -1, 0));
         // throw new RuntimeException("Command `{$command}` returned `{$return}`.");
         return false;
     }
     $this->_object = $temporary;
     return true;
 }
Esempio n. 2
0
 public function testGuessExtensionMimeType()
 {
     $this->assertEquals('jpg', Mime_Type::guessExtension('image/jpeg'));
     $this->assertEquals('xhtml', Mime_Type::guessExtension('application/xhtml+xml'));
     $this->assertEquals('bin', Mime_Type::guessExtension('application/octet-stream'));
     $this->assertEquals('wav', Mime_Type::guessExtension('audio/x-wav'));
 }
Esempio n. 3
0
 public function convert($mimeType)
 {
     $sourceType = Mime_Type::guessExtension($this->_object);
     $targetType = Mime_Type::guessExtension($mimeType);
     $map = array('ogv' => 'ogg');
     if (isset($map[$sourceType])) {
         $sourceType = $map[$sourceType];
     }
     if (isset($map[$targetType])) {
         $targetType = $map[$targetType];
     }
     $command = "{$this->_command} -f {$sourceType} -i - ";
     switch (Mime_Type::guessName($mimeType)) {
         case 'image':
             $command .= "-vcodec {$targetType} -vframes 1 -an -f rawvideo -";
             break;
         case 'video':
             $command .= "-f {$targetType} -";
             break;
         default:
             return true;
     }
     rewind($this->_object);
     $temporary = fopen('php://temp', 'w+b');
     $descr = array(0 => $this->_object, 1 => $temporary, 2 => array('pipe', 'a'));
     $process = proc_open($command, $descr, $pipes);
     fclose($pipes[2]);
     $return = proc_close($process);
     if ($return != 0) {
         // throw new RuntimeException("Command `{$command}` returned `{$return}`.");
         return false;
     }
     $this->_object = $temporary;
     return true;
 }
Esempio n. 4
0
 public function convert($mimeType)
 {
     if (Mime_Type::guessName($mimeType) != 'audio') {
         return true;
         // others care about inter media type conversions
     }
     $sourceType = Mime_Type::guessExtension($this->_object);
     $targetType = Mime_Type::guessExtension($mimeType);
     $map = array('ogv' => 'ogg', 'oga' => 'ogg');
     if (isset($map[$sourceType])) {
         $sourceType = $map[$sourceType];
     }
     if (isset($map[$targetType])) {
         $targetType = $map[$targetType];
     }
     $modify = null;
     if ($this->_sampleRate) {
         $modify .= " --rate {$this->_sampleRate}";
     }
     if ($this->_channels) {
         $modify .= " --channels {$this->_channels}";
     }
     rewind($this->_object);
     $error = fopen('php://temp', 'wrb+');
     $targetFile = tempnam(sys_get_temp_dir(), 'mm_');
     // Since SoX 14.3.0 multi threading is enabled which
     // paradoxically can cause huge slowdowns.
     $command = "{$this->_command} -q --single-threaded";
     $command .= " -t {$sourceType} -{$modify} -t {$targetType} {$targetFile}";
     $descr = array(0 => $this->_object, 1 => array('pipe', 'a'), 2 => array('pipe', 'a'));
     $process = proc_open($command, $descr, $pipes);
     fclose($pipes[1]);
     fclose($pipes[2]);
     $return = proc_close($process);
     // Workaround for header based formats which require the output stream to be seekable.
     $target = fopen($targetFile, 'rb');
     $temporary = fopen('php://temp', 'wb+');
     stream_copy_to_stream($target, $temporary);
     fclose($target);
     unlink($targetFile);
     if ($return != 0) {
         rewind($error);
         //var_dump(stream_get_contents($temporary, -1, 0));
         // throw new RuntimeException("Command `{$command}` returned `{$return}`.");
         return false;
     }
     fclose($error);
     $this->_object = $temporary;
     return true;
 }
Esempio n. 5
0
 /**
  * Returns the relative path to the destination file
  *
  * @param array $via Information about the temporary file
  * @param array $from Information about the source file
  * @return string The path to the destination file or false
  */
 public function transferTo($via, $from)
 {
     extract($from);
     $irregular = array('image' => 'img', 'text' => 'txt');
     $name = Mime_Type::guessName($mimeType ? $mimeType : $file);
     if (empty($extension)) {
         $extension = Mime_Type::guessExtension($mimeType ? $mimeType : $file);
     }
     if (isset($irregular[$name])) {
         $short = $irregular[$name];
     } else {
         $short = substr($name, 0, 3);
     }
     $path = $short . DS;
     $path .= strtolower(Inflector::underscore($this->model)) . DS;
     $path .= String::uuid();
     $path .= !empty($extension) ? '.' . strtolower($extension) : null;
     return $path;
 }
Esempio n. 6
0
 /**
  * Generate a version of a file. If this method is reimplemented in the
  * model, than that one is used by `make()` instead of the implementation
  * below.
  *
  * $process an array with the following contents:
  *  - `directory`:  The destination directory (If this method was called
  *                  by `make()` the directory is already created)
  *  - `version`:  The version requested to be processed (e.g. `'l'`)
  *  - `instructions`: An array specifying processing steps to execute on $file
  *                    in order to get to the desired transformed file.
  *
  *                    Each instruction is either a key/value pair where the key
  *                    can be thought of the method and the value the arguments
  *                    passed to that method. Whenever a value appears without a
  *                    corresponding string key it is used as the method instead.
  *
  *                    `array('name of method', 'name of other method')`
  *                    `array('name of method' => array('arg1', 'arg2'))`
  *
  *                    Most methods are made available through the `Media_Process_*`
  *                    classes. The class is chosen depending on the type of media
  *                    being processed. Since each one of those classes exposes
  *                    different methods the availaibility of those depends on the
  *                    type of media being processed.
  *
  *                    Please see the documentation for the mm library for further
  *                    information on the `Media_Process_*` classes mentioned above.
  *
  *                    However some methods are builtin and made available directly
  *                    through this method here. One of them being the `clone` method.
  *                    Cloning allows instructions which don't actually modify a file
  *                    but represent just a copy of it. Available clone types are `copy`,
  *                    `link` and `symlink`.
  *
  *                    `array('clone' => <type>)`
  *
  *                    In case an instruction method is neither builtin nor available
  *                    through one of the `Media_Proces_*` classes, the `passthru()`
  *                    method is invoked on that media object. The concrete implementation
  *                    of `passthru()` and therefore how it deals with the data passed
  *                    to it *highly* depends on the adapter in use.
  *
  * @link https://github.com/davidpersson/mm The PHP media library.
  * @param Model $Model
  * @param string $file Absolute path to the source file
  * @param array $process directory, version, instructions
  * @return boolean `true` if version for the file was successfully stored
  */
 function makeVersion(&$Model, $file, $process)
 {
     extract($this->settings[$Model->alias]);
     /* Process builtin instructions */
     if (isset($process['instructions']['clone'])) {
         $action = $process['instructions']['clone'];
         if (!in_array($action, array('copy', 'link', 'symlink'))) {
             return false;
         }
         $destination = $this->_destinationFile($file, $process['directory'], null, $overwrite);
         if (!$destination) {
             return false;
         }
         if (!call_user_func($action, $file, $destination)) {
             return false;
         }
         return $action == 'copy' ? chmod($destination, $mode) : true;
     }
     /* Process `Media_Process_*` instructions */
     $Media = Media_Process::factory(array('source' => $file));
     foreach ($process['instructions'] as $method => $args) {
         if (is_int($method)) {
             $method = $args;
             $args = null;
         }
         if (method_exists($Media, $method)) {
             $result = call_user_func_array(array($Media, $method), (array) $args);
         } else {
             $result = $Media->passthru($method, $args);
         }
         if ($result === false) {
             return false;
         } elseif (is_a($result, 'Media_Process_Generic')) {
             $Media = $result;
         }
     }
     /* Determine destination file */
     $extension = null;
     if ($guessExtension) {
         if (isset($process['instructions']['convert'])) {
             $extension = Mime_Type::guessExtension($process['instructions']['convert']);
         } else {
             $extension = Mime_Type::guessExtension($file);
         }
     }
     $destination = $this->_destinationFile($file, $process['directory'], $extension, $overwrite);
     if (!$destination) {
         return false;
     }
     return $Media->store($destination) && chmod($destination, $mode);
 }
Esempio n. 7
0
 protected function _type($object)
 {
     $type = Mime_Type::guessExtension($object);
     $map = array('ogv' => 'ogg', 'oga' => 'ogg');
     return isset($map[$type]) ? $map[$type] : $type;
 }
Esempio n. 8
0
 public function testGuessExtensionResource()
 {
     $handleA = fopen("{$this->_files}/application_pdf.pdf", 'rb');
     $handleB = fopen('php://temp', 'rb+');
     stream_copy_to_stream($handleA, $handleB);
     $this->assertEquals('pdf', Mime_Type::guessExtension($handleA));
     $this->assertEquals('pdf', Mime_Type::guessExtension($handleB));
     fclose($handleA);
     fclose($handleB);
 }
Esempio n. 9
0
 /**
  * Generate a version of a file. If this method is reimplemented in the
  * model, than that one is used by `make()` instead of the implementation
  * below.
  *
  * $process an array with the following contents:
  *  - `directory`:  The destination directory (If this method was called
  *                  by `make()` the directory is already created)
  *  - `version`:  The version requested to be processed (e.g. `'l'`)
  *  - `instructions`: An array containing which names of methods to be called.
  *                 Possible instructions are:
  *                  - `array('name of method', 'name of other method')`
  *                  - `array('name of method' => array('arg1', 'arg2'))`
  * @param Model $Model
  * @param string $file Absolute path to the source file
  * @param array $process directory, version, instructions
  * @return boolean `true` if version for the file was successfully stored
  */
 function makeVersion(&$Model, $file, $process)
 {
     extract($this->settings[$Model->alias]);
     /* Process clone instruction */
     if (isset($process['instructions']['clone'])) {
         $action = $process['instructions']['clone'];
         if (!in_array($action, array('copy', 'link', 'symlink'))) {
             return false;
         }
         $destination = $this->_destinationFile($file, $process['directory'], null, $overwrite);
         if (!$destination) {
             return false;
         }
         return call_user_func($action, $file, $destination) && chmod($destination, $mode);
     }
     /* Process media transforms */
     try {
         $Media = Media_Process::factory(array('source' => $file));
     } catch (Exception $E) {
         return false;
     }
     foreach ($process['instructions'] as $key => $value) {
         if (is_int($key)) {
             $method = $value;
             $args = null;
         } else {
             $method = $key;
             $args = (array) $value;
         }
         if (!method_exists($Media, $method)) {
             return false;
         }
         $result = call_user_func_array(array($Media, $method), $args);
         if ($result === false) {
             return false;
         } elseif (is_a($result, 'Media_Process_Generic')) {
             $Media = $result;
         }
     }
     /* Determine destination file */
     $extension = null;
     if ($guessExtension) {
         if (isset($process['instructions']['convert'])) {
             $extension = Mime_Type::guessExtension($process['instructions']['convert']);
         } else {
             $extension = Mime_Type::guessExtension($file);
         }
     }
     $destination = $this->_destinationFile($file, $process['directory'], $extension, $overwrite);
     if (!$destination) {
         return false;
     }
     return $Media->store($destination) && chmod($destination, $mode);
 }