public function testGuessNameFile() { $map = ['video_h264_qt_tag.mp4' => 'video']; foreach ($map as $file => $name) { $this->assertEquals($name, Type::guessName($this->_files . '/' . $file), "File `{$file}`."); } }
protected function setUp() { $this->_files = dirname(dirname(dirname(dirname(__FILE__)))) . '/data'; $this->_data = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/data'; Type::config('magic', ['adapter' => 'Freedesktop', 'file' => $this->_data . '/magic.db']); Type::config('glob', ['adapter' => 'Freedesktop', 'file' => $this->_data . '/glob.db']); }
protected function setUp() { $this->_files = dirname(dirname(dirname(__FILE__))) . '/data'; $this->_data = dirname(dirname(dirname(dirname(__FILE__)))) . '/data'; Process::config(['image' => new GenericMock(null), 'audio' => new GenericMock(null), 'document' => new GenericMock(null), 'video' => new GenericMock(null)]); Type::config('magic', ['adapter' => 'Freedesktop', 'file' => "{$this->_data}/magic.db"]); Type::config('glob', ['adapter' => 'Freedesktop', 'file' => "{$this->_data}/glob.db"]); }
public function convert($mimeType) { if (Type::guessName($mimeType) != 'image') { return true; } if (!isset($this->_formatMap[$mimeType])) { throw new OutOfBoundsException("MIME type `{$mimeType}` cannot be mapped to a format."); } return $this->_object->setFormat($this->_formatMap[$mimeType]); }
public function testConvertToVideo() { $source = fopen("{$this->_files}/video_theora_comments.ogv", 'r'); $target = fopen('php://temp', 'wb'); $subject = new FfmpegShell($source); $subject->convert('video/mpeg'); $result = $subject->store($target); $this->assertTrue($result); $this->assertEquals('video/mpeg', Type::guessType($target)); fclose($source); fclose($target); }
public function testConvertImageToImage() { $source = fopen("{$this->_files}/image_png.png", 'r'); $target = fopen('php://temp', 'wb'); $subject = new Gd($source); $subject->convert('image/jpeg'); $result = $subject->store($target); $this->assertTrue($result); $this->assertEquals('image/jpeg', Type::guessType($target)); fclose($source); fclose($target); }
public function testConvert() { $source = fopen("{$this->_files}/audio_vorbis_comments.ogg", 'r'); $target = fopen('php://temp', 'wb'); $subject = new SoxShell($source); $subject->convert('audio/x-wav'); $result = $subject->store($target); $this->assertTrue($result); $this->assertEquals('audio/x-wav', Type::guessType($target)); fclose($source); fclose($target); }
/** * This factory method takes a source or an instance of an adapter, * guesses the type of media maps it to a media information class * and instantiates it. * * @param array $config Valid values are: * - `'source'`: An absolute path to a file. * - `'adapters'`: Names or instances of media adapters (i.e. `['Gd']`). * @return \mm\Media\Info\Generic An instance of a subclass of `\mm\Media\Info\Generic` or * if type could not be mapped an instance of the that class itself. */ public static function factory(array $config = []) { $default = ['source' => null, 'adapters' => []]; extract($config + $default); if (!$source) { throw new BadMethodCallException("No source given."); } $name = Type::guessName($source); $class = "\\mm\\Media\\Info\\" . ucfirst($name); if (!$adapters) { if (!isset(static::$_config[$name])) { throw new Exception("No adapters configured for media name `{$name}`."); } $adapters = static::$_config[$name]; } return new $class(compact('source', 'adapters')); }
* variant with the addtion of a `Fileinfo` magic adapter. Not all adapters require * a file to be passed along with the configuration. */ use mm\Mime\Type; if ($hasFileinfo) { Type::config('magic', ['adapter' => 'Fileinfo']); } else { Type::config('magic', ['adapter' => 'Freedesktop', 'file' => __DIR__ . '/data/magic.db']); } if ($cached = $cacheRead('mime_type_glob')) { Type::config('glob', ['adapter' => 'Memory']); foreach ($cached as $item) { Type::$glob->register($item); } } else { Type::config('glob', ['adapter' => 'Freedesktop', 'file' => __DIR__ . '/data/glob.db']); $cacheWrite('mime_type_glob', Type::$glob->to('array')); } /* * Configure the adpters to be used by the media process class. Adjust this * mapping of media names to adapters according to your environment. For example: * most PHP installations have GD enabled thus should choose the `Gd` adapter for * image transformations. However the `Imagick` adapter may be more desirable * in other cases and also supports transformations for documents. */ use mm\Media\Process; Process::config(['document' => $hasImagick ? 'Imagick' : null, 'image' => $hasImagick ? 'Imagick' : 'Gd']); /* * Configure the adpters to be used by the media info class. Adjust this * mapping of media names to adapters according to your environment. In contrast * to `Process` which operates only with one adapter per media type
/** * Convert the track using the best possible means * @param string $filename The new filename * @return object New Media Object */ public function convertMultiple($newFilename, $codecs = array()) { if (empty($codecs)) { return false; } $intermediary = $this->createIntermediaries(); //generate wav form png if (isset($this->image)) { $waveform = new \Jasny\Audio\Waveform($intermediary['wav']['path'], array("width" => 700)); $waveform->save("png", $this->image); } //generate final file foreach ($codecs as $codec) { $parts = pathinfo($newFilename); $base = dirname($newFilename); $file = $base . "/" . $parts['filename'] . "." . $codec; $extension = Type::guessExtension($file); if (empty($extension) || $extension == "bin") { $extension = $codec; } $mime = Type::guessType($file); foreach ($this->getDrivers() as $driver) { $class = "Media\\Driver\\Drivers\\" . $driver; if ($driver == "AsteriskShell") { $i = $intermediary['sln']; } else { $i = $intermediary['wav']; } if ($class::installed() && $class::isCodecSupported($extension, "out")) { $driver = new $class($i['path'], $i['extension'], $i['mime']); $driver->convert($file, $extension, $mime); if (!file_exists($file)) { throw new \Exception("File was not converted"); } break; } } } if (!empty($intermediary['wav']['path']) && file_exists($intermediary['wav']['path'])) { unlink($intermediary['wav']['path']); } if (!empty($intermediary['sln']['path']) && file_exists($intermediary['sln']['path'])) { unlink($intermediary['sln']['path']); } unset($intermediary); return file_exists($newFilename); }
public function convert($mimeType) { if (Type::guessName($mimeType) != 'image') { return true; } if (!isset($this->_formatMap[$mimeType])) { throw new OutOfBoundsException("Conversion to MIME type `{$mimeType}` not supported."); } return $this->_format = $this->_formatMap[$mimeType]; }
/** * Converts the media to given MIME type. * * @param string $mimeType * @return boolean|object false on error or a Media object on success */ public function convert($mimeType) { $this->_adapter->convert($mimeType); if ($this->name() != Type::guessName($mimeType)) { // Crosses media (i.e. document -> image). $config = Process::config(); if ($config[$this->name()] == $config[Type::guessName($mimeType)]) { // ...but using the same adapter. $media = Process::factory(['source' => $mimeType, 'adapter' => $this->_adapter]); } else { // ...using different adapters. $handle = fopen('php://temp', 'w+'); $this->_adapter->store($handle); $media = Process::factory(['source' => $handle]); fclose($handle); } return $media; } // Stays entirely in same media (i.e. image -> image). return $this; }
protected function _type($object) { $type = Type::guessExtension($object); $map = ['ogv' => 'ogg', 'oga' => 'ogg', 'jpg' => 'mjpeg']; return isset($map[$type]) ? $map[$type] : $type; }
public function testPassthru() { $source = fopen("{$this->_files}/image_png.png", 'r'); $target = fopen('php://temp', 'wb'); $subject = new Imagick($source); $subject->passthru('setFormat', 'jpeg'); $result = $subject->store($target); $this->assertTrue($result); $this->assertEquals('image/jpeg', Type::guessType($target)); fclose($source); fclose($target); }
public function testGuessNameResource() { $handleA = fopen("{$this->_files}/application_pdf.pdf", 'r'); $handleB = fopen('php://temp', 'r+'); stream_copy_to_stream($handleA, $handleB); $this->assertEquals('document', Type::guessName($handleA)); $this->assertEquals('document', Type::guessName($handleB)); fclose($handleA); fclose($handleB); }