public function testMediaFactoryTransplantAdapter() { $result = Media_Process::factory(array('adapter' => new Media_Process_Adapter_GenericMock(null), 'source' => 'image/jpeg')); $this->assertType('Media_Process_Image', $result); }
/** * 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); }
/** * 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 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); }