baki/image

存储图片

dev-master 2020-09-29 07:23 UTC

This package is auto-updated.

Last update: 2024-09-29 05:01:11 UTC


README

使用composer安装此包

composer require baki/image

用法

要使用Image包,在控制器中使用如下代码

use StoreImage;

之后,在config/filesystems.php中创建你的磁盘

'yourDisk' => [
            'driver' => 'local',
            'root' => storage_path('app/public/your-directory'),
            'url' => env('APP_URL').'/your-directory',
            'visibility' => 'public',
        ],

示例

如果你想要保存你的图片(例如,用于新闻)并获取这些图片的名称
请遵循以下命令

{{-- Input in laravel blade --}}
<input type="file" name="your_name_for_input">

$news->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg', false(for store), $news->old_image_from_database(enter if the previous parameter is true), 'width', 'height');

// Function to store your news and pictures
public function store(Request $r)
{
    $input = new News;
    
    // It is without crop image
    $input->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg');
    
    // It is with crop image
    $input->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg', false, '', 100, 100);
    
    $input->save();
    return redirect()->route('news.index'); // This is my route. You can use your route.
}

如果你想要编辑新闻。旧图片将被删除,但默认图片将不会被删除
请遵循以下命令

$news->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg', true(for edit), $news->old_image_from_database(enter if the previous parameter is true), 'width', 'height');

// Function to update your news and pictures
public function update(Request $r, News $news)
{
    // It is without crop image
    $news->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg', true, $news->old_image_from_database);
    
    // It is with crop image
    $news->image = StoreImage::store('your_input_name', 'yourDisk', $request, 'default-pictures.jpg', true, $news->old_image_from_database, 'width', 'height');
    
    $news->save();
    return redirect()->route('news.index'); // It is my route. You can use your route.
}