setContent() public method

Sets the content.
public setContent ( string $content, array $metadata = [] ) : integer
$content string
$metadata array optional metadata which should be send when write
return integer The number of bytes that were written into the file, or FALSE on failure
Ejemplo n.º 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));
 }
Ejemplo n.º 4
0
 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());
 }
Ejemplo n.º 5
0
 /**
  * @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);
 }
Ejemplo n.º 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()));
 }
Ejemplo n.º 7
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;
 }
 /**
  * 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());
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function put($data)
 {
     $this->file->setContent($data);
 }
Ejemplo n.º 11
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;
 }