Answer the question
In order to leave comments, you need to log in
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('...
}
/**
* @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;
Answer the question
In order to leave comments, you need to log in
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') );
}
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 questionAsk a Question
731 491 924 answers to any question