如何编码由base64_decode,symfony解码的图像

时间:2014-07-03 00:22:23

标签: image symfony base64

我通过base64_decode进行图像解码。 我有一个实体Image。这是实体编组:文件的id和路径。本指南http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html将图像文件加载到服务器。

如何编码此字符串并将文件上传到服务器并将路径上传到控制器中的数据库。

我的控制器

public function updateattachmentAction()
{
    $em = $this->getDoctrine()->getManager();
    $photo = $em->getRepository('MyPluginBundle:Photo')->findOneById(4);

    $str="";
    $request = $this->container->get('request');
    $image = $request->query->get('image');

//        file_put_contents($photo, base64_decode($data));

//        $photo->upload();
//        $em->persist($photo);
//        $em->flush();
    $response = array("code" => 100,"success" => true);
    //you can return result as JSON
    return new Response(json_encode($response));
}

1 个答案:

答案 0 :(得分:0)

它应该帮助DataUriNormalizer https://symfony.com/blog/new-in-symfony-3-1-data-uri-normalizer##标题##

use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;

$normalizer = new DataUriNormalizer();
$avatar = $normalizer->denormalize('data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=', 'SplFileObject');
// $avatar is a SplFileObject with the GIF image contents

和这个https://github.com/hshn/base64-encoded-file

use Hshn\Base64EncodedFile\HttpFoundation\File\Base64EncodedFile;

$file = new Base64EncodedFile(base64_encode($data));

$file->getPathname(); // "/path/to/file"
$file instanceof Symfony\Component\HttpFoundation\File\File; // true