Created
April 14, 2015 11:41
-
-
Save pekkis/3931dcc95771f51d7287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Xi\Filelib\Event\VersionProviderEvent; | |
| use Xi\Filelib\File\File; | |
| use Xi\Filelib\Plugin\Image\ArbitraryVersionPlugin; | |
| use Xi\Filelib\Plugin\Image\VersionPlugin; | |
| use Xi\Filelib\Plugin\VersionProvider\Events as VersionProviderEvents; | |
| use Xi\Filelib\Plugin\VersionProvider\OriginalVersionPlugin; | |
| use Xi\Filelib\Publisher\Adapter\Filesystem\SymlinkFilesystemPublisherAdapter; | |
| use Xi\Filelib\Publisher\Linker\ReversibleCreationTimeLinker; | |
| use Xi\Filelib\Publisher\Publisher; | |
| use Xi\Filelib\Version; | |
| use Xi\Filelib\Plugin\Image\Command\Command; | |
| use OpenCloud\OpenStack; | |
| use League\Flysystem\Filesystem; | |
| use League\Flysystem\Rackspace\RackspaceAdapter; | |
| use Xi\Filelib\Storage\Adapter\FlysystemStorageAdapter; | |
| use Xi\Filelib\Storage\Storage; | |
| use Xi\Filelib\Backend\Adapter\DoctrineDbalBackendAdapter; | |
| use Doctrine\DBAL\DriverManager; | |
| use Xi\Filelib\Publisher\Adapter\FlysystemPublisherAdapter; | |
| use Xi\Filelib\Events as CoreEvents; | |
| use Xi\Filelib\Event\FileEvent; | |
| use Xi\Filelib\Plugin\Image\Command\ExecuteMethodCommand; | |
| /** | |
| * Versions an image | |
| */ | |
| class TussiVersionPlugin extends VersionPlugin | |
| { | |
| /** | |
| * @param File $file | |
| * @param Version $version | |
| * @return array | |
| */ | |
| protected function doCreateTemporaryVersion(File $file, Version $version) | |
| { | |
| $retrieved = $this->storage->retrieve( | |
| $file->getResource() | |
| ); | |
| /** @var \Xi\Filelib\Plugin\Image\VersionPluginVersion $versionVersion */ | |
| $v = $this->versions[$version->getVersion()]; | |
| $c = new ExecuteMethodCommand( | |
| 'rotateImage', | |
| [ | |
| new ImagickPixel('none'), | |
| $file->getData()->get('rotate') | |
| ] | |
| ); | |
| $v->setCommand(4, $c); | |
| return array( | |
| $version->toString(), | |
| $v->getHelper($retrieved, $this->tempDir)->execute(), | |
| ); | |
| } | |
| /** | |
| * @return bool | |
| */ | |
| public function isSharedResourceAllowed() | |
| { | |
| return true; | |
| } | |
| /** | |
| * @return bool | |
| */ | |
| public function areSharedVersionsAllowed() | |
| { | |
| return false; | |
| } | |
| } | |
| $publisherAdapter = new SymlinkFilesystemPublisherAdapter(__DIR__ . '/web/files', '600', '700', 'files'); | |
| /* | |
| $client = new OpenStack( | |
| 'https://identity.fi-1.nebulacloud.fi:5000/v2.0/', | |
| [ | |
| 'username' => 'jorma@jtc.fi', | |
| 'password' => 'FE50be0XjKXzH9lAUyBVCA', | |
| 'tenantId' => '17d75e7708fe4a76aef711bc80fe3d65', | |
| ] | |
| ); | |
| $store = $client->objectStoreService("swift", 'fi-1'); | |
| $container = $store->getContainer('public'); | |
| $filesystem = new Filesystem( | |
| new RackspaceAdapter($container) | |
| ); | |
| $publisherAdapter = new FlysystemPublisherAdapter( | |
| $filesystem, | |
| 'http://objects.fi-1.nebulacloud.fi/swift/v1/public' | |
| ); | |
| */ | |
| $publisher = new Publisher( | |
| $publisherAdapter, | |
| new ReversibleCreationTimeLinker() | |
| ); | |
| $publisher->attachTo($filelib); | |
| $originalPlugin = new OriginalVersionPlugin('original'); | |
| $filelib->addPlugin($originalPlugin, array('default')); | |
| $filelib->getEventDispatcher()->addListener( | |
| CoreEvents::FILE_AFTER_CREATE, | |
| function(FileEvent $event) { | |
| $file = $event->getFile(); | |
| $rotatores = [ | |
| 90, | |
| 180, | |
| 270 | |
| ]; | |
| $file->getData()->set('rotate', $rotatores[array_rand($rotatores)]); | |
| } | |
| ); | |
| /* | |
| $arbitraryPlugin = new ArbitraryVersionPlugin( | |
| 'arbitrary', | |
| function () { | |
| return array( | |
| 'x' | |
| ); | |
| }, | |
| function () { | |
| return array( | |
| 'x2' | |
| ); | |
| }, | |
| function () { | |
| return array( | |
| 'x' => 800 | |
| ); | |
| }, | |
| function (Version $version) { | |
| $params = $version->getParams(); | |
| if (!is_numeric($params['x'])) { | |
| return false; | |
| } | |
| if ($params['x'] < 200 || $params['x'] > 2000) { | |
| return false; | |
| } | |
| if ($params['x'] % 100) { | |
| return false; | |
| } | |
| return true; | |
| }, | |
| function (File $file, Version $version, ArbitraryVersionPlugin $plugin) { | |
| $params = $version->getParams(); | |
| if ($version->hasModifier('x2')) { | |
| $params['x'] = $params['x'] * 2; | |
| } | |
| return Command::createCommandsFromDefinitions( | |
| [ | |
| array('setImageCompression',Imagick::COMPRESSION_JPEG), | |
| array('setImageFormat', 'jpg'), | |
| array('setImageCompressionQuality', 80), | |
| array('cropThumbnailImage', array($params['x'], round($params['x'] / 4))), | |
| 'Xi\Filelib\Plugin\Image\Command\WatermarkCommand' => array(__DIR__ . '/watermark.png', 'se', 10), | |
| ] | |
| ); | |
| }, | |
| 'image/jpeg', | |
| true, | |
| function() { | |
| die('cocoocococ'); | |
| } | |
| ); | |
| $filelib->addPlugin($arbitraryPlugin); | |
| */ | |
| $tussiPlugin = new TussiVersionPlugin( | |
| array( | |
| 'tussi' => array( | |
| array( | |
| array('setImageCompression',Imagick::COMPRESSION_JPEG), | |
| array('setImageFormat', 'jpg'), | |
| array('setImageCompressionQuality', 80), | |
| array('cropThumbnailImage', array(800, 800)), | |
| array('rotateImage', [new \ImagickPixel('none'), 0]), | |
| 'Xi\Filelib\Plugin\Image\Command\WatermarkCommand' => array(__DIR__ . '/watermark.png', 'se', 10), | |
| ), | |
| 'image/jpeg' | |
| ), | |
| 'lussi' => array( | |
| array( | |
| array('setImageCompression',Imagick::COMPRESSION_JPEG), | |
| array('setImageFormat', 'jpg'), | |
| array('setImageCompressionQuality', 20), | |
| array('cropThumbnailImage', array(400, 400)), | |
| array('rotateImage', [new \ImagickPixel('none'), 0]), | |
| // 'Xi\Filelib\Plugin\Image\Command\WatermarkCommand' => array(__DIR__ . '/watermark.png', 'se', 10), | |
| ), | |
| 'image/jpeg' | |
| ) | |
| ) | |
| ); | |
| $filelib->addPlugin($tussiPlugin, [], 'tussi'); | |
| $versionPlugin = new VersionPlugin( | |
| array( | |
| 'cinemascope' => array( | |
| array( | |
| array('setImageCompression',Imagick::COMPRESSION_JPEG), | |
| array('setImageFormat', 'jpg'), | |
| array('setImageCompressionQuality', 50), | |
| array('cropThumbnailImage', array(800, 200)), | |
| array('sepiaToneImage', 90), | |
| 'Xi\Filelib\Plugin\Image\Command\WatermarkCommand' => array(__DIR__ . '/watermark.png', 'se', 10), | |
| ), | |
| 'image/jpeg' | |
| ) | |
| ) | |
| ); | |
| // $versionPlugin->enableLazyMode(); | |
| $filelib->addPlugin($versionPlugin); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once __DIR__ . '/../bootstrap.php'; | |
| require_once __DIR__ . '/../lazy-publisher-common.php'; | |
| $file = $filelib->uploadFile(__DIR__ . '/../manatees/manatus-25.jpg'); | |
| $publisher->publishAllVersions($file); | |
| /* | |
| $file->getData()->set('rotate', 77); | |
| $tussiPlugin = $filelib->getPluginManager()->getPlugin('tussi'); | |
| $tussiPlugin->provideVersion($file, \Xi\Filelib\Version::get('tussi')); | |
| */ | |
| ?> | |
| <html> | |
| <head> | |
| <title>Filelib Examples</title> | |
| <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"> | |
| <link href="filelib.css" rel="stylesheet"> | |
| <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>You just published a picture of a lazy manatee (are there other kinds?)</h1> | |
| <p> | |
| <img src="<?php echo $publisher->getUrl($file, 'tussi'); ?>" /> | |
| </p> | |
| <p> | |
| <img src="<?php echo $publisher->getUrl($file, 'lussi'); ?>" /> | |
| </p> | |
| </div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment