mr-timofey/laravel-aio-images

一体化 Laravel 图像处理

0.3.0 2020-05-02 15:02 UTC

This package is auto-updated.

Last update: 2024-09-29 05:08:04 UTC


README

本包包含以下内容

  • images 数据库表迁移;
  • images Eloquent 模型;
  • 上传和处理/缓存图像的控制器;
  • 服务提供者。

任何上传或生成的图像都自动使用 spatie/image-optimizer 包进行优化。

即时图像生成仅使用 intervention/image 包。

要求

  • PHP 7.1
  • Laravel 或 Lumen 5

安装

sudo apt-get install pngquant gifsicle jpegoptim optipng
composer require mr-timofey/laravel-aio-images

Laravel

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
php artisan vendor:publish --provider="MrTimofey\LaravelAioImages\ServiceProvider"
php artisan migrate

迁移将创建包含 json 字段 propsaio_images 表。如果你的数据库不支持 JSON/JSONB 字段,你可以将其更改为 text。尽管不推荐这样做。

如果您想使用 storage/app/public 作为存储所有图像的位置(默认配置)

php artisan storage:link

对于 Laravel <= 5.4,将 Intervention\Image\ImageServiceProviderMrTimofey\LaravelAioImages\ServiceProvider 添加到 app.providers 配置中。

有关进一步配置的说明,请参阅 config/aio_images.php 文件。不要忘记配置 aio_images.pipes

Lumen

Intervention\Image\ImageServiceProviderLumenMrTimofey\LaravelAioImages\ServiceProvider 服务提供者添加到 bootstrap/app.php

config.php 的内容复制到 config/aio_images.php

创建迁移 php artisan make:migration create_aio_images_table 并将内容从 此处 复制到刚创建的迁移文件(database/migrations/xxxx_xx_xx_xxxxxx_create_aio_images_table)。

有关进一步配置的说明,请参阅 config/aio_images.php 文件。不要忘记配置 aio_images.pipes

预定义路由

  • route('aio_images.upload')POST multipart/form-data - 图像上传处理程序。支持多个和单个图像上传。字段名称不重要,因为控制器只是使用 Illuminate\Http\Request@allFiles() 来获取您的上传。
  • route('aio_images.original', $image_id) - 原始图像路径。
  • route('aio_images.pipe', [$pipe, $image_id]) - 处理后的图像路径。

用法示例

// add relation to a table
/** @var Illuminate\Database\Schema\Blueprint $table */
$table->string('avatar_image_id')->nullable();
$table->foreign('avatar_image_id')
	->references('id')
	->on('aio_images')
	->onDelete('set null');


// add relation to a model
public function avatarImage()
{
	$model->belongsTo(ImageModel::class, 'avatar_image_id');
}


// create pipe config in config/aio_images.php
[
	// ...
	'pipes' => [
		// /storage/images/avatar/image-id.jpg
		'avatar' => [
			// $interventionImage->fit(120)
			['fit', 120],
			// $interventionImage->greyscale()
			['greyscale']
		]
	]
];

// display original avatar
echo '<img src="' . route('aio_images.original', ['image_id' => $model->avatar_image_id]) . '" alt="Original avatar" />';
// display 120x120 squared grey colored avatar
echo '<img src="' . route('aio_images.pipe', ['pipe' => 'avatar', 'image_id' => $model->avatar_image_id]) . '" alt="Processed with pipe [avatar]" />';

// same with ImageModel instance
echo '<img src="' . $image->getPath() . '" alt="Original avatar" />';
echo '<img src="' . $image->getPath('avatar') . '" alt="Processed with pipe [avatar]" />';


// upload image manually from any of your custom controllers

use Illuminate\Http\Request;
use MrTimofey\LaravelAioImages\ImageModel;

function upload(Request $req)
{
	return ImageModel::upload($req->file('image'));
}