图像处理和上传模块

0.3.5 2016-08-13 14:16 UTC

README

Build Status

这是一个基于Imagine模块的图像处理模块。它是为Laravel 5.* PHP框架设计的。

安装

使用标准的Composer方法

composer require todstoychev/icr

或在您的composer.json中添加

"require": {
    ...
    "todstoychev/icr": "dev-master",
    ...
    }

然后运行

composer update

配置

在您的Laravel config/app.php'providers'部分添加:Todstoychev\Icr\ServiceProvider::class,.

'aliases'中您可以添加:'Icr' => \Todstoychev\Icr\Icr::class,

运行php artisan vendor:publish --provider="Todstoychev\Icr\ServiceProvider"以发布配置,或者您也可以使用php artisan vendor:publish --tag=icr

您还需要为Laravel的存储模块设置一个新的设置。这可以在config/filesystem.php中完成。您可以创建类似以下内容:

'local' => [
    'driver' => 'local',
    'root'   => public_path('/uploads/images'),
],

将此内容放在文件的disks部分。

配置文件

该文件位于config/中,名为icr.php。该文件包含多个部分和参数。

image_adapter

这是设置用于处理图像的驱动库的设置。Imagine支持Gd、Imagick和Gmagick,可用的值是gd、imagick或gmagick。

上下文

这些都是上下文设置。每个上下文由一个数组表示。默认的一个看起来像这样:

'default' => [ 
        'small' => [
            'width' => 100,
            'height' => 100,
            'operation' => 'resize',
        ],
        'medium' => [
            'width' => 300,
            'height' => 300,
            'operation' => 'resize',
        ],
        'large' => [
            'width' => 600,
            'height' => 600,
            'operation' => 'resize',
        ],
    ],

每个上下文可以定义不同的尺寸。尺寸数组有3个参数:宽度、高度和操作。

'small' => [
            'width' => 32,
            'height' => 32,
            'operation' => 'resize-crop',
        ],

允许的操作有裁剪、调整大小、缩放、调整大小-裁剪。

  • 裁剪 - 从提供的图像中心裁剪区域;
  • 调整大小 - 调整图像到给定尺寸,同时保持图像比例;
  • 缩放 - 将图像缩放到给定尺寸;
  • 调整大小-裁剪 - 首先调整图像大小以保持原始比例,然后从图像中心裁剪一个给定大小的区域。

方法

要使用此模块,请调用其基本类 - Icr。该类包含2个方法。一个用于上传图像,另一个用于删除。另一种使用方法是实例化Todstoychev\Icr\Processor

上传图像

Icr::uploadImage()方法在成功时返回文件名。如果出现任何错误,则返回异常类实例。您可以在控制器方法中使用类似的内容

$response = Icr::uploadImage($request->file('image'), 'my_context', 'images');

if ($response instanceof \Exception) {
    // Handle the error
} else {
    // Save the image name to database. Example: $myModel->saveImage($response);
}

第一个参数应使用请求中的文件,第二个参数是上下文。第三个参数是您的config/filesystem.php中的设置名称。

删除图像

这可以通过Icr::deleteImage()来完成。示例

$response = Icr::deleteImage('my_file_name', 'my_context', 'images');

if ($response instanceof \Exception) {
    // Handle the error
} else {
    // Delete the image name from database. Example: $myModel->deleteImage($response);
}

参数与uploadImage()方法相同,除了第一个参数是应删除的文件的文件名。