Sonata Admin图片上传问题

时间:2016-12-21 12:16:30

标签: sonata-admin sonata

我尝试将图片上传到实体时遇到问题。

ENTITY

<?php

namespace app\PibeBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class PibeAdmin extends Admin
{
/**
 * @param DatagridMapper $datagridMapper
 */
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        //->add('id')
        ->add('titulo')
        //->add('texto')
        ->add('activo')
        ->add('ranking')
    ;
}

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{   
    $listMapper
        ->add('id')
        ->add('titulo',null,array('required'=>false))
        //->add('imagenEs')
        ->add('imagen',null,array(
            'template'=>"PibeBundle:Default:imagen.html.twig"
        ))
        ->add('activo',null,array('editable'=>true))
        ->add('ranking')
        ->add('_action', 'actions', array(
            'actions' => array(
                'show' => array(),
                'edit' => array(),
                'delete' => array(),
            )
        ))
    ;
}

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $idioma=$this->getRequest()->getLocale();

    $formMapper
    ->add('imagen', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Imagen'

                     ), array(                       
                    'link_parameters' => array(
                    'context' => 'pibe',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )

    ;


    if($idioma=='es'){
            $formMapper         

                ->add('logoEs', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Logo Español'

                     ), array(                       
                    'link_parameters' => array(
                    'context' => 'pibe',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )
                ->add('logoEn', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Logo Ingles'

                     ), array(                       
                    'link_parameters' => array(
                    'context' => 'pibe',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )

              ->add('titulo','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Titulo Español','required'=>false))
              ->add('tituloEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Titulo Ingles','data'=>'','required'=>false))
              ->add('script','text',array('label'=>'Script Video Español','required'=>false))
              ->add('scriptEn','text',array('label'=>'Script Video Ingles','data'=>'','required'=>false))
              ->add('texto','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Español','required'=>false))
              ->add('textoEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Ingles','data'=>'','required'=>false))

                ;
        }else{
            $formMapper 

                ->add('tituloEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Titulo Ingles','data'=>'','required'=>false))
                ->add('titulo','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Titulo Español','required'=>false))
                ->add('scriptEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Script Video Ingles','data'=>'','required'=>false))
                 ->add('script','text',array('label'=>'Script Video Español','required'=>false))
                 ->add('textoEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Ingles','data'=>'','required'=>false))
                ->add('texto','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Español','required'=>false))




                ;
    }
    $formMapper
        ->add('activo')
        ->add('ranking')
    ;



    $container = $this->getConfigurationPool()->getContainer();
    $em = $container->get('doctrine.orm.entity_manager');

    if($idioma=='es' && $this->getSubject()->getId()!=null){    


        //$titoriginal=$em->getRepository('HomeBundle:ImagenesHome')->find($this->getSubject()->getId());          
        // $titoriginal->setTranslatableLocale('en');
        //$em->refresh($titoriginal);


       //buscamos su traduccion en la tabal de traducciones.
       $repository = $em->getRepository('Gedmo\Translatable\Entity\Translation');
       $translations = $repository->findTranslations($this->getSubject()); 
       //rellenamos el campo con la traduccion.
       if(count($translations)>0){

          if(array_key_exists("titulo",$translations['en'])){
            $formMapper->get('tituloEn')->setData($translations['en']['titulo']);
          }     
         if(array_key_exists("texto",$translations['en'])){
           $formMapper->get('textoEn')->setData($translations['en']['texto']);
          } 
         if(array_key_exists("script",$translations['en'])){
           $formMapper->get('scriptEn')->setData(trim($translations['en']['script']));
          }               
       };


    }else if($idioma=='en'  && $this->getSubject()->getId()!=null){

       $titoriginal=$em->getRepository('PibeBundle:Pibe')->find($this->getSubject()->getId());
       //cambiamos el locale a 'es' para coger el texto en español
       $titoriginal->setTranslatableLocale('en');
       $em->refresh($titoriginal);
       //una vez que lo cogemos, volvemos a cambiar el locale a 'en'
       $formMapper->get('tituloEn')->setData($titoriginal->getTitulo());
       $formMapper->get('textoEn')->setData($titoriginal->getTexto());
       $formMapper->get('scriptEn')->setData(trim($titoriginal->getScript()));

       $titoriginal->setTranslatableLocale('es');
       $em->refresh($titoriginal);

    }





}

/**
 * @param ShowMapper $showMapper
 */
protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('id')
        ->add('titulo')
        ->add('texto')
        ->add('activo')
        ->add('ranking')
    ;
}


public function prePersist($object) {


        $container = $this->getConfigurationPool()->getContainer();
        $entityManager = $container->get('doctrine.orm.entity_manager');


        //insertar en español.
        $object->setTitulo($object->getTitulo());
        $object->setTexto($object->getTexto());
        $object->setscript($object->getscript());
        $object->setTranslatableLocale('es');
        $entityManager->persist($object);
        $entityManager->flush();

        //insertar en ingles.
        $object->setTitulo($object->getTituloEn());
        $object->setTexto($object->getTextoEn());
        //$object->setscript($object->getscriptEn());
        if(!$object->getscriptEn()){
            $object->setscript(' ');
        }
        else{
            $object->setscript($object->getscriptEn());
        }
        $object->setTranslatableLocale('en');
        $entityManager->persist($object);
        $entityManager->flush();


}

public function preUpdate($object) {


        $container = $this->getConfigurationPool()->getContainer();
        $entityManager = $container->get('doctrine.orm.entity_manager');


        //insertar en español.
        $object->setTitulo($object->getTitulo());
        $object->setTexto($object->getTexto());
        $object->setscript($object->getscript());
        $object->setTranslatableLocale('es');
        $entityManager->persist($object);
        $entityManager->flush();

        //insertar en ingles.
        $object->setTitulo($object->getTituloEn());
        $object->setTexto($object->getTextoEn());
        //$object->setscript($object->getscriptEn());
        if(!$object->getscriptEn()){
            $object->setscript(' ');
        }
        else{
            $object->setscript($object->getscriptEn());
        }
        $object->setTranslatableLocale('en');
        $entityManager->persist($object);
        $entityManager->flush();

 }


}

管理

    private async Task<SoftwareBitmap> ResizeSoftwareBitmap(SoftwareBitmap softwareBitmap, double scaleFactor)
    {
        var resourceCreator = CanvasDevice.GetSharedDevice();
        var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap);
        var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor), 96);

        using (var cds = canvasRenderTarget.CreateDrawingSession())
        {
            cds.DrawImage(canvasBitmap, canvasRenderTarget.Bounds);
        }

        var pixelBytes = canvasRenderTarget.GetPixelBytes();

        var writeableBitmap = new WriteableBitmap((int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor));
        using (var stream = writeableBitmap.PixelBuffer.AsStream())
        {
            await stream.WriteAsync(pixelBytes, 0, pixelBytes.Length);
        }

        var scaledSoftwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor));
        scaledSoftwareBitmap.CopyFromBuffer(writeableBitmap.PixelBuffer);

        return scaledSoftwareBitmap;
    }

如果我上传1500x1000的图片,那么它的作品非常完美: enter image description here enter image description here

但是如果图像是1920x1080则不起作用: enter image description here 当我点击crear时它没有添加图像: enter image description here

php从服务器返回错误500,但图像在上传/媒体中。我没有设置尺寸(高度/宽度)限制

1 个答案:

答案 0 :(得分:0)

问题os这个故障是php扩展 php_fileinfo ,如果它被禁用我得到错误,启用它可以按预期工作