Laravel 5 - Image Upload and Resize using Intervention Image Package - laravel

I want to upload a photo with some posts.
This is my controller
public function store(WisataRequest $request)
{
$input = $request->all();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$input->gambar = $filename;
$input->save();
}
}
$wisata = Wisata::create($input);
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}
But when it runs i found an error
Attempt to assign property of non-object

Change
$input->gambar = $filename;
$input->save();
To
$input['gambar']= $filename;

$input variable is not an object, it is an array. You can try accessing gambar in $input by doing $input['gambar']

You can put
$input['gambar']= $filename;
Instead of
$input->gambar = $filename;
$input->save();
OR
public function store(WisataRequest $request)
{
$wista = new Wista;
$wist->name = $request->name;
-----
$wista->save();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$wista->gambar = $filename;
$wista->save();
}
}
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}

Related

Laravel download image

I have a function to add an image, and upon successful addition in the database, we have a path like public/images/asd.png. The question is how to make sure that when added to the name of the picture, an ID is added, and we have something like public/images/asd1.png, public/images/asd2.png, etc.
function in Model
public function getOriginImageUrl()
{
return $this->attributes['image'];
}
public function getImageAttribute($value)
{
return Storage::exists($value) ? Storage::url($value) : null;
}
function in Controller
if ($request->hasFile('image')) {
$file = $request->file('image');
$blog->image = $file->storeAs('public/images', $file->getClientOriginalName());
}
Instead of id you can combine time() with image name.
if ($request->hasFile('image')) {
$file = $request->file('image');
$namewithextension = $file->getClientOriginalName(); //Name with extension 'filename.jpg'
$name = explode('.', $namewithextension)[0]; // Filename 'filename'
$extension = $file->getClientOriginalExtension(); //Extension 'jpg'
$uploadname = $name. '-' .time() . '.' . $extension;
$blog->image = $file->storeAs('public/images', $uploadname);
}

How to convert base64 images to Url images when using summer note text editor in Laravel inside controller?

I am trying to use image url instead of base64. i kindof figured a way how to get images out of the editor and save it inside the directory, but i am trying to insert it into database. i get this error.
Object of class DOMElement could not be converted to string
below i have added the store function inside my controller
public function store(Request $request)
{
//
$data = $request->all();
if (!$request->has('published')) {
$data['published'] = 0;
} else {
$data['published'] = 1;
}
if (!$request->has('featured')) {
$data['featured'] = 0;
} else {
$data['featured'] = 1;
}
$img = null;
if ($request->hasfile('title_img')) {
$image = $request->file('title_img');
$name = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path() . '/uploads/blogs/', $name);
$img = '/uploads/blogs/' . $name;
}
/* summernote */
$detail=$data['description'];
$dom = new \DomDocument();
$dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
foreach($images as $k => $img){
$data = $img->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$image_name= "/uploads/summernote/" . time().$k.'.jpeg';
$path = public_path() . $image_name;
file_put_contents($path, $data);
$img->removeAttribute('src');
$img->setAttribute('src', $image_name);
}
$detail = $dom->saveHTML();
/* summernote */
$data['title_img'] = $img;
$data['user_id'] = auth()->user()->id;
$title = $data['title'];
$data['slug'] = Str::slug($title);
auth()->user()->posts()->create($data);
return Redirect::route('admin.blog.index')->withSuccess('Whoopie!! New Blog Added!');
}
I hope i can get some help regarding this. Thanks in advance

Laravel upload gifs

i am uploading gif for my posts in laravel but gif is like an image its not moving or something like this
<?php
if($request->hasFile('gif')){
$gif = $request->file('gif');
$gif_filename = time() . '.' . $gif->getClientOriginalName();
$gif_location = public_path('/images/' . $gif_filename);
Image::make($gif)->save($gif_location);
}
$post->gif = $gif_filename;
$post->save();
?>
here is the code what I am using I think everything is kinda correct
I use this method
if(Input::hasFile('imagen')) {
$time = Carbon::now()->format('Y-m-d');
$image = $request->file('imagen');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = $time."-".$name;
$image->move(storage_path(),$fileName);
}
Please try this and let me know how it works :)
An easy way to update and save is to do it like this:
public function store(Request $request)
{
$imgLocation = Storage::disk('public')->put(time() . '.' . $request->file('image')->getClientOriginalName(), $request->gif), $request->file('gif'));
// This would save it to the gifs table if you need something like it, otherwise skip this creation
$gif= Gif::create([
'name' => $request->name,
'path' => $imgLocation
]);
if ($gif) {
return response()->json("Success!");
}
return response()->json("Error!"); // or you return redirect()...
}
$image = $request->file('image');
if(isset($image)) {
if($image->getClientOriginalExtension()=='gif'){
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = 'exerciseimages'."-".$name;
$image->move('storage/courseimages/',$fileName);
}
else{
$fileName = 'exerciseimages'.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('courseimages')){
Storage::disk('public')->makeDirectory('courseimages');
}
$amenitiesimg = Image::make($image)->resize(250,250)->stream();
Storage::disk('public')->put('courseimages/'.$fileName, $amenitiesimg);
}
}
else {
$fileName = 'default.png';
}

How to upload image in array input type

I want to upload image using protected function create(array $data){}
Below code is used for public function create(Request $request){}
public function create(Request $request)
{
$image = new Image();
if ($request->hasFile('image')) {
$dir = 'uploads/';
$extension = strtolower($request->file('image')->getClientOriginalExtension()); // get image extension
$fileName = str_random() . '.' . $extension; // rename image
$request->file('image')->move($dir, $fileName);
$image->image = $fileName;
}
$image->save();
return view('here');
}
}
I tried the following code but gets error
protected function create(array $data)
{
$dir = '/customer/images/';
$extension = strtolower($data['image']->getClientOriginalExtension()); // get image extension
$fileName = str_random() . '.' . $extension; // rename image
$data['image']->move($dir, $fileName);
$data['image'] = $fileName;
return Image::create([
'image' => $data['image'],
]);
}
I'm getting error. How can i upload image using array.

I want to delete the stored image while update new image

I want to delete the stored image while update new image
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
You can write this. This will solve your problem
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
This will delete the previous image and update the new image
Well, the answer is technically incorrect. What if the save operation fails, since you have deleted that image the current record will not have an image anymore.
So to overcome this problem you can adjust your code like:
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
}

Resources