Laravel Backpack Image Upload Issue - laravel

I m trying to upload images in Laravel Backpack. I had added field in my Controller:
$this->crud->addfield([
'label' => "Logo",
'name' => "logo",
'type' => 'image',
'upload'=> true,
'crop' => true, // set to true to allow cropping, false to disable
'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
//'disk' => 'public', // in case you need to show images from a different disk
'prefix' => 'uploads/college/' // in case your db value is only the file name (no path), you can use this to prepend your path to the image src (in HTML), before it's shown to the user;
]);
Here is my Model:
protected $fillable = ['name','description','logo','location','establised_at'];
// protected $hidden = [];
// protected $dates = [];
public function setImageAttribute($value)
{
$attribute_name = "logo";
$disk = "public";
$destination_path = "/uploads";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}
It's trying to save the base64 data in database but I don't want to do so.
It would be great help if anyone solve it. Thank You!

I've tried the code shown in docs and it works.
I think your problem is the mutator function name. To define a mutator you have to define a method in your model like set{attribute_name}Attribute.
In your case, your mutator function is setImageAttribute then it is looking for an attribute called image.
I think that if you change this code:
public function setImageAttribute($value)
{
with this
public function setLogoAttribute($value)
{
it will work

you miss a step:
like in doc
\Storage::disk($disk)->delete($this->{$attribute_name});
$public_destination_path = Str::replaceFirst('public/', '', $destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;

Related

How to save data as array in laravel

I have some of the data, I want to save my data-id in array format. How can I do this? Below is my controller code.
Controller:
public function PostSaveRentCertificateReport(Request $request)
{
$report = $request->session()->get('report');
$reports = new Report;
$reports->column_one = $report->sum('column_one');
$reports->column_two = $report->sum('column_two');
// I want to save those id as array
$reports->adc_report_id = array('$report->id');
$reports->save();
$notification = array(
'message' => 'Report Created Successfully',
'alert-type' => 'success'
);
return redirect()->route('adc.pending.reports')->with($notification);
}
You can encode the data as JSON before saving it.
$reports = new Report;
$reports->column_one = $report->sum('column_one');
$reports->column_two = $report->sum('column_two');
$reports->adc_report_id = json_encode([$report->id]);
$reports->save();
But this will make it a bit more difficult to work with.

Deleting an Item from array of a table column in Laravel

There is a Posts table that has a column named images. in images I store data as JSON array as:
["p1.jpg", "p2.jpg","p4.jpg","p9.jpg","p11.jpg", "p12.jpg","p13.jpg", "p14.jpg"];
I want to delete one of these images by ajax. I send the id and image by ajax and get that id and image in the controller correctly:
public function remove() {
$imgs = Image::where('id', request()->id)->pluck('images');
return $imgs;
}
reults in console:
[Array(8)]
0: (8) ["p1.jpg", "p2.jpg", "p4.jpg", "p9.jpg", "p11.jpg", "p12.jpg", "p13.jpg", "p14.jpg"]
length: 1
__proto__: Array(0)
also, I get the image name by request()->img from ajax.
$image = request()->img; => p12.jpg
How I can delete the $image from $images array?
First You may cast images attr to array in your Image model
//App\Image
protected $casts = [
'images' => 'array'
];
Then in your remove function :
public function remove() {
if(request()->id && request()->img) {
$imgModel = Image::findOrFail(request()->id);
$imgs = $imgModel->images;
// find request()->img position inside imgs array
$position = array_search(request()->img, $imgs);
// delete request()->img
unset($imgs[$position]);
$imgModel->images = array_values($imgs);
$imgModel->save();
}
}

Attach TCPDF to mail in Laravel

I want to attach pdf generated with tcpdf library without save.
I'm able to attach the pdf generated but it's corrupt.
I search a lot examples but any seems don't work
This my code:
public function index($id) {
$viaje = Viaje::find($id);
$users = User::orderBy('id', 'asc')->get();
// usersPdf is the view that includes the downloading content
$view = \View::make('usersPdf', ['viaje' => $viaje, 'users' => $users]);
$html_content = $view->render();
// Set title in the PDF
PDF::SetTitle("List of users");
PDF::AddPage();
PDF::writeHTML($html_content, true, false, true, false, '');
//PDF::Output('userlist.pdf');
$fileatt = PDF::Output($name='yourfilename.pdf', $dest='E');
$pdf = chunk_split($fileatt);
$contactopro = Contactoviajespro::find($id);
$data = [
'link' => 'http://',
'contacto' => $contactopro->name,
];
Mail::send('emails.notificacion', $data, function($msg) use($pdf) {
$msg->from('administracion#buendialogistica.com', 'Javier');
$msg->to('xavieeee#gmail.com')->subject('Notificación');
$msg->attachData($pdf, 'orden.pdf');
});
return redirect()->route('home')
->with(['message' => 'Email enviado correctamene']);
}
Use "S" to generate pdf and do not do chunk_split(), Laravel will do that. Additionally, if you are using queue() instead of send(), it will fail because of the attachment. To queue, write a job and send with the job queue.

How to display multiple images that stored as array in laravel?

please help me to display image that stored as array in database
my controller:
( filename is my image column name in database)
public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
// Start multiple image upload code
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
// End multiple image upload code
$houses= new House();
$houses->division = $request->input('division');
$houses->city = $request->input('city');
$houses->area = $request->input('area');
$houses->owner_name = $request->input('owner_name');
$houses->house_name = $request->input('house_name');
$houses->type = $request->input('type');
$houses->from = $request->input('from');
$houses->rent = $request->input('rent');
$houses->phone = $request->input('phone');
$houses->address = $request->input('address');
$houses->description = $request->input('description');
$houses->filename=json_encode($data); **// This for image upload**
$houses->save();
return back()->with('success', 'Your House has been successfully');
}
image column
First array cast the filename column in your model. For more: Array & JSON Casting
protected $casts = [
'filename' => 'array',
];
After that, whenever you fetch filename from House model, laravel will automatically convert the json data to array. Loop through the array and display the images.
$house = House::find(1);
foreach($house->filename as $filename) {
echo public_path().'/images/', $filename;
}
If you want to retrieve then display the images stored as an array what I'd suggest is casting the filename attribute to json/array in the model.
You can do that in your House model like this:
class House extends Model
{
protected $casts = [
'filename' => 'array',
];
}
To display it in a view you can loop through the casted array:
#foreach($house->filename as $image)
<img src="{{ url('link/to/assets/') . $image }}"
#endforeach
See more in the docs about casting here:
https://laravel.com/docs/5.7/eloquent-mutators#array-and-json-casting
A side point on image uploads, you may want to give uploaded files a unique name rather than using the user's filename so there's no conflict if a user uploads a document with the same name twice.
Laravel handles this all for you in the store() method seen here: https://laravel.com/docs/5.7/requests#storing-uploaded-files. You can create your own names if preferred using storeAs().

Intervention\Image\Exception\NotSupportedException Encoding format (tmp) is not supported

I am using the Intervention package with Laravel 5.6, the issue I am getting whenever I am uploading a file I have been presented with the error Encoding format(tmp) is not supported. I have my gdd2 extension enabled also. This is the code where I have used.
public function store(Request $request)
{
$this->validate($request , [
'name' => 'required|unique:categories',
'description' => 'max:355',
'image' => 'required|image|mimes:jpeg,bmp,png,jpg'
]);
// Get Form Image
$image = $request->file('image');
$slug = str_slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imageName = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
// Check if Category Dir exists
if (!Storage::disk('public')->exists('category'))
{
Storage::disk('public')->makeDirectory('category');
}
// Resize image for category and upload
$categoryImage = Image::make($image)->resize(1600,479)->save();
Storage::disk('public')->put('category/'.$imageName, $categoryImage);
// Check if Category Slider Dir exists
if (!Storage::disk('public')->exists('category/slider'))
{
Storage::disk('public')->makeDirectory('category/slider');
}
// Resize image for category slider and upload
$categorySlider = Image::make($image)->resize(500,333)->save();
Storage::disk('public')->put('category/slider/'.$imageName, $categorySlider);
}
else
{
$imageName = 'default.png';
}
$category = new Category();
$category->name = $request->name;
$category->slug = $slug;
$category->description = $request->description;
$category->image = $imageName;
$category->save();
Toastr::success('Category Saved Successfully','Success');
return redirect()->route('admin.category.index');
}
You don't need to use the save() function on the Intervention\Image class as you are saving the file to your public disc via the Storage Facade.
Simply replace the line
$categoryImage = Image::make($image)->resize(1600,479)->save();
with
$categoryImage = Image::make($image)->resize(1600,479)->stream();
to avoid having to store the image to the temp folder under a .tmp extension. Laravel Storage Facade will handle the stream created by Intervention\Image and store the file to the public disk.
The Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in.
The reason you are getting the error is it does not know what encoding to save the temporary image object (tmp) in.
Here is an example
->save('my-image.jpg', 90)
There is also a optional second parameter that controls the quality output. The above outputs at 90% quality.
http://image.intervention.io/api/save
Saw this somewhere and it worked for me
$image->save('foo' . $img->getClientOriginalExtension());
The Laravel Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in
$categoryImage = Image::make($image)->resize(1600,479)->save( $imageName,90);
I've solved this by
Trimming
my file path, i was using this script inside laravel Artisan Console.
$img->save(trim('public/uploads/images/thumbnails/'.$subFolder.'/'.$filename));
Rather you use stream its working without error
$categoryImage = Image::make($image)->resize(1600,479)->save();
$categoryImage = Image::make($image)->resize(1600,479)->save();
Storage::disk('public')->put('category/'.$imageName, $categoryImage);
change to
Image::make($image)->resize(1600, 479)->save(storage_path('app/public/category').'/'.$imagename);
$categorySlider = Image::make($image)->resize(500,333)->save();
Storage::disk('public')->put('category/slider/'.$imageName, $categorySlider);
change to
Image::make($image)->resize(500, 333)->save(storage_path('app/public/category/slider/') .$imagename);

Resources