I am trying to use Redactor with Laravel4. I can succesfully edit my textarea but I cant get to work with image uploads. When I try to upload a file I get 500 error and In developer tools , I can see
Request URL:http://projemiz.dev/admin/blogs/3/postimage/3
This is my link for redactor photo upload:
<script>$('#editor').redactor({ imageUpload: "postimage/{{$post->id}}"});</script>
My routes are inside prefixes :
# Blog Management
Route::group(array('prefix' => 'blogs'), function()
{
Route::get('/', array('as' => 'blogs', 'uses' => 'Controllers\Admin\BlogsController#getIndex'));
Route::get('create', array('as' => 'create/blog', 'uses' => 'Controllers\Admin\BlogsController#getCreate'));
Route::post('create', 'Controllers\Admin\BlogsController#postCreate');
Route::get('{blogId}/edit', array('as' => 'update/blog', 'uses' => 'Controllers\Admin\BlogsController#getEdit'));
Route::post('{blogId}/edit', 'Controllers\Admin\BlogsController#postEdit');
Route::post('{blogId}/postimage','Controllers\Admin\BlogsController#postImage');
Route::get('{blogId}/delete', array('as' => 'delete/blog', 'uses' => 'Controllers\Admin\BlogsController#getDelete'));
});
and my controller is :
public function postImage($blogId) {
$path = base_path().'/public/uploads/img/posts/' . (int)$blogId;
$image = Input::file('photo');
if (Input::hasFile('photo'))
{
$fileName = $file->getClientOriginalName();
$image->move($path,$fileName);
$image = new Image;
$image->name = $fileName.name;
$image->save();
// resizing an uploaded file
Image::make($image->getRealPath())->resize(300, 200)->save($path.'thumb-'.$fileName);
Image::make($image->getRealPath())->resize(300, 200)->save($path.'thumb-'.$fileName);
//File::delete( $path . '/' . Input::file('file.name'));*/
}
}
Can anyone help me to fix my link inside redactor?
Try changing your js-script this:
<script>$('#editor').redactor({ imageUpload: "/{{$post->id}}/postimage"});</script>
In the upload function return the path of the image after upload
public function postImage($blogId)
{
$path = base_path().'/public/uploads/img/posts/' . (int)$blogId;
$image = Input::file('photo');
if (Input::hasFile('photo'))
{
$fileName = $file->getClientOriginalName();
$image->move($path,$fileName);
$image = new Image;
$image->name = $fileName.name;
$image->save();
// resizing an uploaded file
Image::make($image->getRealPath())->resize(300, 200)->save($path.'thumb-'.$fileName);
Image::make($image->getRealPath())->resize(300, 200)->save($path.'thumb-'.$fileName);
// Return Image path as JSON
if ($file->move($path, $fileName))
{
return Response::json(array('filelink' => $path . '/' . $fileName));
}
}
}
Related
In my script all images uploaded goes into directory "ib" but i want to change that directory to a different name eg. "imgib"
here is what i did so far. i changed the code vlues from "ib" to "imgib"
} else {
// upload path
$path = 'imgib/';
// if path not exists create it
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
// move image to path
$upload = $request->file('uploads')->move($path, $imageName);
// file name
$filename = url($path) . '/' . $imageName;
// method server host
$method = 1;
}
// if image uploded
if ($upload) {
// if user auth get user id
if (Auth::user()) {$userID = Auth::user()->id;} else { $userID = null;}
// create new image data
$data = Image::create([
'user_id' => $userID,
'image_id' => $string,
'image_path' => $filename,
'image_size' => $fileSize,
'method' => $method,
]);
// if image data created
if ($data) {
// success array
$response = array(
'type' => 'success',
'msg' => 'success',
'data' => array('id' => $string),
);
// success response
return response()->json($response);
} else {
if (file_exists('imgib/' . $filename)) {$delete = File::delete('imgib/' . $filename);}
// error response
return response()->json(array(
'type' => 'error',
'errors' => 'Opps !! Error please refresh page and try again.',
));
}
so far everything looks ok and it creates "imgib" directory automatically and all uploads go into "imgib" directory.
But the issue is, image url still uses the same old directory name.
eg. site.org/ib/78huOP09vv
How to make it get the correct url eg. site.org/imgib/78huOP09vv
Thanks for the help everyone. I managed to fix the issue by editing viewimage.blade.php
Yes of course need to clear the browser cache after editing the files.
I want my user's old image to be deleted when they update it. This current function does not work how I want it. How would I change it so it works?
public function update($id)
{
$profile = Profile::findOrFail($id);
$data = request()->validate([
'title' => 'required|max:255',
'image' => ''
]);
if ($profile->image) {
if (Storage::exists("storage/{$profile->image}")) {
Storage::delete("storage/{$profile->image}");
}
}
if (request('image')) {
$imagePath = request('image')->store('profile', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))->orientate()->fit(1000, 1000); //Intervention Image Package
$imageArray = ['image' => $imagePath];
$image->save();
}
// $profile->image = $request->image; //Lägg till senare
$profile->update(array_merge(
$data,
$imageArray ?? [],
));
return redirect("/profile/{$profile->user_id}");
}
}
First you have to take the old image and delete it from the server
if (file_exists(('./images/partners/' . $partner->image_path))) {
unlink(('./images/partners/' . $partner->image_path));
}
after that you can upload your new image and update the record in the database
I uploaded my files on my server, but I cannot change my root folder to public via the hosting company, so I moved the files from public to the httpdocs directory, but I have now an issue with uploading images, i made this path
$this->validate($request, [
'image' => 'image|nullable|max:1999'
]);
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . Carbon::today()->toDateString() . '.' . $extension;
$request->file('image')->storeAs('/../Supplier/public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
And when I submit my form I get this error
Path is outside of the defined root, path:
And to show the image after upload i have this code in html
<td><a download="retourmelding_{{$retour->firmaname}}" href="/storage/images/{{$retour->images}}" title="Foto">
<img alt="Foto" src="/storage/images/{{$retour->images}}">
</a></td>
But locally it works perfectly
Please try it:
$request->file('image')->storeAs(storage_path('images'), $fileNameToStore);
The directory definition you have made is incorrect.
../httpdocs/storage/images/ mean is [laravel directory]/httpdocs/storage/images/
Use helpers for directory defination: Helpers - Laravel
I would change the disk in "config \ filesystems.php" and put something like this:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
You can do that using the below code:
In config/filesystem.php
'my_file' => [
'driver' => 'local',
'root' => storage_path(),
],
In controller
$fileNameToStore = $request->file('myfile');
$name = $fileNameToStore->getClientOriginalName();
Storage::disk('my_file')->PutFileAs('images', $fileNameToStore, $name);
For retrieving the image in view file using the route. Because of directly can not access the storage/images folder.
You need to create a new function in the controller.
use Auth, Storage, File, Response;
public function displayImage($filename)
{
$path = storage_path('images/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
New Route add
Route::get('image/{filename}', 'YOURCONTROLLER#displayImage')->name('image.displayImage');
View( Retrieving image)
<img src="{{ route('image.displayImage',$image) }}" alt="" title="">
I want to override the way Laravel Spark save the profile picture of a user to use an external driver such as S3 for example. I already have my S3 config for the bucket I want to use. What would be the best way to do this? Should I use a completely different route and use a custom endpoint or is there a config somewhere I could change so that Spark uses a different driver?
So ended up doing this
I added these methods in update-profile-photo.js
methods: {
updateProfilePhoto() {
axios.post('/settings/profile/details/profile-picture', this.gatherFormData())
.then(
() => {
console.log('Profile picture updated');
Bus.$emit('updateUser');
self.form.finishProcessing();
},
(error) => {
self.form.setErrors(error.response.data.errors);
}
);
},
gatherFormData() {
const data = new FormData();
data.append('photo', this.$refs.photo.files[0]);
return data;
}
}
And my Controller looked like this
public function updateProfilePicture(Request $request)
{
$this->validate($request, [
'photo' => 'required',
]);
// Storing the photo
//get filename with extension
$filenamewithextension = $request->file('photo')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('photo')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
Storage::disk('s3_users')->put($filenametostore, fopen($request->file('photo'), 'r+'), 'public');
$url = $filenametostore;
$request->user()->forceFill([
'image_url' => $url
])->save();
return response()->json(
array(
"message" => "Profile picture was updated!",
)
);
}
Please i want to change the default laravel configuration from storage/app/public to public/media on the server. In localhost it worked with storage:link but on the server it isn't working even after adding a symlink. Below is my symlink code.
<?php symlink('/home/cemo/cem/storage/app/public','/home/cemo/public_html');
Also if i return the public_path() from the store function i get /home/cemo/cem/public
this is the structure of my cpanel
below is my store function using image intervention
public function store(Request $request)
{
return public_path();
$this->validate($request,[
'title'=>'required|min:6',
'body'=>'required'
]);
if($request->hasFile('image')){
$img = $request->file('image');
$imgName = time().'-'.uniqid() . '.' . $img->getClientOriginalExtension();
}
else{
$imgName = 'default.jpg';
}
$posts = new post;
$posts->title = $request->title;
$posts->body = $request->body;
$posts->posted_by = $request->posted_by;
$posts->status = $request->status;
$posts->position = $request->position;
$posts->image = $imgName;
$posts->source = $request->source;
$posts->save();
$posts->tags()->sync($request->tags);
if(!empty($img)){
Image::make($img)->resize(1500,550)->save(public_path('/media/images/blog/'. $imgName));
}
$notification = array(
'message' => 'Successfully created',
'alert-type' => 'success'
);
return redirect(route('post.index'))->with($notification);
}
In config/filesystems.php
Change the array to:
'root' => 'public/media',