Author: Antoine Hérault (antoine.herault@gmail.com)
Example #1
0
 /**
  * {@inheritdoc}
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderName()));
     }
     $image = $this->adapter->load($in->getContent());
     $size = $media->getBox();
     if (null != $settings['height']) {
         if ($size->getHeight() > $size->getWidth()) {
             $higher = $size->getHeight();
             $lower = $size->getWidth();
         } else {
             $higher = $size->getWidth();
             $lower = $size->getHeight();
         }
         $crop = $higher - $lower;
         if ($crop > 0) {
             $point = $higher == $size->getHeight() ? new Point(0, 0) : new Point($crop / 2, 0);
             $image->crop($point, new Box($lower, $lower));
             $size = $image->getSize();
         }
     }
     $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
         $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->mode)->get($format, array('quality' => $settings['quality']));
     } else {
         $content = $image->get($format, array('quality' => $settings['quality']));
     }
     $out->setContent($content, $this->metadata->get($media, $out->getName()));
 }
 /**
  * {@inheritdoc}
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderName()));
     }
     $image = $this->adapter->load($in->getContent());
     $content = $image->thumbnail($this->getBox($media, $settings), $this->mode)->get($format, array('quality' => $settings['quality']));
     $out->setContent($content, $this->metadata->get($media, $out->getName()));
 }
 public static function goodInputs()
 {
     $filesystem1 = new Filesystem(new Local(FILESYSTEM1));
     $gaufrette = new File('key1', $filesystem1);
     $gaufrette->setContent(self::CONTENT);
     $file = new FSiFile('key2', $filesystem1);
     $file->setContent(self::CONTENT);
     return array(array($gaufrette), array($file));
 }
Example #4
0
 /**
  * @inheritdoc
  * @throws \RuntimeException
  */
 public function download($id, FileInterface $output)
 {
     $source = new GFile($id, $this->filesystem);
     ErrorHandler::start();
     $flag = file_put_contents($output->getPath(), $source->getContent());
     ErrorHandler::stop(true);
     if ($flag === false) {
         throw new \RuntimeException("Could not download file ({$id})");
     }
 }
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  * @param \Gaufrette\File $in
  * @param \Gaufrette\File $out
  * @param string $format
  * @param array $settings
  * @return void
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderClass()));
     }
     $image = $this->getAdapter()->load($in->getContent());
     if ($settings['height'] == null) {
         $size = $image->getSize();
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->getMode())->get($format);
     $out->setContent($content);
 }
Example #6
0
 /**
  * {@inheritdoc}
  * @return Bpi\ApiBundle\Domain\Factory\ResourceBuilder
  */
 public function extract()
 {
     $entity = $this->doc->getEntity('resource');
     $builder = new ResourceBuilder();
     $fs = new Filesystem(new MemoryAdapter());
     $entity->walk(function ($e) use($builder, $fs) {
         if ($e->typeOf(Property::TYPE_ASSET)) {
             $file = new File($e->getName(), $fs);
             $file->setContent($e->getValue());
             $builder->addFile($file);
         }
     });
     return $builder->title($entity->property('title')->getValue())->body($entity->property('body')->getValue())->teaser($entity->property('teaser')->getValue())->ctime(new \DateTime($entity->property('ctime')->getValue()));
 }
 /**
  * Resize Image Media file. Sizes is based on ImageSizes class.
  * @param Media $media
  * @param array $sizes 
  * @param bool $forceCrop
  */
 function resize(Media $media, $sizes = array(), $forceCrop = true)
 {
     if (count($sizes)) {
         $in = new File($media->getName(), $this->filesystem);
         $fileArr = explode(".", $in->getName());
         $format = array_pop($fileArr);
         if (!$forceCrop) {
             $this->resizer->setModeToInset();
         }
         foreach ($sizes as $each) {
             $out = new File($each . '_' . $media->getName(), $this->filesystem);
             $imageSize = ImageSizes::toArray($each);
             $this->resizer->resize($media, $in, $out, $format, $imageSize);
         }
     }
 }
 public function testCreateAnDeleteFile()
 {
     $fs = $this->getFilesystem();
     $filename = uniqid('test_');
     $file = new File($filename, $fs);
     $this->assertFalse($file->exists());
     $file->setContent('Hello');
     $this->assertTrue($file->exists());
     $file->delete();
     $this->assertFalse($file->exists());
 }
 /**
  * Returns the size of the file
  *
  * !! WARNING !!
  * Calling this loads the entire file into memory,
  * unless it is on a stream-capable filesystem.
  * In case of bigger files this could throw exceptions,
  * and will have heavy performance footprint.
  * !! ------- !!
  *
  */
 public function getSize()
 {
     // This can only work on streamable files, so basically local files,
     // still only perform it once even on local files to avoid bothering the filesystem.php g
     if ($this->filesystem->getAdapter() instanceof StreamFactory && !$this->size) {
         if ($this->streamWrapperPrefix) {
             try {
                 $this->setSize(filesize($this->streamWrapperPrefix . $this->getKey()));
             } catch (\Exception $e) {
                 // Fail gracefully if there was a problem with opening the file and
                 // let gaufrette load the file into memory allowing it to throw exceptions
                 // if that doesn't work either.
                 // Not empty to make the scrutiziner happy.
                 return parent::getSize();
             }
         }
     }
     return parent::getSize();
 }
 /**
  * @param File $file
  */
 public function __construct(File $file)
 {
     $this->domains = Utilities::parseLines($file->getContent());
 }
 /**
  * {@inheritDoc}
  */
 public function createFile($key, Filesystem $filesystem)
 {
     $file = new File($key, $filesystem);
     $stat = $this->sftp->stat($this->computePath($key));
     if (isset($stat['size'])) {
         $file->setSize($stat['size']);
     }
     return $file;
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function createFile($key, Filesystem $filesystem)
 {
     $this->ensureDirectoryExists($this->directory, $this->create);
     $file = new File($key, $filesystem);
     if (!array_key_exists($key, $this->fileData)) {
         $dirname = \Gaufrette\Util\Path::dirname($key);
         $directory = $dirname == '.' ? '' : $dirname;
         $this->listDirectory($directory);
     }
     if (isset($this->fileData[$key])) {
         $fileData = $this->fileData[$key];
         $file->setName($fileData['name']);
         $file->setSize($fileData['size']);
     }
     return $file;
 }
Example #13
0
 /**
  * {@inheritDoc}
  */
 public function createFile($key, Filesystem $filesystem)
 {
     if (!$this->exists($key)) {
         throw new \RuntimeException(sprintf('The \'%s\' file does not exist.', $key));
     }
     $file = new File($key, $filesystem);
     if (!array_key_exists($key, $this->fileData)) {
         $directory = dirname($key) == '.' ? '' : dirname($key);
         $this->listDirectory($directory);
     }
     $fileData = $this->fileData[$key];
     $created = new \DateTime();
     $created->setTimestamp($fileData['time']);
     $file->setName($fileData['name']);
     $file->setCreated($created);
     $file->setSize($fileData['size']);
     return $file;
 }
 /**
  * write file
  *
  * @param $key
  * @param $content
  * @param bool $overwrite
  */
 public function write($key, $content, $overwrite = true)
 {
     $file = new File($key, $this->filesystem);
     $file->setContent($content, $this->getMetadata());
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function delete()
 {
     $this->file->delete();
 }
 /**
  * Return the upload status.
  *
  * @param  \SRIO\RestUploadBundle\Upload\UploadContext $context
  * @param  ResumableUploadSession                      $uploadSession
  * @param  \Gaufrette\File                             $file
  * @param  array                                       $range
  * @return UploadResult
  */
 protected function requestUploadStatus(UploadContext $context, ResumableUploadSession $uploadSession, File $file, array $range)
 {
     if (!$file->exists()) {
         $length = 0;
     } else {
         $length = $file->getSize();
     }
     $response = new Response(null, $length == $range['total'] ? 201 : 308);
     if ($length < 1) {
         $length = 1;
     }
     $response->headers->set('Range', '0-' . ($length - 1));
     $result = new UploadResult();
     $result->setResponse($response);
     return $result;
 }
Example #17
0
 public function getLastModified()
 {
     return new DateTime('@' . $this->file->getMtime());
 }
Example #18
0
 private function parseFile(File $file)
 {
     $parser = new Parser(new Lexer());
     return $parser->parse($file->getContent());
 }
Example #19
0
 /**
  * {@inheritdoc}
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
 {
     $image = $this->adapter->load($in->getContent());
     $size = $image->getSize();
     $originalRatio = $size->getWidth() / $size->getHeight();
     if ($settings['width'] === null && $settings['height'] === null) {
         $settings['width'] = $size->getWidth();
         $settings['height'] = $size->getHeight();
     }
     if (!$settings['width']) {
         $settings['width'] = $size->getWidth();
     }
     if (!$settings['height']) {
         $settings['height'] = $settings['width'] * $originalRatio;
     }
     // Scale Image up if too small
     if ($size->getHeight() > $size->getWidth()) {
         if ($settings['height'] > $size->getHeight()) {
             $image->resize(new Box($settings['height'] * $originalRatio, $settings['height']));
         }
     } else {
         if ($settings['width'] > $size->getWidth()) {
             $image->resize(new Box($settings['width'], $settings['width'] / $originalRatio));
         }
     }
     // Re calculate size and aspect ratio
     $size = $image->getSize();
     $originalRatio = $size->getWidth() / $size->getHeight();
     // Resize and on center
     if ($size->getHeight() > $size->getWidth()) {
         $thRatio = $settings['width'] / $settings['height'];
         $higher = $size->getHeight();
         $lower = $size->getWidth();
         $newWidth = $thRatio * $higher;
         $newHeight = $lower / $thRatio;
         if ($newWidth < $settings['width']) {
             $newWidth = $settings['width'];
         }
         if ($newHeight < $settings['height']) {
             $newHeight = $settings['height'];
         }
         $crop = ($higher - $newHeight) / 2;
         if ($crop > 0) {
             $point = new Point(0, $crop);
             $image->crop($point, new Box($lower, $newHeight));
         } else {
             $cropHigher = -($crop * $thRatio);
             $point = new Point($cropHigher, 0);
             $image->crop($point, new Box($newWidth, $higher));
         }
     } else {
         $thRatio = $settings['width'] / $settings['height'];
         $higher = $size->getWidth();
         $lower = $size->getHeight();
         $newWidth = $thRatio * $lower;
         $newHeight = $higher / $thRatio;
         if ($newWidth < $settings['width']) {
             $newWidth = $settings['width'];
         }
         if ($newHeight < $settings['height']) {
             $newHeight = $settings['height'];
         }
         $crop = ($higher - $newWidth) / 2;
         if ($crop > 0) {
             $point = new Point($crop, 0);
             $image->crop($point, new Box($newWidth, $lower));
         } else {
             $cropLower = -($crop / $thRatio);
             $point = new Point(0, $cropLower);
             $image->crop($point, new Box($higher, $newHeight));
         }
     }
     // Generate thumbnail
     $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->mode)->get($format, array('quality' => $settings['quality']));
     $out->setContent($content);
 }
Example #20
0
 /**
  * {@inheritDoc}
  */
 public function createFile($key, Filesystem $filesystem)
 {
     $file = new File($key, $filesystem);
     if (!array_key_exists($key, $this->fileData)) {
         $directory = dirname($key) == '.' ? '' : dirname($key);
         $this->listDirectory($directory);
     }
     $fileData = $this->fileData[$key];
     $file->setName($fileData['name']);
     $file->setSize($fileData['size']);
     return $file;
 }
Example #21
0
 /**
  * Save or update a file
  *
  * @param Number $id   ID of file
  * @param String $data content to save
  *
  * @return Gaufrette\File $file the saved file
  *
  * @throws BadRequestHttpException
  */
 private function saveFile($id, $data)
 {
     if (is_resource($data)) {
         throw new BadRequestHttpException('/file does not support storing resources');
     }
     $file = new File($id, $this->gaufrette);
     $file->setContent($data);
     return $file;
 }
Example #22
0
 function saveFileContent($object, $content, $name = null, $type_code = null, $content_type = null)
 {
     $filename = !empty($name) ? $name : str_replace(".", "", microtime()) . getFileExtension($content_type);
     $filesystemPath = $object->getDbTableName() . '/' . date('Y/m/d') . '/' . $object->id . '/';
     $filesystem = $this->getFilesystem($filesystemPath);
     $file = new File($filename, $filesystem);
     $file->setContent($content);
     $att = new Attachment($this->w);
     $att->filename = $filename;
     $att->fullpath = str_replace(FILE_ROOT, "", $this->getFilePath($filesystemPath) . "/" . $att->filename);
     $att->parent_table = $object->getDbTableName();
     $att->parent_id = $object->id;
     $att->title = $filename;
     $att->type_code = $type_code;
     $att->mimetype = $content_type;
     //                $att->modifier_user_id = $this->w->Auth->user()->id;
     $att->insert();
     return $att->id;
 }