artesaos/attacher

此包已被废弃,不再维护。未建议替换包。

附件并随意处理你的图片!适用于Laravel 5

v0.6.5.beta 2016-04-06 00:59 UTC

This package is auto-updated.

Last update: 2024-04-13 01:39:04 UTC


README

上传到S3,复制,本地,任何地方,操作并在你的模型中附加图片

当前构建状态

Code Climate Codacy Badge PullReview stats

统计信息

Latest Stable Version Total Downloads Latest Unstable Version License

Inssues Inssues Stars

技巧

Powered by ZenHub

安装

1 - 依赖

第一步是使用composer安装包并自动更新你的composer.json文件,你可以通过运行以下命令来实现:

composer require artesaos/attacher

或者手动更新你的composer.json文件

{
    "require": {
        "artesaos/attacher": "~0.6"
    }
}

2 - 提供者

你需要更新你的应用程序配置以便注册此包,使其可以被Laravel加载,只需更新你的config/app.php文件,在'providers'部分的末尾添加以下代码

// file START ommited
    'providers' => [
        // other providers ommited
        \Artesaos\Attacher\Providers\AttacherServiceProvider::class,
    ],
// file END ommited

3 - Facade

可选。你不需要注册Attacher的Facade,但如果你想使用一些快捷方式,可以自由使用它。

为了使用Attacher facade,你需要在config/app.php文件中注册它,你可以通过以下方式来实现:

<?php
# config/app.php

// file START ommited
    'aliases' => [
        // other Facades ommited
        'Attacher'   => \Artesaos\Attacher\Facades\Attacher::class,
    ],
// file END ommited

3.1 - Facade API

Attacher::process(Model $model);
Attacher::getPath();
Attacher::setPath($path);
Attacher::setBaseURL($url);
Attacher::getProcessor();
Attacher::getInterpolator();

4 - 配置

在你的控制台运行php artisan vendor:publish,现在你有3个新文件,config/attacher.phpconfig/flysystem.phpdatabase/migrations/2015_03_28_000000_create_attacher_images_table.php

Attacher需要graham-campbell/flysystem。不用担心,Attacher会自动为你注册flysystem服务。

config/app.php文件中,你可以配置目标路径和样式指南来操作图片。

return [
    'model'    => 'Artesaos\Attacher\AttacherModel', # You can customize the model for your needs.
    'base_url' => '', # The url basis for the representation of images.
    'path'     => '/uploads/images/:id/:style/:filename', # Change the path where the images are stored.

    'style_guides'   => [
        'default' => [
            # If you set the original style all other styles used his return to base
            'original'=> function($image)
            {
                return $image->insert('public/watermark.png');
            },

            # Generate thumb (?x500)
            'thumb' => function ($image) {
                $image->resize(null, 500, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });

                return $image;
            },
        ],
        'my_custom_style' => [
            # Generate thumb (460x120)
            'cover' => function ($image) {
                $image->fit(460, 120);

                return $image;
            }
        ],
    ]
];

用法

用法非常简单。图片目标信息位于flysystem配置文件config/flysystem.php中,在那里你定义了上传时使用的提供者。

1 - 基础

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload); # attach image
$image->save(); # now attacher process file (generate styles and save in your provider configured in flysystem)

echo $image->url('original');
echo $image->url('thumb'); // your style

1.1 - 使用样式指南

使用特定的样式指南来操作图片

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, 'custom_style'); # attach image using the "custom_style"
$image->save();

echo $image->url('cover'); // The "cover" setted in "my_custom_style" of the config/attacher.php file

你可以通过传递一个键为style guide和你要更改的style的数组来更改config/attacher.php中设置的样式。数组值应该是接收\Intervention\Image\Image的闭包实例

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, [
    'my_custom_style' => [
        # Generate thumb (30x30)
        'cover' => function ($image) {
            $image->fit(30, 30);

            return $image;
        }
    ]
]); # attach image using the "my_custom_style" changed by Closure
$image->save();

echo $image->url('cover'); // Now, the "cover" generates a resized image of 30 by 30 pixels

或者使用点表示法来更改样式

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, [
    'my_custom_style.cover' => function ($image) {
        $image->fit(30, 30);

        return $image;
    }
]); # attach image using the "my_custom_style" changed by Closure
$image->save();

echo $image->url('cover'); // Now, the "cover" generates a resized image of 30 by 30 pixels

2 - 特性

Attacher提供了两个特性,以简化使用morphManymorphOne技术创建与其它对象相关联的图片画廊/集合的创建

2.1 - HasImages

与多个图片绑定

#app/Project.php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Artesaos\Attacher\Traits\HasImage;

class Projects extends Model
{
    use HasImages;

    protected $table = 'projects';
}

////

$upload = Input::file('image');

$project = Projects::find(73);

$image = $project->addImage($upload); # Create a new image, save model and save image file with your styles

echo $image->url('thumbnail');

////

$project = Projects::find(73);

# Collection of images
$images = $project->images;

addImage()方法具有AttachModel中setupFile()方法相同的属性

$model->addImage(UploadedFile $image, $styleGuide = null, $type = null);

2.2 - HasImage [WIP]

与一个图片链接

#app/People.php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Artesaos\Attacher\Traits\HasImage;

class People extends Model
{
    use HasImage;

    protected $table = 'people';
}

////

$upload = Input::file('image');

$people = People::find(73);

$image = $people->addImage($upload); # Create a new image, save model and save image file with your styles

echo $image->url('thumbnail');

////

$people = People::find(73);

echo $people->image->url('original');

addImage()方法具有AttachModel中setupFile()方法相同的属性

$model->addImage(UploadedFile $image, $styleGuide = null, $type = null);

3 - 设置图片模型类型

有时你可能需要指定图片模型类型。例如,当产品有用于列表的图片和用于画廊的图片。为此,只需将额外的第三个参数传递给该方法

$people = People::find(73);

$upload = Input::file('image');
$people->addImage($upload, 'default', 'listing'); # attach image using the "listing" custom guide style

$upload2 = Input::file('image2');
$people->addImage($upload2, 'default', 'gallery'); # attach image using the "gallery" custom guide style

$listingImages = $people->images->ofType('listing'); // Get images of the listing
$galleryImages = $people->images->ofType('gallery'); // Get images of the gallery

作者

Vinicius Reis