1

I'm creating a website where users can uploads some images, URL images are saved into json and inserted into database so when I select all of the rows in the table it gives me something this.

{
"sroxrafm72pgipmpa4nz":
 "http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610510\/sroxrafm72pgipmpa4nz.jpg"
},
{
"egrrhg4umqgyjoltxrky":
"http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610511\/egrrhg4umqgyjoltxrky.jpg"
},
{
"rfurg40o5af2h6s55thb":
"http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610511\/rfurg40o5af2h6s55thb.jpg"
}

I need to decode it with twig to get the url of each id to display the images, but I just can't figure out to do this. I hope you guys can help me.

DaveSanchez
  • 329
  • 1
  • 3
  • 10
  • You should probably decode it before passing it to twig, or you could use JavaScript and do it on the client side. – user2182349 Oct 12 '15 at 01:56

1 Answers1

0

I'd say this is a clone of: Decoding JSON in Twig

What might be easier is to base64 encode the images and store the string inside of the database. I use the following:

In HTML:

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="image" />
</form>

upload.php:

function getImageBase64($image_path) {
    $image = imagecreatefromfile($image_path);
    ob_start();
    imagepng($image);
    $contents = ob_get_contents();
    ob_end_clean();
    $image_string = 'data:image/png;base64,' . base64_encode($contents);
    imagedestroy($image);

    return $image_string;
}

if (getimagesize($_FILES["image"]["tmp_name"]) !== false) {
    $image = getImageBase64($_FILES["image"]["tmp_name"]);

    //Add to database...
}
Community
  • 1
  • 1
Andrew Mast
  • 311
  • 1
  • 17