Search code examples
phpsymfonyeasyadmin

Removing the generated hash when uploading an image with easy_admin / @Vich\Uploadable


My problem is simple but complicated at the same time.

Basically when you upload a image with easy_admin. The image get's a hash.

Like so:

/uploads/images/5f17449f4932f_image004.jpg

Is there any way to remove the generated hash before the image name ?

Here is my Entity:

use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

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

/**
 * ImageFile
 *
 * @Vich\UploadableField(mapping="images", fileNameProperty="image")
 * @var File
 */
private $imageFile;

Is there a setting that I may use in the easy_admin.yml config ?

 form:
    fields:
      - { property: 'imageFile', label: 'Image', type: 'vich_image'}

Let me know if any other information is needed. Thank you.

UPDATE: The class


namespace App\Service\FileNamer;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;

class FileNamer implements NamerInterface
{
   public function name($object, PropertyMapping $mapping): string
   {
       /* @var $file UploadedFile */
       $file = $mapping->getFile($object);

       return  $file->getClientOriginalName();
   }
}

Easy_admin

    db_driver: orm
    mappings:
        images:
            uri_prefix:         '%upload_images_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_images_folder%'
            namer:
                service: App\Service\FileNamer
        videos:
            uri_prefix:         '%upload_videos_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_videos_folder%'
            namer:
                service: vich_uploader.namer_origname
        pdfs:
            uri_prefix:         '%upload_pdfs_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_pdfs_folder%'
            namer:
                service: vich_uploader.namer_origname

Solution

  • Create your own custom namer class. Just implement Vich\UploaderBundle\Naming\NamerInterface and add it to vich_uploader configuration. https://github.com/dustin10/VichUploaderBundle/blob/master/docs/file_namer/howto/create_a_custom_file_namer.md