I'm using the Image Intervention package in Laravel and I can't display my profile image. The problem is that the browser fails to load the resource
localhost:8000/uploads etc.
All files are saved in public folder :
public/uploads
My view:
<div class="row">
<div class="col-md-2 ">
<img src="uploads/{{ $user->avatar }}">
</div>
</div>
Controller:
class UserController extends Controller
{
public function profile()
{
return view ('user.profile', array('user'=>Auth::user()));
}
public function update(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/avatar' . $filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('user.profile',array('user'=>Auth::user()));
}
}
I'm using windows and i see these files saved in my folder public/uploads but i cant display it
Regards
When you save the image, add a / after "uploads/avatar":
save(public_path('/uploads/avatar/' . $filename));
That should solve the naming problem. Then in your HTML add "avatar/" after "uploads/", as you are saving to the "upload/avatar" folder.
Then your view would look like this:
<div class="row">
<div class="col-md-2 ">
<img src="uploads/avatar/{{ $user->avatar }}">
</div>
</div>
In your controller class change this line:
$user->avatar = $filename;
to this:
$user->avatar = 'avatar' . $filename;
Related
I am doing an Instagram clone project and I have encountered a tricky problem because, there is no error really, but the app is not functioning like its supposed to. Basically, I have added a functionality to instagram web application, where the user can add a post. Once the user uploads the image and caption, the create post page is supposed to redirect back to their profile. Which it does, perfectly. But the problem is, the uploaded posts are not displaying on the profile. Here is how my index.blade looks like
<div class="d-flex">
<div style="padding-right: 25px;"><strong>153</strong> posts</div>
<div style="padding-right: 25px;"><strong>2M</strong> followers</div>
<div style="padding-right: 25px;"><strong>300</strong> following</div>
</div>
<div><strong>{{$user->profile->title}}</strong></div>
<div>{{$user->profile->description}}</div>
<div>{{$user->profile->url}}</div>
</div>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4>
<img src="storage/{{ $post->image }}" class="w-100">
</div>
#endforeach
</div>
</div>
My PostsController
class PostsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$data = $request->validate([
//'another =>'', Tells laravel to ignore all other fields
'caption'=>'required',
'image'=> 'required' ,
]);
if($request->hasfile('image'))
{
$data = $request->file('image');
$extension = $data->getClientOriginalExtension();
$filename = time().'.'.$extension;
$data->storeAs('uploads/public/', $filename);
}
$data = Auth::user()->posts()->create([
'caption'=>$request->caption,
'image'=>$request->image,
]);
$data = Auth::id();
return redirect()->route('Profile.show', [$data]);
//goes ahead and grabs the authenticated user
}
}
My routes
Route::get('/p/create', [App\Http\Controllers\PostsController::class, 'create'])->name('posts.create');
Route::post('/p', [App\Http\Controllers\PostsController::class, 'store']);
Route::get('/Profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('Profile.show');
i have a project that it has some posts on it
i have made admin panel to create new posts
since i uploaded my project on cpanel host i can create a post but image wont shown in my post but before that on my localhost i can craete a post and store image in my path
i checked my public path in my host and saw that there isn,t any new image related to my post that i have created with my admin panel on my host just my image name has saved in my database
this is my blog blade file
<section class="w-80 mx-auto my-5">
<div class="container">
<div class="row">
#foreach($posts as $post)
<div class="col-md-4 translate-box relative mb-4">
<div class="blog-box-height">
<img src="{{asset('image/'.$post->header_image)}}" alt="{{$post->title}}" class="img-fluid">
<div class="py-3 px-4">
<a class="text-xmedium fw-bold text-decoration-none text-black" href="{{route('blog.single',['title'=>$post->title])}}">
{{str_replace('_',' ',$post->title)}}
</a>
<p class="text-muted-1 mt-2 text-small">
{!! strip_tags(mb_substr($post->content,0,270,'utf-8')) !!} ...
</p>
<div class="box-time absolute">
<p class="text-muted-1"><i class="far fa-clock"></i> {{$post->time}} دقیقه</p>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</section>
this is my postcontroller codes
public function storeImage(Request $request)
{
$image_name = null;
if ($request->file('image')) {
$image = $request->file('image')->getClientOriginalName();
$image_name = time() . $image;
$folder = public_path('/image');
$path = $folder . '/' . $image_name;
if (is_dir($folder)) {
$img = Image::make($request->file('image'))->resize(500, 438);
$img->save($path);
} else {
File::makeDirectory($folder, 0755, true);
$img = Image::make($request->file('image'))->resize(500, 438);
$img->save($path);
}
}
return $image_name;
}
this code works great in my localhost server but when i uploaded it on host i think image intervation wont work
You have to set the symlink to connect the stroge folder with the public folder.
Try running php artisan storage:link on the webserver.
Also in some hostings this command is banned. In that case you have to create the symlink manually using forexample midnight commander.
Try with 777 permission for make directory.
File::makeDirectory($folder, 0777, true);
I am making a pdf, for which I use the library: barryvdh/laravel-dompdf, in the pdf I try to print an image, with which when I open the recently downloaded file, all the data is printed except the image, I clarify this image if it is displayed correctly in my show.blade view, I cannot find a solution for this problem.
Below I detail how I am proceeding:
Controller: (fragment of store method and exportPdf method)
public function store(SaveDocumentRequest $request)
{
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$name = time().$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
}else{
$name = null;
}
$document->photo = $name;
public function exportPdf()
{
$movie = Movies::with('document.creator', 'actors', 'generate_movie', 'document.adequacy', 'document.lenguage')->first();
$pdf = PDF::loadView('admin.movies.show', compact('movie'));
return $pdf->download('cine.pdf');
}
My show.blade file:
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Imagen de Portada </h3>
</div>
<div class="box-body box-profile">
<img class="img-responsive" src="{{ asset('/images/'.$movie->document->photo) }}" alt="{{ $movie->document->title }}" >
</div>
</div>
</div>
This way I am accessing the image that if it is displayed correctly in my show.blade file, but it is not displayed in the downloaded pdf.
I clarify I do not receive any type of error and the image is stored in the public folder of the project.
Could you guide me to find the solution?
Its simple just use this I have used same package and added image on pdf like this.
<img src="{{asset('foldername/path_to_image')}}">
So i was making a form that consists of three field, the title, thumbnail, and textarea(with texteditor) and it seems that it upload the image just fine since i notice that there is images that i had uploaded in my /public/image folder after i submit the form, but when i check the database the thumbnail field showed not the name of the file in the /public/image folder like 20190713125534.jpg but
C:\xampp\tmp\phpC18A.tmp
im confused i thought it doesn't upload the image at all, but as i explained earlier it does, so my question is how do i change the value of thumbnail field with the filename and how do i show the image in my view ?
this is my Blogcontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Blog;
use Validator,Redirect,Response,File;
class BlogController extends Controller
{
public function index()
{
$blogs = Blog::get();
return view('post.post_textarea',[
'blogs' => $blogs
]);
}
public function store(Request $request)
{
Blog::create([
'name' => $request->name,
'message' => $request->message,
'thumbnail' => $request->thumbnail,
]);
if ($files = $request->file('thumbnail')) {
$destinationPath = 'public/image/'; // upload path
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
$insert['thumbnail'] = "$profileImage";
}
return redirect()->back();
}
public function getFullPost($blog_id) {
$blogs = Blog::where('id', '=', $blog_id)->get();
return view('post.read')->with(compact('blogs'));
}
}
this is my view for the form
<form enctype="multipart/form-data" method="POST">
{{csrf_field()}}
<div class="form-group">
<h4 style="color: black;" >Judul</h4>
<br>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<h4 style="color: black;" >Thumbnail</h4>
<br>
<input type="file" name="thumbnail">
</div>
<div class="form-group">
<h4 style="color: black;" >Isi</h4>
<br>
<textarea class="form-control" name="message" id="" rows="10"></textarea>
</div>
<div class="form-group">
<button class="pull-right site-btn" type="submit" >Upload<img src="../public/asset/img/icons/double-arrow.png" alt="#"/></button>
</div>
</form>
and this is my view to display the data from database
#foreach ($blogs as $blog)
<div class="blog-item">
<div class="blog-thumb">
<img src="asset/img/blog/1.jpg" alt=""> ->this is where i was supposed to fetch the image
</div>
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($blog->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $blog->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($blog->message, 30, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
#endforeach
try to add this in controller
$blog = new Blog;
$blog->name = $request->name;
$blog->message = $request->message;
if($request->hasFile('thumbnail')) {
$file = Input::file('thumbnail');
//getting timestamp
$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
$name = $timestamp. '-' .$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$blog->thumbnail = url('/images/' . $name);
}
$blog->save();
return back();
than in your view
#foreach ($blogs as $blog)
<div class="blog-item">
<div class="blog-thumb">
<img src="{{ $blog->thumbnail }}" alt=""> ->this is where i was supposed to fetch the image
</div>
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($blog->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $blog->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($blog->message, 30, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
#endforeach
The problem with your code, I think in your store function, you did not save it correctly. Please see my code below to save link of your thumbnail into db.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Blog;
class BlogController extends Controller{
//some your function goes here
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$blog = new Blog;
$blog->name = $request->input('name');
$blog->message = $request->input('message');
$file = $request->file('your_thumbnail');
//make sure yo have image folder inside your public
$destination_path = 'image/';
$profileImage = date("Ymd").".".$file->getClientOriginalName();
$file->move($destination_path, $profileImage);
//save the link of thumbnail into myslq database
$blog->thumbnail = $destination_path . $profileImage;
$blog->save();
return redirect()->back();
}
}
In my application, a user can upload an image for their profile photo. How do I update a users photo and the delete the previously uploaded photo? This is what I have so far in the upload controller:
Photos Model
class Photos extends Eloquent {
protected $table = 'photos';
protected $fillable = array('user_id', 'location');
public function user (){
return $this->belongsTo('User');
}
}
user model
public function photos()
{
return $this->hasOne('photos');
}
Upload Controller
class UploadController extends \BaseController {
public function postUpload (){
$id = Auth::user()->id;
$input = Input::all();
$rules = array(
'photo' => 'required|image|max:500'
);
$validator = Validator::make($input, $rules);
if($validator->fails()){
return Redirect::to('user/profile/'.$id.'/edit')->withErrors($validator);
}
$extension = Input::file('photo')->getClientOriginalExtension();
$directory = public_path('/var/chroot/home/content/59/11581559/html/beta') . '/uploads/'.sha1($id);
$filename = sha1($id.time()).".{$extension}";
$upload_success = Input::file('photo')->move($directory, $filename);
Image::make(Input::file('photo')->getRealPath())->resize(300, 200)->save('foo.jpg');
if($upload_success){
$photo = new Photos(array(
'location' => URL::to('/uploads/'.sha1($id).'/'.$filename)
));
$photo->user_id = $id;
$photo->save();
Session::flash('success_photo', 'You have Successfully uploaded your profile photo!');
return Redirect::to('user/profile/'.$id.'/edit');
}
else{
Session::flash('status_error', 'An Error has occurred while uploading your photo. Please try again!');
}
return Redirect::to('user/profile/'.$id.'/edit');
}
}
View
<div class="col-lg-3">
<div class="panel panel-default">
<div class="panel-heading">
#if(Session::has('status_error'))
<div class="alert alert-error">
<a class="close" data-dismiss="alert" href="#" aria-hidden="true">×</a>
{{ implode('', $errors->all('<li class="error"><b>:status_error</b></li>')) }}
</div>
#endif
<span class="glyphicon glyphicon-exclamation-sign"></span> Upload a Photo
</div>
<div class="panel-body text-center">
#if($user->photos)
<img width="190" height="119" class="img-responsive img-thumbnail" src="{{$user->photos->location }}" alt="">
#endif
<div class="clearfix"></div>
<div class="btn-group mtop-20">
<button type="button" class="btn btn-primary" onclick="$('#upload_modal').modal({backdrop:'static'});">
<span class="glyphicon glyphicon-upload"></span>
</button>
</div>
</div>
<?php echo View::make('modals.photo-upload') ?>
</div><!-- end panel-default -->
</div><!-- col 3-->
Let's talk about what you're doing and work from there.
Uploading a Photo
- In your controller, you're uploading a photo.
- If the upload succeeds, you're storing the location of the photo on your server as well as the ID of the user to whom the photo belongs.
Updating a Photo
- In your controller, you're uploading a photo.
- If the upload succeeds, you're going to:
Get the location of the current file in the database.
$this->photo->where('user_id' , '=' , $the_user_id_in_question)
->get(array('location'));
Delete the file at that location using the delete() function in the FileSystem class (found here: https://github.com/laravel/framework/blob/master/src/Illuminate/Filesystem/Filesystem.php)
Update the user's photo location with the new one:
$this->photo->where('user_id' , '=' , $the_user_id_in_question)
->update('location' => $the_new_upload_location);
I hope that's what you're looking for. Cheers!