tiloweb / base64-bundle
/
1.2.4
2020-06-19 10:11 UTC
Requires
- php: >=5.3.3
- symfony/framework-bundle: >=2.3
README
此Bundle允许您管理通过表单发送的base64编码的实体文件。
安装
步骤1:下载Bundle
打开命令行,进入您的项目目录,并执行以下命令以下载此Bundle的最新稳定版本
$ composer require tiloweb/base64-bundle "dev-master"
此命令需要您已全局安装Composer,如Composer文档中的安装章节所述。
步骤2:启用Bundle(仅限Symfony 2或3)
然后,通过将其添加到项目中的app/AppKernel.php
文件中已注册的Bundle列表中,来启用此Bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Tiloweb\Base64Bundle\TilowebBase64Bundle(), ); // ... } // ... }
步骤3:添加默认的twig主题
将默认的twig主题添加到您的app/config/config.yml
中的表单,对于Symfony 2和3,或app/config/packages/twig.yml
中的Symfony 4。
# app/config/config.yml twig: form_themes: - '@TilowebBase64/form/fields.html.twig'
步骤4:配置您的表单类型
<?php // src/AppBundle/Form/ImageType.php use App\Entity; use Tiloweb\Base64Bundle\Form\Base64Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class ImageType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add("avatar", Base64Type::class, array( 'label' => 'Avatar', 'required' => false, // Can be true 'deleteLabel' => 'Delete', // Optional if "required" is true, default : "Delete" 'width' => 600, // Optionnal : If set, the image will be resized 'height' => 400 // Optionnal : If not setted, the ratio will be respected, if setted with width, the image will be cropped. )) ; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => Avatar::class, )); } }
步骤5:享受!
现在,您的表单将显示一个文件输入框。当您选择图片时,预览将更新。如果字段不是必填项,将显示“删除”按钮。当表单提交时,您将收到图片的base64 URL。