使用 VichUploaderBundle 下载一些图像

Download few images with VichUploaderBundle

我想用 VichUploaderBundle 上传文件。有我的实体

........
/**
 *
 * @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;
}
.........

我的表单生成器看起来像

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

我的配置文件看起来像

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

问题: 我需要更改什么才能使用此捆绑包下载一些图片?

您的表单应如下所示:

->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')

在你的前端,如果你想显示产品:

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

捆绑文档中都有详细记录:https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/index.md

我建议使用 LiipImagineBundle 为您的 frontend/backend 生成所有需要的缩略图。