Download few images with VichUploaderBundle

2.2k views Asked by At

I want to upload files with VichUploaderBundle. There is my Entity

........
/**
 *
 * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
 *
 * @var File $imageFile
 */
protected $imageFile;

/**
 * @ORM\Column(type="string", length=255, name="image_name")
 *
 * @var string $imageName
 */
protected $imageName;

/**
 * @ORM\Column(type="datetime")
 *
 * @var \DateTime $updatedAt
 */
protected $updatedAt;

/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 */
public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        $this->updatedAt = new \DateTime('now');
    }
}

/**
 * @return File
 */
public function getImageFile()
{
    return $this->imageFile;
}

/**
 * @param string $imageName
 */
public function setImageName($imageName)
{
    $this->imageName = $imageName;
}

/**
 * @return string
 */
public function getImageName()
{
    return $this->imageName;
}
.........

My form builder looks like

  ........
  $builder
        ->add('brand')
        ->add('model')
        ->add('price')
        ->add('imageName')
        ->add('updatedAt')
        ->add('sub_rel')
 .......

My config file looks like

vich_uploader:
db_driver: orm
mappings:
    product_image:
        uri_prefix:         /images/goods
        upload_destination: %kernel.root_dir%/../web/images/goods

        inject_on_load:     false
        delete_on_update:   true
        delete_on_remove:   true

Question: What I need to change in order that I could use this bundle to download a few pictures?

1

There are 1 answers

0
gvf On BEST ANSWER

Your form should look like:

->add('brand')
->add('model')
->add('price')
->add('image', 'vich_image', array(
    'required'      => false,
    'allow_delete'  => true, // not mandatory, default is true
    'download_link' => true, // not mandatory, default is true
));
->add('updatedAt')
->add('sub_rel')

And the in your frontend, if you want to display products:

<img src="{{ vich_uploader_asset(product, 'image') }}" alt="{{ product.name }}" />

It's all well documented in the bundles documentation: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/index.md

I would suggest using LiipImagineBundle to generate all the needed thumbnails for your frontend/backend.