A
A
Andrey Sokolov2015-07-09 07:28:58
symfony
Andrey Sokolov, 2015-07-09 07:28:58

How to save an image in Symfony when using 2 bundles?

Good afternoon, once again I ran into such a problem:
The developer who worked before me preferred Symfony, but as such I didn’t do anything serious on it, only on Laravel / CodeIgniter, and it’s a little difficult for me to navigate (it takes time). In general, it costs Bundle (Sonata Admin Bundle). The image is loaded via iPHPBundle (ipp filestore bundle). The following task came: to fasten CROP for cutting an image, I found a bundle (Comur Image Bundle), I managed to fasten it, it loads images, but I can’t save it, the fact is that iphp saves everything as an object under `serialize()`. But Comur saves only the file name. It is not possible to change the type of the field, because video, etc. is also loaded through iPHP. as explained, we get the time of the video, the size of the photo, etc. through it.
How to save a cropped photo via Comur?

protected function configureFormFields(FormMapper $formMapper)
        {
            $em                    = $this->getModelManager()->getEntityManager($this->getSubject());
            $searchTagsTransformer = new SearchTagsToStringTransformer($em);
            $formMapper
                ->add('title',...
                //->add('imageFile', 'iphp_file', array('required' => false, 'label' => 'Изображение'))
                ->add('imageFile', 'comur_image', array(
                    'uploadConfig' => array(
                        'uploadRoute' => 'comur_api_upload',        //optional
                        'uploadUrl' => $this->getRoot()->getSubject()->getUploadRootDir(),       // required - see explanation below (you can also put just a dir path)
                        'webDir' => $this->getRoot()->getSubject()->getUploadDir(),              // required - see explanation below (you can also put just a dir path)
                        'fileExt' => '*.jpg;*.gif;*.png;*.jpeg',    //optional
                        'libraryDir' => null,                       //optional
                        'libraryRoute' => 'comur_api_image_library', //optional
                        'showLibrary' => true,                      //optional
                        'saveOriginal' => 'originalImage'           //optional
                    ),
                    'cropConfig' => array(
                        'minWidth' => 588,
                        'minHeight' => 300,
                        'aspectRatio' => true,              //optional
                        'cropRoute' => 'comur_api_crop',    //optional
                        'forceResize' => false,             //optional
                        'thumbs' => array(                  //optional
                            array(
                                'maxWidth' => 180,
                                'maxHeight' => 400,
                                'useAsFieldImage' => true  //optional
                            )
                        )
                    ),
                    'required' => false, 'label' => 'Изображение'
                ))
                ->add('...
    }

Here, when saving, the following happens:
Comur returns an array
[imageFile] => cropped/123.jpg
[originalFile] => 123.jpg
When saving, the doctrine swears that it needs a "string" and not an array, and how correctly this "string" should be stored as "serialize" of that same iPHP.
/**
    	 * @ORM\Column(type="array", nullable=true)
    	 * @FileStore\UploadableField(mapping = "publication_preview_image")
    	 * @Assert\Image(mimeTypesMessage="Загружаемый формат изображения не поддерживается")
    	 */
    	protected $imageFile;
    
    	/**
    	 * @ORM\Column(type="array", nullable=true)
    	 * @FileStore\UploadableField(mapping = "publication_preview_image")
    	 */
    	protected $originalImage;

I hope someone can help me solve this problem :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-07-09
@eX1stenZ

In sonata, use prePersist, preUpdate and similar methods to work with an entity before/after saving to the database.
Comur needs a string, iPHP needs an array.
Decide what your main one is and "distill" the string into an array (or vice versa)

public function preUpdate($object) {
    $object->setImage( $object->getImage()['imageFile') );
}

In general, read the doc for two bundles, see what each needs and manipulate the entity just before (after) saving it to the database.

A
Alexey Pavlov, 2015-07-09
@lexxpavlov

I would store the cropped images directly with the ComurImageBundle, without iPHP. ComurImageBundle itself will save the original file (if necessary, you can turn it off) and the cropped file to the specified folder, and it will save itself via ajax, even before saving the form, and a ready-made string with a ready-made file (cropped and original) will come to the sonata.
Remove iPHP related attributes from entity fields and make them string instead of array. That is, do it exactly as described in the ComurImageBundle documentation. And if you still need to do something else with iPHP, then additionally make new fields for iPHP, and manually save the necessary data for them in prePersist, as Denis advises you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question