【问题标题】:how to encode image decoded by base64_decode, symfony如何对 base64_decode、symfony 解码的图像进行编码
【发布时间】:2014-07-03 00:22:23
【问题描述】:

我有一个 base64_decode 的图像解码。 我有一个实体图像。这是实体组成:文件的 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));
}

【问题讨论】:

  • 首先,我认为我可以找到图像并放入解码后的图像,但我无法保存图像,因为我保存了图像的路径。然后我想,也许我必须创建文件,但我不知道如何在控制器中创建文件。

标签: image symfony base64


【解决方案1】:

它应该有助于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

【讨论】:

    猜你喜欢
    • 2011-08-29
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2012-10-26
    相关资源
    最近更新 更多