dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

How to use metadata with VichUploader #1440

Closed Mgt03 closed 5 months ago

Mgt03 commented 5 months ago

I need help to use Vich uploader on my project.

My problem concerns the metadata of an image imported from a php form.

I have a form where users can insert images, which are saved after submitting the form. But if they insert an image with a rotation value, after saving, the image loses this value.

I have this configuration in the vich_uploader.yml file:

vich_uploader:
    db_driver: orm

    mappings:
        marque_image:
            uri_prefix: /marques
            upload_destination: '%kernel.project_dir%/public/marques'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true
        document_file:
            uri_prefix: /documents
            upload_destination: '%kernel.project_dir%/documents'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true
        dossier_file:
            uri_prefix: /dossiers/uploads
            upload_destination: '%kernel.project_dir%/dossiers/uploads'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true
        messenger_file:
            uri_prefix: /messenger/uploads
            upload_destination: '%kernel.project_dir%/public/messenger/uploads'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true
        photos:
            uri_prefix: /dossiers/uploads
            upload_destination: '%kernel.project_dir%/public/dossiers/uploads'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true

https://github.com/dustin10/VichUploaderBundle/blob/master/docs/usage.md This documentation show the possibility to add a 'metadata' property but I can't find how to use it after.

My entity is declared as follows :

/**
 * @OA\Schema(schema="Photos")
 *
 * @Serializer\ExclusionPolicy("ALL")
 */
#[Vich\Uploadable]
#[ORM\Entity(repositoryClass: PhotoRepository::class)]
#[ORM\Table(name: 'PHOTOS')]
class Photo
{
    /**
     * @OA\Property(
     *     type="integer",
     *     description="Identifiant de la photo",
     *     example=1
     * )
     * @Serializer\Expose
     * @Serializer\Groups({"api_advertisement_index"})
     */
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(name: 'ID', type: 'integer')]
    private $id;
    /**
     * Ce champs n'est pas exposé içi mais dans App\Serializer\LinkSerializationSubscriber.
     *
     * @OA\Property(
     *     property="path_file",
     *     type="string",
     *     description="Lien de la photo",
     *     example="http://dafokaz.docker:8090/dossiers/uploads/image.png"
     * )
     * @Serializer\SerializedName(value="path_file")
     */
    #[ORM\Column(name: 'FILE', type: 'string', length: 255, nullable: true)]
    private $pathFile;
    /**
     * @var File
     */
    #[Vich\UploadableField(mapping: 'photos', fileNameProperty: 'pathFile')]
    private $file;
    ( . . . )

Concerning the metadata information of the picture before and after, I have this : Picture original :


 array(9) {
  ["FileName"]=>
  string(12) "original.jpg"
  ["FileDateTime"]=>
  int(1711527777)
  ["FileSize"]=>
  int(13668)
  ["FileType"]=>
  int(2)
  ["MimeType"]=>
  string(10) "image/jpeg"
  ["SectionsFound"]=>
  string(24) "ANY_TAG, IFD0, THUMBNAIL"
  ["COMPUTED"]=>
  array(7) {
    ["html"]=>
    string(24) "width="244" height="207""
    ["Height"]=>
    int(207)
    ["Width"]=>
    int(244)
    ["IsColor"]=>
    int(1)
    ["ByteOrderMotorola"]=>
    int(1)
    ["Thumbnail.FileType"]=>
    int(2)
    ["Thumbnail.MimeType"]=>
    string(10) "image/jpeg"
  }
  ["Orientation"]=>
  int(3)
  ["THUMBNAIL"]=>
  array(6) {
    ["Compression"]=>
    int(6)
    ["XResolution"]=>
    string(4) "96/1"
    ["YResolution"]=>
    string(4) "96/1"
    ["ResolutionUnit"]=>
    int(2)
    ["JPEGInterchangeFormat"]=>
    int(120)
    ["JPEGInterchangeFormatLength"]=>
    int(4914)
  }
} ```
Picture saved in my app folder:
```bash
array(7) {
  ["FileName"]=>
  string(26) "6603d7c701f62441951521.jpg"
  ["FileDateTime"]=>
  int(1711527879)
  ["FileSize"]=>
  int(7072)
  ["FileType"]=>
  int(2)
  ["MimeType"]=>
  string(10) "image/jpeg"
  ["SectionsFound"]=>
  string(0) ""
  ["COMPUTED"]=>
  array(4) {
    ["html"]=>
    string(24) "width="244" height="207""
    ["Height"]=>
    int(207)
    ["Width"]=>
    int(244)
    ["IsColor"]=>
    int(1)
  }
} ```

I need to have the orientation value in the saved file ...

Does anyone have any ideas?
garak commented 5 months ago

The metadata mentioned in the documentation are Doctrine metadata, not image metadata. How do you perform the image rotation?

Mgt03 commented 5 months ago

I've open my picture with the default photo app of my computer, and I have rotate the image with it and save it. This action modify the image ['Orientation'] parameter

garak commented 5 months ago

So does the image have the metadata after this operation? If you try to do a manual image upload with a simple PHP script, do you get the same metadata loss?

Mgt03 commented 5 months ago

Okay, I can describe more with an example. Firstly, I download or I take a picture bmw s1000RR At this moment, the metadatas of the picture are :

array(7) {
  ["FileName"]=>
  string(11) "classic.jpg"
  ["FileDateTime"]=>
  int(1711023627)
  ["FileSize"]=>
  int(8620)
  ["FileType"]=>
  int(2)
  ["MimeType"]=>
  string(10) "image/jpeg"
  ["SectionsFound"]=>
  string(0) ""
  ["COMPUTED"]=>
  array(4) {
    ["html"]=>
    string(24) "width="244" height="207""
    ["Height"]=>
    int(207)
    ["Width"]=>
    int(244)
    ["IsColor"]=>
    int(1)
  }
} 

I rotate it with windows and I save it bmw s1000RR - retournée At this moment, the metadatas of the picture are :

array(9) {
  ["FileName"]=>
  string(12) "original.jpg"
  ["FileDateTime"]=>
  int(1711527777)
  ["FileSize"]=>
  int(13668)
  ["FileType"]=>
  int(2)
  ["MimeType"]=>
  string(10) "image/jpeg"
  ["SectionsFound"]=>
  string(24) "ANY_TAG, IFD0, THUMBNAIL"
  ["COMPUTED"]=>
  array(7) {
    ["html"]=>
    string(24) "width="244" height="207""
    ["Height"]=>
    int(207)
    ["Width"]=>
    int(244)
    ["IsColor"]=>
    int(1)
    ["ByteOrderMotorola"]=>
    int(1)
    ["Thumbnail.FileType"]=>
    int(2)
    ["Thumbnail.MimeType"]=>
    string(10) "image/jpeg"
  }
  ["Orientation"]=>
  int(3)
  ["THUMBNAIL"]=>
  array(6) {
    ["Compression"]=>
    int(6)
    ["XResolution"]=>
    string(4) "96/1"
    ["YResolution"]=>
    string(4) "96/1"
    ["ResolutionUnit"]=>
    int(2)
    ["JPEGInterchangeFormat"]=>
    int(120)
    ["JPEGInterchangeFormatLength"]=>
    int(4914)
  }
}

I upload it in my app with Vich Uploader and the result is : 660412264d5d7087015010 At this moment, the metadatas of the picture are :

array(7) {
  ["FileName"]=>
  string(26) "660412264d5d7087015010.jpg"
  ["FileDateTime"]=>
  int(1711542822)
  ["FileSize"]=>
  int(7072)
  ["FileType"]=>
  int(2)
  ["MimeType"]=>
  string(10) "image/jpeg"
  ["SectionsFound"]=>
  string(0) ""
  ["COMPUTED"]=>
  array(4) {
    ["html"]=>
    string(24) "width="244" height="207""
    ["Height"]=>
    int(207)
    ["Width"]=>
    int(244)
    ["IsColor"]=>
    int(1)
  }
}

So, the file is not realy impacted by this modification, but the metadatas change. It seems that VichUploader deletes these metadatas, which has the effect of removing this defined orientation because the image is not originally upside down.

garak commented 5 months ago

I'm not aware of any metadata handling here. I invite you again to try uploading a file without this bundle and checking the outcome.

Mgt03 commented 5 months ago

I found the origin of my problem, it's on our side, sorry for the inconvenience.

bresam commented 4 months ago

@Mgt03 any hint what it was on your side? Possibly it can help me too.

Mgt03 commented 4 months ago

Hi @bresam, I don't think this is really helpful for you, but this problem was due to an optimization function in my image, where I explicitly remove all metadata from the image I loaded. So no link to the Vich Bundle After deleting this part of my code, it works perfectly.

bresam commented 4 months ago

@Mgt03 Thank you anyway. Found the issue in a Post Upload Event Listener where we did something similar 👍