2

I want to return image with data.

$ads = DB::table('ads')->whereRaw('category_id=' .$id)->orderBy('id', 'desc')->get();

$filename = public_path().'/uploads/images/'.$ads->ad_image;
$filename = (file_get_contents($filename));
$image = Array('image'=>json_encode(base64_encode($filename)));
$ads[0]->image = $image;

return $ads; 
3
  • why not store image on server and return its path? Commented Aug 19, 2016 at 11:25
  • do you want to transmit whole image via json ? Or I'm not understanding your question.. Commented Aug 19, 2016 at 14:08
  • Yes i am transmit the whole image in JSON Commented Aug 20, 2016 at 4:11

2 Answers 2

10

Assuming you want to show the images to the user on the page stored in laravel's storage directory, You can do the following implementation:

Create a controller to fetch and return the image as a response.

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;
Use DB;


class PhotoController extends Controller
{
    public function image($fileName){
        $path = public_path().'/uploads/images/'.$fileName;
        return Response::download($path);        
    }
}

Add the Route to handle the image request

Route::get('image/{filename}',PhotoController @image);

Now, to access the image , lets say in image tag

<img src="image/abc.jpg">

PS: This method of serving images will have little overhead with respect to direct link of image...
In case, your images are public, you can simply create symbolic link of the directory where you have stored the images into the public directory.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I am storing in public folder.
If privacy of the images doesn't mater use the symbolic links
Can you please share symbolic link of the directory I am storing the images into the public directory. It will access in Andriod and IOS Aps also.
@Anwar in linux you can create the symbolic link by using this command ln -s /path/to/laravel/storage/avatars /path/to/laravel/public/avatars more info :stackoverflow.com/questions/30191330/…
0

$filename = asset('/uploads/images/'.$ads->ad_image);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.