1

I have a image decode by base64_decode. I have a entity Image. This is entity consist : id and path to the file. File of image load to server by this guide http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html.

How encode this string and upload file to server and upload path to database in controller.

my controller

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));
}
Vlad
  • 11
  • 1
  • 3
  • First of all, i think that i can find image and put decoded image, but i can't save image, because i saved path to the image. Then i think, that maybe i must create file, but i didn't know ow to create file in controller. – Vlad Jul 03 '14 at 00:27
  • this might help you http://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file – Tom Tom Jul 03 '14 at 08:56

1 Answers1

0

it should help DataUriNormalizer https://symfony.com/blog/new-in-symfony-3-1-data-uri-normalizer## Heading ##

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

and this 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
Naskalin
  • 895
  • 9
  • 9