PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

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

 January 23, 2022     easyadmin, php, symfony     No comments   

Issue

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



Answered By - Leprechaun
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing