/** * Get thumbnail path * * @param string $type * @param Replica_ImageProxy_Abstract $proxy * @return null|string - Relative path to thumbnail */ function thumbnail_path($type, Replica_ImageProxy_Abstract $proxy) { $config = sfReplicaThumbnail::getConfig($type); // Has image if ($proxy->getUid()) { sfReplicaThumbnail::loadMacro($type, $config['macro']); // If image not found return null try { $proxy->setMimeType($config['mimetype']); if (null !== $config['quality']) { $proxy->setQuality($config['quality']); } return sfConfig::get('app_thumbnail_dir') . '/' . Replica::cache()->get($type, $proxy); } catch (Replica_Exception_ImageNotInitialized $e) { if ($config['required']) { throw $e; } return; } // Default image } else { if (isset($config['default'])) { return $config['default']; } } }
/** * Возвращает путь к превьюшке * * @param string $type * @return string */ public function getImagePath($type) { if ($this->isNew() || !$this->size) { $proxy = new Replica_ImageProxy_FromFile(null); } else { $proxy = new sfReplicaImageDoctrine($this); } $config = sfReplicaThumbnail::getConfig($type); // Has image if ($proxy->getUid()) { sfReplicaThumbnail::loadMacro($type, $config['macro']); // If image not found return null try { $path = sfConfig::get('app_thumbnail_dir') . '/' . Replica::cache()->get($type, $proxy, $config['mimetype']); } catch (Replica_Exception_ImageNotInitialized $e) { return; } // Default image } else { if (isset($config['default'])) { $path = $config['default']; } } return $path; }
/** * Load macro once */ public function testLoadMacroOnce() { // first $config = array('Replica_Macro_Null' => array()); sfReplicaThumbnail::loadMacro('load_once', $config); // second $config = array('Replica_Macro_ThumbnailFit' => array($width = 10, $height = 20)); sfReplicaThumbnail::loadMacro('load_once', $config); $this->assertTrue(Replica::hasMacro('load_once'), 'Macro is initialized'); $this->assertType('Replica_Macro_Chain', $macro = Replica::getMacro('load_once')); $this->assertEquals(array('Replica_Macro_Null' => array()), $macro->getParameters()); }
/** * Get thumbnail */ public function testGetTumbnail() { $this->_setConf('logo', array('mimetype' => 'image/jpg', 'quality' => 25)); $proxy = new Replica_ImageProxy_FromFile('/some/file'); $proxy->setMimeType('image/jpg'); $proxy->setQuality(25); $cache = $this->getMock('Replica_Macro_CacheManager', array('get'), array('/save/dir')); $cache->expects($this->once())->method('get')->with('logo', $proxy)->will($this->returnValue($path = 'path/to/thumbnail')); Replica::setCacheManager($cache); $expected = sprintf('<img src="/upload/%s" alt="" />', $path); $this->assertEquals($expected, thumbnail('logo', new Replica_ImageProxy_FromFile('/some/file'))); }
/** * Load and register macro if not defined * * @param string $type - thumbnail type * @param array $config - macro definition * @return void */ public static function loadMacro($type, array $config) { if (!Replica::hasMacro($type)) { $chain = new Replica_Macro_Chain(); foreach ($config as $class => $args) { $reflection = new ReflectionClass($class); if ($args && is_array($args)) { $macro = $reflection->newInstanceArgs($args); } else { $macro = $reflection->newInstance(); } $chain->add($macro); } Replica::setMacro($type, $chain); } }
/** * Initialize * * @see sfPluginConfiguration */ public function initialize() { $dir = sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . ltrim(sfConfig::get('app_thumbnail_dir'), '\\/'); Replica::setCacheManager(new Replica_Macro_CacheManager($dir)); }
/** * TearDown */ public function tearDown() { sfConfig::clear(); Replica::removeAll(); Replica::setCacheManager(null); }