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); }
protected function setUp() { $this->_files = dirname(dirname(dirname(__FILE__))) . '/data'; $this->_data = dirname(dirname(dirname(dirname(__FILE__)))) . '/data'; Media_Process::config(array('image' => new Media_Process_Adapter_GenericMock(null), 'audio' => new Media_Process_Adapter_GenericMock(null), 'document' => new Media_Process_Adapter_GenericMock(null), 'video' => new Media_Process_Adapter_GenericMock(null))); Mime_Type::config('Magic', array('adapter' => 'Freedesktop', 'file' => "{$this->_data}/magic.db")); Mime_Type::config('Glob', array('adapter' => 'Freedesktop', 'file' => "{$this->_data}/glob.db")); }
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 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; }
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')));
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')); }