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�xtu�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!