Simple qrcode to generate in DOMPDF - laravel

Hi i cant generate simpleqrcode in dompdf
Here is my blade.php
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) !!}">
in my controller
public function printpdf($isbn)
{
$data = [
'isbn' => $isbn
];
$pdf = PDF::loadView('main.inventory.view_pdf ', $data);
return $pdf->stream($isbn.'.pdf');
}
i tried this image and successfully render
<img src="{{ public_path('/uploads/image/1578635297.jpg')}}" style="width:30%;height:50%;">
Dont know why i cant generate qrcode to dompdf but when i generate in other blade its working but not in dompdf

I got mine working by initializing the base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) in a variable within a controller, and passing it to the view.
Controller:
public function foobar() {
$qrcode = base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string'));
$pdf = PDF::loadView('main.inventory.view_pdf', compact('qrcode'));
return $pdf->stream();
}
View:
<img src="data:image/png;base64, {!! $qrcode !!}">
Used laravel-snappy instead of laravel-dompdf. https://github.com/barryvdh/laravel-snappy
If you're still unable to generate a pdf file, then temporarily change the read-write permissions of the folder with chmod -R 777 <folder_path>.

Related

Laravel does not display uploaded image

I'll need your help there, I have a form when I submit an image, it's working fine, but when I try to display the image on an another page, its display a white square (I can see that it's working because I can see the name of the image in the console).
This is my app.blade.php :
<div class="menu_connect_big_screen">
<img src="{{Auth::user()->image}}"/>
<p>{{Auth::user()->firstName}} {{Auth::user()->lastName}}</p>
Mon compte | Se déconnecter
</div>
And this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class ProfilePictureController extends Controller
{
public function update(Request $request)
{
$request = $request->All();
User::where('id', Auth::user()->id)
->update(
[
'image' => $request['image']]
);
return redirect()->to('/')->with(['image-changed' => 'Photo de profil modifiée !']);
}
}
I'm kinda new to laravel so any help would be thankful.
Thank you.
If the image is stored in blob you should use base64 in your image tag like so;
<img src="data:image/jpeg;base64,{{base64_encode( Auth::user()->image )}}"/>
However, this is not specific to Laravel.
For example user images stored in public/user_images directory
<div class="menu_connect_big_screen">
<img src="{{ asset('user_images/' . Auth::user()->image) }}" height="50px" width="50px">
<p>{{Auth::user()->firstName}} {{Auth::user()->lastName}}</p>
Mon compte | Se déconnecter
</div>

laravel local storage 404 error on images

I have made a youtube style website, some people may click the video to be private, I keep these videos in storage/app/vidoes/{channelname}, the public images and videos I have working not a problem but local storage I am having a problem with, any ideas?
just getting a 404 on the thumbnails
thanks
view
<img src="{{ route('home.image', ['file_path' => 'videos/' . $video->channel_name . '/thumbnails/' . $video->video_thumbnail_name]) }}" alt="" class="img-responsive">
route
Route::get('{file_path}', [
'uses' => 'HomeController#getImage',
'as' => 'home.image'
]);
controller
public function getImage($filepath)
{
$fileContents = \Storage::disk('local')->get($filepath);
$response = \Response::make($fileContents, 200);
$response->header('Content-Type', "image/jpg");
return $response;
}
Laravel 5.8 local storage working for images
view
<img class="home-video" src="{{ route('getImage', ['thumbnail' => $video->video_thumbnail_name, 'channel_name' => $video->channel_name]) }}" alt="" >
Route
Route::get('get-image/{channel_name}/{thumbnail}','HomeController#getImage')->name('getImage');
Controller
public function getImage($channel_name,$thumbnail)
{
$fileContents = \Storage::disk('local')->get("videos/$channel_name/thumbnails/$thumbnail");
return \Image::make($fileContents)->response();
}
see how the thumbnail is important for me, I was passing the thumbnail name eg photo.jpg if you are using route model binding you would want to pass the id of the image, hope this saves someone a few hours

Laravel Dropzone.js some photos cannot be saved into the folder

I am using dropzone.js to allow the users to upload multiple photos to the database. Before I set up the database for the image uploader feature, I wanted to see if the image can be saved into my desired folder. However, after successfully uploading 3 photos(since the tick logo was shown right after the image was uploaded, I assumed the image has been successfully uploaded), only 2 of them were present in the folder.
In the following codes I have listed the view(including javascript and html codes), route and controller file:
CSS ->
{{html::style('https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.css')}}
HTML ->
{!! Form::open(array('route' => 'gallery.images.upload', 'class' => 'dropzone', 'enctype' => 'multipart/form-data','files' => true, 'id' => 'addImages', 'method' => "POST")) !!}
{{ Form::hidden('gallery_id', $gallery->id) }}
{!! Form::close() !!}
Javascript ->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script>
<script type="text/javascript">
Dropzone.options.addImages = {
maxFilesize: 5,
acceptedFiles: 'image/jpeg,image/png,image/gif',
paramName: "file",
}
</script>
Route ->
Route::prefix('gallery')->group(function(){
Route::post('/images/upload', 'GalleryController#uploadGalleryImages')->name('gallery.images.upload');
});
Controller ->
public function uploadGalleryImages(Request $request)
{
if ($request->hasFile('file')) {
// Get the file from the file request
$file = $request->file('file');
// set my file name
$originalName = $file->getClientOriginalName();
$filename = uniqid() . $originalName;
$file->move('galleries/images/', $filename);
}
else{
return redirect()->route('gallery.view');
}
}
I have been asking around for several weeks but couldn't get any answer. I really need your help!
Screenshot of images being successfully uploaded
Screenshot of images present in the folder
This happened to me once because the server when going through the images it overwrites some images because they get the same filename, to avoid this use something like this to make sure you have unique filenames:
$random = mt_rand(1,1000);
$filename = time().''.$random. '.' . $file->getClientOriginalExtension();

I want to upload and save image in database but when i try to do it Call to a member function images() on null error shown up

I am beginner in laravel and i want to make image uploading and saving app.
Everything is going cool but as i try to upload images it isnot saved to database.
But in public/gallery/images folder images are present.How this is possible without saving in database.
When i try to upload following error shown up:
FatalErrorException in GalleryController.php line 71:
Call to a member function images() on null
My controller is:
public function doImageUpload(Request $request){
//get the file from the post request
$file = $request->file('file');
//set my file name
$filename = uniqid() . $file->getClientOriginalName();
//move the file to correct location
$file->move('gallery/images',$filename);
//save image details into the database
$gallery = Gallery::find($request->input('gallery_id'));//get the gallery_id
$image = $gallery->images()->create([
'gallery_id'=>$request->input('gallery_id'),
'file_name'=>$filename,
'file_size'=>$file->getClientSize(),
'file_mime'=>$file->getClientMimeType(),
'file_path'=>'gallery/images/' . $filename,
'created_by'=>1,
]);
My view is:
<div class="row">
<div class="col-md-12">
<form action="{{url('image/do-upload')}}"
method="POST" enctype="multipart/form-data">
<label>Select image to upload:</label >
<input type="file" name="file" id="file">
<input type="submit" value="Upload" name="submit">
<input type="hidden" name="_token" value={{ csrf_token() }}>
</form>
</div>
and my image model is:
class Image extends Model
{
protected $fillable = [
'gallery_id','file_name','file_size','file_mime','file-path','created_by'
];
public function gallery(){
return $this->belongsTo('App\Gallery');
}
}
Being new to laravel i didnt get the actual error meaning Call to a member function images() on null??
How to fix this?
do a log debug on $gallery after you use the find method, there's a good chance it's not initialized, if the find method fails to find the id you've given it, it returns null
a good practice would be to verify you get an object back and verify your input contains gallery_id and that it is a number > 0
if ($request->has('gallery_id') && intval($request->input('gallery_id'))>0){
$gallery = Gallery::find($request->input('gallery_id'));//get the gallery_id
if ($gallery){
$image = $gallery->images()->create([
'gallery_id'=>$request->input('gallery_id'),
'file_name'=>$filename,
'file_size'=>$file->getClientSize(),
'file_mime'=>$file->getClientMimeType(),
'file_path'=>'gallery/images/' . $filename,
'created_by'=>1,
]);
} else {
return Response::make('no gallery found with this $request->input('gallery_id') gallery_id',404);
}
} else {
return Response::make('invalid gallery_id as input',400);
}
you could also create the image via the Image create method as is instead of using the gallery relationship, that would have created an image probably with the gallery_id of 0 if the galley_id input is incorrect.
$image = Image::create([
'gallery_id'=>$request->input('gallery_id'),
'file_name'=>$filename,
'file_size'=>$file->getClientSize(),
'file_mime'=>$file->getClientMimeType(),
'file_path'=>'gallery/images/' . $filename,
'created_by'=>1,
]);
I don't know if you resolved the problem, but I had the same issue and I found the solution.
When you send a form, you need to put a hidden input .
<input type"hidden" name="gallery_id" value="{{ $gallery->id }}">
I'm sure you work on the same project as me, DropZone Gallery, if so I think you have the solution

How do i retrieve the content data to blade from this json

i'm encountered with an blade issue.
[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]
This is the json i'm getting from webservice, i need to get each image url and need to assign to a tag . how could i achive it?
#foreach($json->content as $img)
<img src="{{$img}}" alt="alt text">
#endforeach
From the information provided that is the most I can give you.
For the given json, you can try something like this on your controller:
public function index()
{
$json_string = '[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]';
$json = json_decode($json_string, true);
$images = json_decode($json[0]['content']);
return view('index', compact('images'));
}
Then, on your blade view:
#foreach($images as $image)
<img src="$image">
#endforeach

Resources