public function testMediaChangeButSameAdapter() { Media_Process::config(array('image' => 'GenericMock', 'document' => 'GenericMock')); $media = new Media_Process_Document(array('source' => "{$this->_files}/application_pdf.pdf", 'adapter' => 'GenericMock')); $result = $media->convert('image/jpg'); $this->assertType('Media_Process_Image', $result); }
public static function config(array $config = array()) { if (!$config) { return self::$_config; } self::$_config = $config; }
public function testMediaChangeDifferentAdapter() { Media_Process::config(array('image' => 'GenericMock', 'video' => 'GenericNameMock')); $source = fopen("{$this->_files}/video_theora_notag.ogv", 'rb'); $storeFrom = fopen("{$this->_files}/image_jpg.jpg", 'rb'); $adapter = new Media_Process_Adapter_GenericNameMock($source); $adapter->storeCopyFromStream = $storeFrom; $media = new Media_Process_Video(compact('adapter')); $result = $media->convert('image/jpg'); $this->assertInstanceOf('Media_Process_Image', $result); fclose($source); fclose($storeFrom); }
public function testMediaFactoryTransplantAdapter() { $result = Media_Process::factory(array('adapter' => new Media_Process_Adapter_GenericMock(null), 'source' => 'image/jpeg')); $this->assertType('Media_Process_Image', $result); }
public function testHasManyWithMissingMediaAdapters() { $_backupConfig = Configure::read('Media'); $_backupProcess = Media_Process::config(); $_backupInfo = Media_Info::config(); $s = array('convert' => 'image/png', 'zoomCrop' => array(100, 100)); $m = array('convert' => 'image/png', 'fitCrop' => array(300, 300)); $l = array('convert' => 'image/png', 'fit' => array(600, 440)); Configure::write('Media.filter', array('audio' => compact('s', 'm'), 'document' => compact('s', 'm'), 'generic' => array(), 'image' => compact('s', 'm', 'l'), 'video' => compact('s', 'm'))); Media_Process::config(array('image' => null)); Media_Info::config(array('image' => null)); $Model = $this->_model('hasMany'); $file = $this->Data->getFile(array('image-jpg.jpg' => 'ta.jpg')); $data = array('Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'), 'Attachment' => array(array('file' => $file, 'model' => 'Movie'))); $Model->create(); $result = false; $expected = null; try { $result = $Model->saveAll($data, array('validate' => 'first')); } catch (Exception $exception) { $expected = $exception; } if ($expected === null) { $this->fail('Expected Model::saveAll to raise an error.'); } $this->assertFalse($result); $this->assertTrue(file_exists($this->Data->settings['transfer'] . 'img' . DS . 'ta.jpg')); $result = $Model->find('first', array('conditions' => array('title' => 'Weekend'))); $expected = array(0 => array('id' => '1', 'model' => 'Movie', 'foreign_key' => '4', 'dirname' => 'img', 'basename' => 'ta.jpg', 'checksum' => '073addc9c90e4d20a9a19d8a31e01b39', 'group' => null, 'alternative' => null, 'path' => 'img/ta.jpg')); $this->assertEqual($result['Attachment'], $expected); Media_Process::config($_backupProcess); Media_Info::config($_backupInfo); Configure::write('Media', $_backupConfig); }
/** * 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) { if (!$this->_adapter->convert($mimeType)) { return false; } if ($this->name() != Mime_Type::guessName($mimeType)) { // i.e. document -> image $config = Media_Process::config(); if ($config[$this->name()] == $config[Mime_Type::guessName($mimeType)]) { $media = Media_Process::factory(array('source' => $mimeType, 'adapter' => $this->_adapter)); } else { $handle = fopen('php://temp', 'w+'); if (!$this->_adapter->store($handle)) { // err } $media = Media_Process::factory(array('source' => $handle)); fclose($handle); } return $media; } return $this; }
/** * 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); }
public function testMakeVersionUnknownMethodsArePassedThrough() { $config = Media_Process::config(); $message = 'Need imagick media processing adapters configured for both image.'; $skipped = $this->skipIf(!isset($config['image']) || $config['image'] != 'Imagick', $message); if ($skipped) { return; } $Model = ClassRegistry::init('Unicorn', 'Model'); $Model->Behaviors->load('Media.Generator', $this->behaviorSettings); $directory = $this->Data->settings['filter'] . 's' . DS; mkdir($directory); $file = $this->Data->getFile(array('image-jpg.jpg' => 'image.jpg')); $result = $Model->Behaviors->Generator->makeVersion($Model, $file, array('version' => 's', 'directory' => $directory, 'instructions' => array('setFormat' => 'png'))); $this->assertTrue($result); $mimeType = Mime_Type::guessType($directory . 'image.jpg', array('paranoid' => true)); $this->assertEqual($mimeType, 'image/png'); }
} if ($cached = Cache::read('mime_type_glob')) { Mime_Type::config('Glob', array('adapter' => 'Memory')); foreach ($cached as $item) { Mime_Type::$glob->register($item); } } else { Mime_Type::config('Glob', array('adapter' => 'Freedesktop', 'file' => $mm . DS . 'data' . DS . 'glob.db')); Cache::write('mime_type_glob', Mime_Type::$glob->to('array')); } /** * Configure the adpters to be used by 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. * * @see GeneratorBehavior */ require_once 'Media/Process.php'; Media_Process::config(array('document' => $hasImagick ? 'Imagick' : null, 'image' => $hasImagick ? 'Imagick' : 'Gd')); /** * Configure the adpters to be used by media info class. Adjust this * mapping of media names to adapters according to your environment. In contrast * to `Media_Proces` which operates only with one adapter per media type * `Media_Info` can use multiple adapter per media type. * * @see MetaBehavior */ require_once 'Media/Info.php'; Media_Info::config(array('image' => $hasImagick ? array('ImageBasic', 'Imagick') : array('ImageBasic')));
/** * 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); }
function testHasManyWithMissingMediaAdapters() { $_backupConfig = Configure::read('Media'); $_backupProcess = Media_Process::config(); $_backupInfo = Media_Info::config(); $s = array('convert' => 'image/png', 'zoomCrop' => array(100, 100)); $m = array('convert' => 'image/png', 'fitCrop' => array(300, 300)); $l = array('convert' => 'image/png', 'fit' => array(600, 440)); Configure::write('Media.filter', array('audio' => compact('s', 'm'), 'document' => compact('s', 'm'), 'generic' => array(), 'image' => compact('s', 'm', 'l'), 'video' => compact('s', 'm'))); Media_Process::config(array('image' => null)); Media_Info::config(array('image' => null)); $Model = $this->_model('hasMany'); $file = $this->Data->getFile(array('image-jpg.jpg' => 'ta.jpg')); $data = array('Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'), 'Attachment' => array(array('file' => $file, 'model' => 'Movie'))); $this->expectError(); $this->expectError(); $this->expectError(); $Model->create(); $result = $Model->saveAll($data, array('validate' => 'first')); $this->assertTrue($result); $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'ta.jpg')); $result = $Model->find('first', array('conditions' => array('title' => 'Weekend'))); $expected = array(0 => array('id' => 1, 'model' => 'Movie', 'foreign_key' => 4, 'dirname' => 'img', 'basename' => 'ta.jpg', 'checksum' => '1920c29e7fbe4d1ad2f9173ef4591133', 'group' => null, 'alternative' => null)); $this->assertEqual($result['Attachment'], $expected); Media_Process::config($_backupProcess); Media_Info::config($_backupInfo); Configure::write('Media', $_backupConfig); }
function testMakeVersionAccrossMedia() { $config = Media_Process::config(); $message = '%s Need media processing adapters configured for both image and document.'; $skipped = $this->skipIf(!isset($config['image'], $config['document']), $message); if ($skipped) { return; } $Model = ClassRegistry::init('Unicorn', 'Model'); $Model->Behaviors->attach('Media.Generator', $this->_behaviorSettings); $file = $this->Data->getFile(array('application-pdf.pdf' => $this->Folder->pwd() . 'application-pdf.pdf')); $directory = $this->Folder->pwd() . 'filter' . DS . 's' . DS; mkdir($directory); $result = $Model->Behaviors->Generator->makeVersion($Model, $file, array('version' => 's', 'directory' => $directory, 'instructions' => array('convert' => 'image/png'))); $this->assertTrue($result); $this->assertTrue(file_exists($directory . 'application-pdf.png')); }