Create a thumb and original image - laravel

in my controller very simply I load an image like this:
// upload original
$path = $request->file('thumb')->store('thumbs');
However, I want to create a thumbnail of predefined size (for example 100x100), using the same name created automatically .. adding only "_thumb" to the name.
I use the library http://image.intervention.io/getting_started/introduction

You have to use following code
Adding this in header section
use ImageResize;
And in your controller use following code
$photoThumbnail = ImageResize::make($request->file('thumb'))
->resize(100, 100, function ($constraint) { $constraint->aspectRatio(); } )
->encode('jpg',100);
Storage::disk('thumbnail')->put("your_image_name", $photoThumbnail);
In your app/config folder filesystems.php add this configuration
'thumbnail' => [
'driver' => 'local',
'root' => storage_path('app/public/thumbnail'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
You have to configure also app\config folder app.php file provider section
Intervention\Image\ImageServiceProvider::class,
and aliases section use
'ImageResize' => Intervention\Image\Facades\Image::class,
For More Details

Related

Issue displaying images with Laravel file manager. Routes question

I'm messed with the paths to get UniSharp / laravel-filemanager working on the server. In local mode it works perfectly but now I'm making the change of routes for production in online mode and I am not clear at all.
The problem is that it loads the images but they are not displayed either on the page or in the filemanager itself. In filemanager, I can see a red square with a cross (as error).
Let's see if anyone knows how to write the correct routes. I have tried many things but I'm messed.
The server files scaffolding:
server_files
my_laravel_app
public_html
another_files
The config filesystem.php file in Laravel:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('public_html'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
I have, default uploading path in filemanager's lfm.php:
'disk' => 'public',
In case it helps something, this is my index.php ubicated at public_html:
if (file_exists(__DIR__.'/../my_app_laravel/storage/framework/maintenance.php')) {
require __DIR__.'/../my_app_laravel/storage/framework/maintenance.php';
}
require __DIR__.'/../my_app_laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../my_app_laravel/bootstrap/app.php';
And this is ServiceProviders.php
public function register()
{
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
How should be the correct path in filesystem to allow filemanager can access to images and display them?
Note: the public_html folder has the symbolic link as you can see at filesystem.php.
I have checking routes and I solved temporary:
I changed:
'public' => [
'driver' => 'local',
/* this */ 'root' => storage_path('app/public'),
/* this */ 'url' => env('APP_URL').'/storage/app/public',
'visibility' => 'public',
],
And now I can upload and see the images in file manager and inside the web app. But now the problem is that users can visit the image url and see the private route of the folder:
http//mi_lar_app/storage/app/public/photos/31/art_1/fig_1.0.png
Some help????
If you have similar problems, the procedure I do in this case was to create a symbolic link with routes (I created a php file because I haven't access to SSH on my server):
<?php
/*__DIR__ is the directory file, where you save this file.php */
$mytargetDIR = __DIR__.'/../mi_lar_app/storage';
$mylinkDIR = __DIR__.'/storage';
symlink($mytargetDIR,$mylinkDIR);
echo 'Todo ok/ Symlink process successfully completed';
?>
And important, remember clean cache and routes every changes. If you don't have SSH access:
<?php
Artisan::call('cache:clear');
Artisan::call('config:cache');
Artisan::call('route:cache');
Artisan::call('view:clear');
return 'Todo limpio/All is cleaned';
?>
And next this little explanation, some help to me?? :)

cant store the file using storeAs in laravel

iam using a laravel site and in my file i have a directory in path
/var/www/site/admin.site.com/public/storage/salescall
i need to upload a file to this salescall folder i have used the following code.
$filenameWithExt = $request->file('sales_call')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('sales_call')->getClientOriginalExtension();
$filenameToStore = $filename . '-' . time() . '.' . $extension;
$request->file('sales_call')
->storeAs('', $filenameToStore, 'sales');
and my filesystems.php is as follows
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
'sales' => [
'driver' => 'local',
'root' => public_path('salescall'),
'visibility' => 'public',
],
],
file is not uploaded to folder.please help me to solve this. Thank you in advance.
You can easily do this:
$request->file('sales_call')-> storeAs('salescall/',$filenameToStore);
FOR BETTER UNDERSTANDING, SEE BELOW
The configuration of your filesystem seems to be incorrect.
Use the below sample as a guide and I hope it helps you.
If you use this code, it will upload your file in storage/app folder
Storage::disk('local')->put('file.txt', 'Contents');
Now, if you need to change the directory and store into the storage folder directly, you need to change something in the filesystems.php file.
Go to config/filesystems.php file and change the following code-
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
Here, this line of code 'root' => storage_path('app'), responsible to define where to store. You just adjust according to your demands.
Alternatively, In your controller, You could do something like this:
<?php
namespace App\Http\Controllers\Api;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class UserAvatarController extends Controller
{
/**
* Handle the incoming request.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$request->validate([
'avatar'=> ['required','max:200']
]);
$user = User::findOrFail(auth()->user()->id);
// Handle file Upload
if($request->hasFile('avatar')){
//Storage::delete('/public/avatars/'.$user->avatar);
// Get filename with the extension
$filenameWithExt = $request->file('avatar')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('avatar')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('avatar')->storeAs('public/avatars',$fileNameToStore);
$user->avatar = $fileNameToStore ;
$user->save();
}
return $user;
}
}
But in your own case, I believe:
In your config, set the root key to storage_path('salescall')
Yours should be $path = $request->file('sales_call')->storeAs('public/salescall',$fileNameToStore);
Just let me know if this works.. Cheers!
You have to use like this
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
$request->file('sales_call')-> storeAs('salescall/',$filenameToStore);

i cannot access to file content on public disk

On Laravel 8
file_get_contents(asset('storage/list.json');
Give me:
ErrorException
file_get_contents(http://localhost:8000/storage/list.json): failed to
open stream: HTTP request failed!
But: http://localhost:8000/storage/list.json exist and it is accesible via browser for example.
On my config/filesystem.php iv'e:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
And ive created the symbolic link with:
php artisan storage:link
command.
I've tried even to add manually file perssion to storage folder as described in the filesystem.php but nothing change.
You should use absolute paths when you want to work with files. file_get_contents() is fine. There are some file wrappers available in laravel but internally they all use file_get_contents().
Try
file_get_contents(public_path(asset('storage/list.json')));
If getting from storage file try
file_get_contents(storage_path('app/assets/list.json'));
See https://laravel.com/docs/5.5/helpers#method-resource-path

Not delete image from laravel backpack 4.0

I'm using image field in laravel backpack 4.0 and it uploads the images without any problems. When I delete the image by using the delete button, it deletes the register (E.N. Probably means "it deletes the image from the database"), but not the image file from my local folder. I've checked the answer from backpack for laravel deleting image , but it did not help to fix my issue.
My config/filesystem:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
My Model code:
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = config('backpack.base.root_disk_name'); // or use your own disk, defined in config/filesystems.php
$destination_path = env('FOLDER_PUBLIC')."/uploads/medias"; // path relative to the disk above
// 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 = rand ( 10000 , 99999 ).'-'.strtolower(trim(preg_replace('/[\s-]+/', '-', preg_replace('/[^A-Za-z0-9-]+/', '-', preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $this->title))))), '-')).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the public path to the database
// but first, remove "public/" from the path, since we're pointing to it from the root folder
// that way, what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst(env('FOLDER_PUBLIC').'/', '', $destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
}
}
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('public')->delete($obj->image);
});
}
I have tried to change:
\Storage::disk('public')->delete($obj->image);
With:
\Storage::disk(config('backpack.base.root_disk_name'))->delete($obj->image);
But it is not working either,
Can anyone help me?
Sorry for my english
You're on the right track. Looks like something is misconfigured if code in boot() doesn't affect any changes. My guess is that that the $obj->image path to the file you're trying to delete is NOT the exact path to the file. You might need to add the destination folder there too.
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
$disk = config('backpack.base.root_disk_name');
$destination_path = env('FOLDER_PUBLIC')."/uploads/medias";
\Storage::disk($disk)->delete($destination_path.'/'.$this->image);
});
}
If this works, to further improve the code, I recommend you turn the $disk and $destination_path variables into properties on the PHP class itself (the Laravel model). That way, you only define them in one place, and you can then use them in both methods, with something like $this->imageDisk and $this->imagePath.
Thanks for your reply, At the end I have fixed it, making this change:
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('uploads')->delete($obj->image);
});
}
And in config/filesystem:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
'uploads' => [
'driver' => 'local',
/* 'root' => base_path().'/web/storage',*/
'root' => base_path().'/'.env('FOLDER_PUBLIC'),
],
],
Regards

Laravel 5.5 - Upload to public folder

I'm trying to store a file in the public folder storage/app/public/ but for some reason Laravel just seems to put it in the private storage/app/ folder.
If I understand correctly I'm supposed to just set the visibility to 'public' but that doesn't seem to change anything:
Storage::put($fileName, file_get_contents($file), 'public');
When I call getVisibility I get public so that seems to work fine:
Storage::getVisibility($fileName); // public
These are the settings in my filesystems.php:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
When you call Storage::put, Laravel will use the default disk which is 'local'.
The local disk stores files at its root: storage_path('app'). The visibility has nothing with where the file should be stored.
You need to choose the public disk which will store the files at its root: storage_path('app/public'),
To do that, you need to tell Laravel which disk to use when uploading the file. Basically change your code to this:
Storage::disk('public')->put($fileName, file_get_contents($file), 'public');

Resources