I'm trying to referencing an image I Laravel but I don't know how to adding
<img src="{{ asset('images/avatar/helmet/'.Auth::user()->avatar()->first()->helmet.'.png')}}">
Did you save the file name with its original extension in the data base? If so, the code below should work.
<img src="{{ asset('images/avatar/helmet/'.Auth::user()->avatar)}}">
I will recommend to save the file with its original extension by adding ->getClientOriginalExtension().
It will look like this:
$image = $request->file('imagename');
if (isset($image))
{
$imagename = uniqid() .'-'. $image->getClientOriginalExtension();
if (!file_exists('Uploads/Media/Slider'))
{
mkdir('Uploads/Media/Slider', 0777 , true);
}
$image->move('Uploads/Media/Slider',$imagename);
}else {
$imagename = 'slider.png';
}
Related
I have integrated ckeditor in my laravel project, and when i try add a picture in the text after i click on save post. I Get an error in the console. POST https://encode.ba/admin/lista-vijestis/4 403 (Forbidden)
The upload working fine, permission on the storage folder is set properly
and of course, don't record my post
it seems to me that the server is blocking something, I'm not sure, that's why I'm asking, maybe someone has a similar experience
I downloaded the project to my computer and ran it on a local server and everything works fine.
here is a screenshot
enter image description here
enter image description here
Here is first screen shot when i try add post.
In your html page:
<textarea name="description" placeholder="Enter the Description"></textarea>
In script:
<script src="https://cdn.ckeditor.com/4.12.1/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('description', {
filebrowserUploadUrl: "{{ route('admin.product.uploadMedia', ['_token' => csrf_token()]) }}",
filebrowserUploadMethod: 'form'
});
</script>
create Route:
Route::post('product/img', [ProductController::class, 'uploadMedia'])->name('admin.product.uploadMedia');
In Controller:
public function uploadMedia(Request $request)
{
if ($request->hasFile('upload')) {
$originName = $request->file('upload')->getClientOriginalName();
$fileName = pathinfo($originName, PATHINFO_FILENAME);
$extension = $request->file('upload')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$path = storage_path('app/public/tmp/uploads');
$fileName = $request->file('upload')->move($path, $fileName)->getFilename();
$CKEditorFuncNum = $request->input('CKEditorFuncNum');
$url = asset('storage/tmp/uploads/' . $fileName);
$msg = 'Image uploaded successfully';
$response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
#header('Content-type: text/html; charset=utf-8');
echo $response;
}
return false;
}
I'm uploading an image to S3 with Laravel as follows:
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url($imageFileName);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}
The image uploads successfully but I need to store the URL in my database. This is being called here:
$image = Storage::cloud()->url($imageFileName);
The issue is the URL being returned, it looks like this:
http://test-env.XXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
Hence:
http://mentr-test-env.2w8sh3esch.us-west-2.elasticbeanstalk.com/profile/
is somewhat-correct. But the next piece is missing the 'profile' sub-folder, and obviously starts at HTTPS again:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
It would appear I'm getting two halves of the link in a single string. I don't edit the $image variable anywhere else.
The correct link is:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/profile/6_52644340.jpg
I have confirmed the files are uploading correctly and publicly available.
I have tried calling:
$image = Storage::cloud()->url($filePath);
And this returns:
http://test-env.XXXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-XXX123XXX.s3.us-west-2.amazonaws.com//profile/6_31595766.jpg
Update
I just noticed the first part of the returned URL is the BeanStalk instance URL with the /profile/ added. This is even stranger as I don't wish to use Beanstalk, I only want to use S3.
If you want to store the entire url you can get it from the return variable passed back from the put() function.
$s3_url = $s3->put($filePath, file_get_contents($image), 'public');
Sometimes I like to just store the path though and save just that piece to the database then I can pass the path to Storage::url($filePath); and it still works.
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url('s3-url/s3-bucket'.'/'.$filepath);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}
I am using below code to store the uploaded file
$file = $request->file($file_attachment);
$rules = [];
$rules[$file_attachment] = 'required|mimes:jpeg|max:500';
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()
->with('uploadErrors', $validator->errors());
}
$userid = session()->get('user')->id;
$destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
$uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));
The uploaded files are stored in storage/app/2/filename.jpg
I want to show back the user the file he uploaded. How can i do that?
$storage = Storage::get('/2/filename.jpg');
I am getting unreadable texts. I can confirm that the file is read. But how to show it as an image to the user.
Hope i made my point clear.
Working Solution
display.blade.php
<img src="{{ URL::asset('storage/photo.jpg') }}" />
web.php
Route::group(['middleware' => ['web']], function () {
Route::get('storage/{filename}', function ($filename) {
$userid = session()->get('user')->id;
return Storage::get($userid . '/' . $filename);
});
});
Thanks to: #Boghani Chirag and #rkj
File not publicly accessible like you said then read file like this
$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg');
Assuming your file is at path storage/app/{$userid}/file.jpg
and default disk is local check config/filesystems.php
File publicly accessible
If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.
php artisan storage:link
This create a symbolic link of storage/app/public to public/storage. Means now you can access your file like this
$contents = Storage::disk('public')->get('file.jpg');
here the file physical path is at storage/app/public/file.jpg and it access through symbolic link path public/storage/file.jpg
Suppose you have subfolder storage/app/public/uploads where you store your uploaded files then you can access it like this
$contents = Storage::disk('public')->get('uploads/file.jpg');
When you make your upload in public folder then you can access it in view
echo asset('storage/file.jpg'); //without subfolder uploads
echo asset('storage/uploads/file.jpg');
check for details https://laravel.com/docs/5.6/filesystem#configuration
Remember put your folder in storage/app/public/
Create the symbolic linksymbolic link to access this folder
php artisan storage:link
if you want to access profile images of 2 folder then do like this in your blade file
<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />
Laravel 8
Controller
class MediaController extends Controller
{
public function show(Request $request, $filename)
{
$folder_name = 'upload';
$filename = 'example_img.jpeg';
$path = $folder_name.'/'.$filename;
if(!Storage::exists($path)){
abort(404);
}
return Storage::response($path);
}
}
Route
Route::get('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);
Can you please try this code
routes.php
Route::group(['middleware' => ['web']], function() {
Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
});
});
view file code
<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />
Thanks
Try this:
Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');
Good Luck !
Uploaded like this
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
"public/avatar",
$uploadedFile,
$photo
);
and then access like this
<img src="{{ asset('storage/avatar/'.$filename) }}" />
Create Route:
Route::get('image/{filename}', 'HomeController#displayImage')->name('image.displayImage');
Create Controller Method:
public function displayImage($filename)
{
$path = storage_public('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;
}
<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">
can You please try this
$storage = Storage::get(['type_column_name']);
I am trying to resize, rename and upload the image using laravel Storage, intervention on laravel5.
Here is my upload code:
if( $request->file('logo_image') ){
$logo_path = $request->file('logo_image')->store('university/'.Auth::User()->id);
if ( $logo_path ){
Storage::delete($university->logo_image);
}
$university->logo_image = $logo_path;
}
$university->update();
It is storing the image on folder like: storage/app/public/university/1/fjkdhjkfdh.jpg
Now I want to rename image name like university-name.jpg and also resize and upload to sam directory.
So I should have two images one is storage/app/public/university/1/university-name.jpg.jpg
and other is storage/app/public/university/1/thumbnail/university-name.jpg
I play around with this for the whole day but no success.
if(Input::file())
{
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('profilepics/' . $filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$user->image = $filename;
$user->save();
}
Please someone can help me?
Thanks in advance.
I have been stuck in a problem. I am using another server for image uploading. Let me elaborate on the issue first.
When I delete an image manually from the media folder, now placeholder image is getting showed up.
Initially
catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/c/o/controlbar-white-small.jpg
After Deleting image manually
catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg
My requirement is my store should still fetch the same image Url as was before deletion as image path in the database is still there.
Please help.
This is a core magento behavior so that product without image will show the placeholder instead of a broken image, so unless you make a lot of changes to core image/helper in app/code/core/Mage/Catalog/Model/Product/Image.php the only other way is to change placeholder.
Have you think about just change the placeholder image?
In admin, under System > Configuration > Catalog > Product Image Placeholders, you can upload new file.
core_config_data table
I have resolve my issue . I am mentioning the solution.
go to OnePica_ImageCDN-1.0.9\OnePica\ImageCdn\Model\Adapter\Amazons3.php
Replace this
public function getUrl($filename)
{
$type = Mage::app()->getStore()->isCurrentlySecure() ? 'url_base_secure' : 'url_base';
$base_url = Mage::getStoreConfig('imagecdn/amazons3/' . $type);
$filename = $base_url . $this->getRelative($filename);
return str_replace('\\', '/', $filename);
}
by
public function getUrl($filename)
{
$type = Mage::app()->getStore()->isCurrentlySecure() ? 'url_base_secure' : 'url_base';
$base_url = Mage::getStoreConfig('imagecdn/amazons3/' . $type);
$product = Mage::registry('current_product');
$productId = $product->getId();
$resource = Mage::getSingleton('core/resource')->getConnection('core_write');
$imageName = $resource->fetchOne("select value from catalog_product_entity_varchar where attribute_id=86 && entity_id = ".$productId);
$filename = $base_url . $this->getRelative($filename);
$filename = substr($filename, 0, -22);
$filename = $filename.$imageName;
return str_replace('\\', '/', $filename);
}