I want to add a watermark to my uploaded image using intervention image library in laravel.I have installed it through command composer require intervention/image
, added Intervention\Image\ImageServiceProvider::class in providers array and 'Image' => Intervention\Image\Facades\Image::class in aliases array.
This is my code sample:
if($request->file1)
{
$this->validate($request, [
'file1' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
]);
$imageName = time().'-'.rand(11111, 99999).'.'.$request->file1->getClientOriginalExtension();
$imageName = $imageName->insert('https://www.exabytes.my/wp-content/uploads/400x150-exabytes-logo.png','center');
$request->file1->move('theme/img/properties', $imageName);
}
You have to actually use the Image class/facade:
$imageName = time().'-'.rand(11111, 99999).'.'.$request->file1->getClientOriginalExtension();
$file = $request->file1->move('theme/img/properties', $imageName);
$source = 'https://www.exabytes.my/wp-content/uploads/400x150-exabytes-logo.png';
Image::make($file->getPath())->insert($source, 'center');
Related
I'm trying to reduce the size of the images uploaded by users on a project I'm working on. The main idea is to resize the image down by 10%, and checking if its size is under a set requirement.
Here is my code: ($optimisedImage is the original image uploaded by the user and recieved from the request)
$optimisedImage->save("user_uploads/" . $scrollFeedName);
$scrollFeedImg = Image::make("user_uploads/" . $scrollFeedName);
while(filesize("user_uploads/" . $scrollFeedName) > 1000000) {
$scrollFeedImg->resize(floor($scrollFeedImg->width() * 0.9), floor($scrollFeedImg->height() * 0.9));
$scrollFeedImg->save();
}
Currently there seems to be an issue with the loop, getting stuck. I also tried the Intervention Image filesize(), but was getting an error stating that the width or heigth need to be higher than 0.
Is there any way to achieve my goal, or are there better solutions to that problem?
Stack:
PHP: 8.1.10
Laravel: 9.19
Step 1: Install intervention for resize image
composer require intervention/image
Step 2: Add provider path and alias path in config/app.php
return [
$provides => [
'Intervention\Image\ImageServiceProvider'
],
$aliases => [
'Image' => 'Intervention\Image\Facades\Image'
]
];
Step 3: Resize Image code for controller
public function resizeImagePost(Request $request) {
$this->validate($request, [
'title' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$image = $request->file('image');
$input['imagename'] = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/thumbnail');
$img = Image::make($image->getRealPath());
$img->resize(100, 100, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input['imagename']);
$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);
return back()
->with('success','Image Upload successful')
->with('imageName',$input['imagename']);
}
I am creating a controller that saves photos to the /storage/ folder. To protect myself from submitting a bunch of large photos and not to style their CSS, I wanted to resize them using the Intervention / image library. Unfortunately, despite following the installation instructions directly from the documentation, several uninstallations and reinstallations of the library do not work. When I use this code snippet:
Use Image;
I get an error saying:
Undefined type 'Image'
Following the instructions, I added the following to /config/app.php:
'providers' => [
...
Intervention\Image\ImageServiceProvider::class,
],
'aliases' => [
...
'Image' => Intervention\Image\Facades\Image::class,
...
],
Besides, I cleaned and reconfigured the cache and config, restarted the server, tried to use:
use Intervention\Image\ImageManagerStatic as Image;
But unfortunately that didn't help either.
What am I doing wrong?
You have to use the namespace below
use Intervention\Image\Facades\Image;
Then you can use like-
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$img = Image::make($image)->resize(300, 200)->save('storage/folder/filename'.'.'.$ext);
Use Image facade directly
\Intervention\Image\Facades\Image::make(\File::get($file_address))
->fit($width, $height)
->save($path_for_saving);
You can fit or crop the image based on your needs.
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
public function imageUploadPost(Request $request)
{
$photo = $request->file('image');
$imagename = time() . '.' . $photo->getClientOriginalExtension();
// Upload Crop Image...
$destinationPath = public_path('uploads/thumbnail_images');
if (!File::isDirectory($destinationPath)) {
File::makeDirectory($destinationPath, 0777, true, true);
}
$thumb_img = \Intervention\Image\Facades\Image::make($photo->getRealPath())->resize(100, 100);
$thumb_img->save($destinationPath . '/' . $imagename, 100); // Define Quality 100 (Optional)
echo '<pre>';
print_r("Upload Successfully. Store File : laravel_project/public/uploads & laravel_project/public/uploads/thumbnail_images");
}
I am Try to Upload User Avatar with Intervention Image as below private function
Everything working fine but image name save to database with my specific folder name
eg : user_avatar/image.jpg
Here is My Code Thanks everyone
private function storeAvatar($user)
{
if (request()->hasFile('avatar')) {
$user->update([
'avatar' => request()->avatar->store('user_avatar', 'public'),
]);
$avatar = Image::make(public_path('storage/' .$user->avatar))->fit(300, 300);
$avatar->save();
}
}
enter image description here
You're not setting a file name.
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
As indicated here: https://laravel.com/docs/7.x/filesystem#file-uploads
So your line should be:
$user->update([
'avatar' => request()->avatar->storeAs('user_avatar', 'filename', 'public'),
]);
Or something along those lines. You can also use the file name as uploaded by making the filename the same as the original.
$filename = $request->file('avatar')->getClientOriginalName();
I have a laravel upload for file (along with other data that is passed to the database) Everything works. But I just can't figure out how to save the path of the file that is saved.
Here is my controller function:
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'logo' => 'nullable',
'original_filename' => 'nullable',
]);
//This is where the file uploads?
if ($request->hasFile('logo')) {
$request->file('logo')->store('carrier_logo');
$request->merge([
'logo' => '',//TODO: get file location
'original_filename' => $request->file('logo')->getClientOriginalName(),
]);
}
Carrier::create($request->all());
return redirect()->route('carriers.index')->with('toast', 'Carrier created successfully.');
}
The thing I want to achieve:
I want logo to fill with something like carrier_logo/ZbCG0lnDkUiN690KEFpLrNcn2exPTB8mUdFDwAKN.png
The thing that happened every time I tried to fix it was that it placed the temp path in the database. Which ended up being something in the PHP install directory.
Just assign result to variable:
$path = $request->file('logo')->store('carrier_logo');
According to docs
Then you can do with $path variable whatever you want.
just assign the value like this.
$location=base_path('img/'.$filename);
and save it in db.
You could do this:
For FileName
$fileName = $request->file('test')->getClientOriginalName();
OR
$fileName = $request->user()->id.'.'.$request->file('logo')->getClientOriginalExtension();
$imageDirectory = 'logo_images';
$path = $request->file('logo')->storeAs($imageDirectory, $fileName);
dd($path);
I want to store the uploaded users image's path in db.Here is my code in controller,
$file = Input::file('pic');
$img = Image::make($file);
Response::make($img->encode('jpeg'));
$filepath = $file->getPathName();
But the above code results,
wrong path like C:\xampp\tmp\phpA0C8.tmpBt I've uploaded image from my laravel public folder.I've tried plenty of codes but they doesn't work.How do I solve this?Can anybody help?
Use laravel file store method and hashname
public function fileUPload(Request $request){
$file = $request->file('pic');
$dir = '/my_images/';
$file->store($dir, 'public_path'); //store images
echo $filePath= $dir . $file->hashName();
}
//config/filesystems.php
return [
///....
'disks' => [
'public_path' => [
'driver' => 'local',
'root' => public_path(),
],
//...
],
];
You have to define a path with the file name.
$FileName = Input::file('pic')->getClientOriginalName();
$FileDestinationPath = public_path('') .'/folder-name-in-public-folder/'.$FileName;
Or you can get it by using store method
$path = Input::file('pic')->store('folder-name');
return $path;
https://laravel.com/docs/5.5/filesystem
Use the store method
$filepath = $request->file('pic')->store('pictures');
more info
Place the code before the response