Vichuploaderbundle: Show a image in a template twig. ?!?!

Created on 29 Oct 2012  路  4Comments  路  Source: dustin10/VichUploaderBundle

I have the same problem that issue #24.

In my twig template I have:

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

The image is a field of entity User:

/**
     * @ORM\Column(type="string", length=255, name="image_name")
     */
    public $imageName;
    /**
     * @Assert\File(
     *     maxSize="200k",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="imagen_perfil", fileNameProperty="imageName")
     *
     * @var File $image
     */
    protected $image;

Somebody could help me?
Thanks!

Most helpful comment

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

This should work.

All 4 comments

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

This should work.

@loostro is correct. Pass the entity object, not the property value.

Ok, Thanks very much.
It was the error. I had not passed the entity object.
:(

I can't see the uploaded image after submitting form
i see just the name of image but the image is uploded corrctery
I'm using VichUploaderBundle for an user profile image.
****_config.yml_*****
`vich_uploader:
db_driver: orm
mappings:
user_image:
uri_prefix: /images/user
upload_destination: %kernel.root_dir%/../web/images/user

        inject_on_load:     false
        delete_on_update:   true
        delete_on_remove:   true

        `

***_show.html.twig_******

<tr> <th>image</th> <img src="{{ vich_uploader_asset(user, 'imageFile') }}" alt="{{user.imageName }}" /> </tr>

*****_usertype.php_*******

` ->add('imageFile', 'vich_image', array(
'required' => false,
'allow_delete' => true, // not mandatory, default is true
'download_link' => true, // not mandatory, default is true
));

    ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'GestionAdministrative\LgmBundle\Entity\User'
    ));
}`

*****_user.php_******
` /
*
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Assert\File(maxSize="1200k",mimeTypes={"image/png", "image/jpeg", "image/pjpeg"})
* @Vich\UploadableField(mapping="user_image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;

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

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

/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 * of 'UploadedFile' is injected into this setter to trigger the  update. If this
 * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
 * must be able to accept an instance of 'File' as the bundle will inject one here
 * during Doctrine hydration.
 *
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 *
 * @return Product
 */
public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

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

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

    return $this;
}

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

*****_userControler.php_*******
` public function newAction(Request $request)
{
$user = new User();
$form = $this->createForm('GestionAdministrative\LgmBundleForm\UserType', $user);
$form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $helper = $this->container->get('vich_uploader.templating.helper.uploader_helper');
        $path = $helper->asset($user, 'imageFile');

        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_show', array('id' => $user->getId()));
    }

    return $this->render('user/new.html.twig', array(
        'user' => $user,
        'form' => $form->createView(),
    ));
}

`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NicolaPez picture NicolaPez  路  6Comments

Propscode picture Propscode  路  4Comments

ngilain picture ngilain  路  5Comments

andrea-daru picture andrea-daru  路  3Comments

WassabiVl picture WassabiVl  路  3Comments