0

I'm currently saving images that the user uploads to storage/dokumentarkiv/{{$building->id}}/byggbilder and using Intervention to also save a thumbnail of the image. Then I want to display all the images in a grid in my view. The code below is working, but I was wondering if there is a cleaner way to display the image as encoding the image to data-url gives me the base64 code for each image in the html.

My controller:

public function storeImage(Request $request, $id)
{
    $building = Building::find($id);

    $file = $request->file('imageInput');

    $fileName = $request->imageName;
    $fileOriginalName = $request->file('imageInput')->getClientOriginalName();
    $fileExtension = $request->file('imageInput')->getClientOriginalExtension();
    $fileSize = $request->file('imageInput')->getSize();
    $mimeType = $request->file('imageInput')->getMimeType();
    $path = $building->id.'/byggbilder'.'/';
    $thumbnailPath = $building->id.'/byggbilder'.'/thumbnails'.'/';
    $thumbnailOriginalName = $request->file('imageInput')->getClientOriginalName();

    $image = new Image;

    $image->name = $fileName;
    $image->original_name = $fileOriginalName;
    $image->path = $path;
    $image->thumbnail_path = $thumbnailPath;
    $image->thumbnail_original_name = $thumbnailOriginalName;
    $image->file_extension = $fileExtension;
    $image->file_size = $fileSize;
    $image->mime_type = $mimeType;

    $building->buildingImages()->save($image);
    $dateTime = $image->created_at->format('d-m-Y-H-i-s');

    $mImage = Img::make($request->file('imageInput')->getRealPath());

    $mImage->save(storage_path('dokumentarkiv/').$path.'('.$dateTime.')'.$fileOriginalName)
           ->resize(300, null, function ($constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
           })
           ->save(storage_path('dokumentarkiv/').$thumbnailPath.'('.$dateTime.')'.$fileOriginalName);

    return redirect()->back();
}

The function that retrieves the image and passes it to the view:

public function administration(Request $request, $id)
{
    $moderator = Auth::user();

    $building = Building::find($id);
    $buildingID = $building->id;

    // Find the buildings users
    $buildingsUsers = $building->users()->get();

    // Find the buildings images
    $images = $building->buildingImages()->get();

    foreach ($images as $image) {
      $test = (string) Img::make(storage_path('dokumentarkiv/25/byggbilder/(30-01-2017-17-20-46)logo-test.png'))->encode('data-url');
    }

    return view('users.buildingAdministration', compact('moderator', 'building', 'buildingID', 'buildingsUsers', 'images', 'test'));
}

I just hard coded the url to test if it works. I'll change this to be dynamic when I find a good solution.

My view:

<img src="{{$test}}" alt="">

I also tried to just use this in my view:

@php
 $img = Image::make(file_get_contents(storage_path('dokumentarkiv/25/byggbilder/(30-01-2017-17-20-46)logo-test.png')));
 echo $img->response();
@endphp

But that just displays on the page as:

HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Length: 9957
Content-Type: image/png �PNG  IHDR*J�� pHYs���+
IDATx��{�e���[Փ�1�D6و9!�����D@Z\
Dȸ��"˲D�;�9{��(���Q�f��
f��f�6�6�,ƨ1f#Lf��w�x޺tu�tu��n�>��t׽���W���<
��ֲ;�f� ��X�PA�:��
�{c���S��u�{w.>��]�J�(�W`Ɖj��х�;�a`��'�i�Yh�KK 
]�e^E�UDb����^U�۵�?~��n��/S�D��3FTkV�\ �
:xDB�y���7P�,�4%DUI-s

Appreciate all inputs!

GKMelbo
  • 41
  • 5
  • as far as I know you can't get the file from storage folder without symlink...someone correct me if I am wrong – lewis4u Feb 12 '17 at 12:57
  • @lewis4u All the solutions I have found uses synlinks, but I can't use symlinks because the images shouldn't be accessible to everyone. That's why I created the image folder in Storage instead of Public. – GKMelbo Feb 12 '17 at 13:19
  • Well then you must use a middleware....you can't make images available for some users and for others not...you must use middleware for that and say if this user has right to see it then he can see it, and if he doesn't have right then he can't – lewis4u Feb 12 '17 at 13:22
  • maybe http://stackoverflow.com/questions/1207190/embedding-base64-images – SpX Feb 12 '17 at 13:25
  • @lewis4u I just find it odd that retrieving documents from Storage is easy, but retrieving an image is so difficult. So I must be doing something wrong. But if I create a symlink from the Storage folder to Public, will people be able to see the folder in the browsers developer tools? That's the concern I have about creating a symlink. – GKMelbo Feb 12 '17 at 14:41
  • How are you retrieving a document from storage folder??? – lewis4u Feb 12 '17 at 14:43
  • I'm looking up the document id in the db table, get the path and then made a function in my controller that downloads the document when a user clicks on it. – GKMelbo Feb 12 '17 at 14:46
  • And what differs documents from images? Why doesn't the same process work for images? – Marco Aurélio Deleu Feb 12 '17 at 18:31
  • @MarcoAurélioDeleu With documents I simply download the file, not showing it in the browser. I made a download route for my images as well and just referenced the download url as the image src. That works, but then I'm not able to lazyload as all the images are hitting the download url at once. – GKMelbo Feb 16 '17 at 17:52

1 Answers1

0

You can create a method in controller like

MediaController@getImage($path){
    $img = Image::make($path);
    return $img->response();
}

Then later you can assign this method a middleware.

Kudos, keep working