0

I'm working on a Laravel 4 application and I wrote a method called show() inside my controller that gets the id from the route and makes an array of some elements from the database, these elements will be returned with an image. I can return the image without the text using Response::make() and a header, but if I bind it with a data it can't read the image.

I tried to do something like :

public function show($id){

    $onlineAd = OnlineAds::where('id', $id)->get(array('title', 'short_description', 'full_description'));

    $fileName = OnlineAds::find($id)->image;
    $path = 'app/storage/uploads/mobile/' . $fileName;

    return Response::json([
        "data" => $onlineAd,

        "Image"=> base64_encode(File::get($path))
    ]);
}

This returns the data perfectly, but the image returns as an unreadable text instead. Any suggestions?

  • can you post the relevant bit of code that displays or handles the displaying of the image? – Alex Andrei Sep 30 '15 at 08:05
  • @AlexAndrei it is above, here it is again return Response::json([ "data" => $onlineAd, "Image"=> base64_encode(File::get($path)) ]); – Saif Harbia Sep 30 '15 at 08:11

2 Answers2

0

I assume you don't do this already in the view, this is what I meant by the "code that handles the displaying of the image"

Before inserting it in the src attribute, you should format the image payload properly.
Change this bit of code like so:

return Response::json([
    "data" => $onlineAd,

    "Image"=> 'data:image/png;base64,' . base64_encode(File::get($path))
]);

to include the data and the mime type and the base64 after the comma.

Modify the mime type accordingly, image/png, image/jpg, etc.

Also, see this for reference How to display Base64 images in HTML?

Community
  • 1
  • 1
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • that didn't change the output, even a bit. it still giving me unreadable text instead of an image – Saif Harbia Sep 30 '15 at 08:21
  • check the `base64` string to see if it's valid or not, then check the answers on the reference question I posted. If you could edit your post with the part from the view, that would be great too :) – Alex Andrei Sep 30 '15 at 08:25
  • another thing you could do is do a view source on the document and post what is actually displayed – Alex Andrei Sep 30 '15 at 08:27
  • and by view you mean the output I'm receiving when running the code? or you mean the view file ? – Saif Harbia Sep 30 '15 at 08:28
  • I mean both. The relevant part of the view file, where you display the image *and* the output you get. – Alex Andrei Sep 30 '15 at 08:29
  • I tried to use a bit of an html code to display the image but it returns a blank page, the code looks like this echo ('';). I don't have a view as I'm working as a backend developer, that's why I don't use a view to output the data. For the output, it gives large number of texts, so I can't post it all here. It starts with------- ({"data":[{"title":"title","short_description":"this is short","full_description":"this is full "}],"Image":"data:image\/file;base64,\ – Saif Harbia Sep 30 '15 at 08:34
  • you can use pastebin or any other alternative to post the entire output. One thing that comes up is the `mime type` of `image/file` try to use `image/jpg` or whatever type of image you have – Alex Andrei Sep 30 '15 at 08:37
  • also your `echo` statement looks wrong. You have some commas there that shouldn't be there. Modify it like this: `echo '';` and please post also how `$src` gets populated. – Alex Andrei Sep 30 '15 at 08:41
  • echo ''; gives a blank page – Saif Harbia Sep 30 '15 at 08:50
  • I provided an answer below, please look it up – Saif Harbia Sep 30 '15 at 09:05
0

I successfully made the desired output, but still not sure if this is the best way to display a text alongside an image,

here is my code :

    $fileName = OnlineAds::find($id)->image;
    $path = 'app/storage/uploads/mobile/' . $fileName;
    $data=base64_encode(file_get_contents($path));
    $src= 'data: '.mime_content_type($path).';base64,'.$data;


    echo '<img src="' . $src . '" />';

    return Response::json([
        "data" => $onlineAd,

    ]);

So, I echoed the image and returned the text, if you have a better solution that returns both of them without the echo statement then don't hesitate to provide it.