daycode / stup-images
Stup Images 或 存储或更新图片 是一个用于存储/更新图片的包,具有更清晰的代码和更高的可读性
v1.0.1
2023-07-25 03:28 UTC
Requires
- intervention/image: ^2.7
This package is auto-updated.
Last update: 2024-09-26 10:03:29 UTC
README
Stup Images 或 存储或更新图片 是一个用于存储/更新图片的包,具有更清晰的代码和更高的可读性。
安装指南
composer require daycode/stup-images
更新到最新的 laravel 项目供应商
composer update
最后,为了确保此包正确安装。
composer dump-autoload && php artisan optimize:clear
因为此包使用存储路径作为默认路径,您必须在公共文件夹和存储文件夹之间建立链接
php artisan storage:link
使用说明
您必须在代码顶部使用这些类
use DayCod\StupImages\StupImages; use DayCod\StupImages\Functions\StupImageFunctions;
然后,对于使用,让我们使用这些函数
(new StupImages($filename, $path, $width, $height))->StupImagesToStorage($new_image_file, $old_image_file = null)
以下是你必须遵循的规则
$filename = "Filename cannot be an empty string ``"; $path = "Path cannot be an empty string ``"; $width = "Width input must be an integer"; $width = "Width Basically is Dimension and Dimension would`nt be lower than zero or negative"; $height = "Height input must be an integer"; $height = "Height Basically is Dimension and Dimension would`nt be lower than zero or negative";
如果您想获取文件名,请使用这个
(new StupImageFunctions())->getFileName($old_image_file); // $old_image_file is a value from the database table where you stored at
使用示例
存储到数据库示例
这绝对很好,但我们还可以改进。
public function store(Request $request): RedirectResponse { $data = [ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), ]; $image = $request->file('images'); $imageName = hexdec(uniqid()).'.'.$image->getClientOriginalExtension(); $dir = 'upload/images/'; $save_path = 'upload/images/'.$imageName; Image::make($realImage)->resize($width, $height)->save(public_path($save_path)); $data['user_profile_img'] = $save_path; User::create($data); return redirect()->back()->with('success', 'User Successfully Created'); }
为此,
public function store(Request $request): RedirectResponse { User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), 'user_profile_img' => (new StupImages('user-profile', 'profile', 300, 300))->StupImagesToStorage($request->file('user_profile_img')), ]); return redirect()->back()->with('success', 'User Successfully Created'); }
更新到数据库示例
这绝对很好,但我们还可以改进。
public function update(Request $request, $id): RedirectResponse { $data = [ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), ]; $user = User::findOrFail($id); if ($request->file('images')) { $image = $request->file('images'); $imageName = hexdec(uniqid()).'.'.$image->getClientOriginalExtension(); $dir = 'upload/images/'; $save_path = 'upload/images/'.$imageName; Image::make($realImage)->resize($width, $height)->save(public_path($save_path)); $data['images'] = $save_path; $user->update($data); } else { $user->update($data); } return redirect()->back()->with('success', 'User Successfully Updated'); }
为此
public function update(Request $request, $id): RedirectResponse { $user = User::find($user_id); $user->update([ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), 'user_profile_img' => (is_null($request->file('user_profile_img'))) ? $user->fresh()->user_profile_img : (new StupImages('user-profile', 'profile', 300, 300)) ->StupImagesToStorage($request->file('user_profile_img'), $user->fresh()->user_profile_img), ]); }