marcohern/dimages

上传图片并按不同密度和尺寸下载

2.2.1 2020-08-18 03:31 UTC

This package is auto-updated.

Last update: 2024-09-18 11:45:26 UTC


README

为Laravel提供简单的图片管理。如果您正在开发为移动应用提供的Laravel API,dimages可以根据图片用途和设备密度重新采样图片,非常有用。

安装

下载并安装laravel。

$ laravel new app1
$ cd app1

Dimages允许公众下载图片。但是,上传图片只能通过认证进行,通过auth:api中间件的方式。实现API认证的最简单方法是使用laravel/passport。注意:laravel/passport需要一个数据库,所以请确保您已经设置了一个。

因此,下载、安装并配置laravel passport

$ composer require laravel/passport

这将安装passport及其所有依赖项。

$ php artisan passport:install

您将得到类似以下输出

Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Password grant client created successfully.
Client ID: 2
Client secret: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

这创建了2个授权用于登录。第一个是一个个人访问客户端,我们现在可以忽略它。第二个是一个密码授权,这是我们将会使用的。所以请记住第二个客户端密钥,我们将将其用作类似API密钥的东西来登录API。现在,同时,您需要创建一个用户来登录,让我们创建一个seeder来添加一个用户。

$ php artisan make:seeder UserSeeder
Seeder created successfully.

这将在数据库/seeds文件夹中创建seeder类,让我们添加代码

<?php

use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
  protected $users = [
    ['name' => 'Son Goku', 'email' => 'goku@dbz.com', 'password' => 'goku'],
    ['name' => 'Bulma', 'email' => 'bulma@dbz.com', 'password' => 'bulma'],
    ['name' => 'Master Roshi', 'email' => 'master.roshi@dbz.com', 'password' => 'masterroshi'],
    ['name' => 'Yamcha', 'email' => 'yamcha@dbz.com', 'password' => 'yamcha'],
    ['name' => 'Krillin', 'email' => 'krillin@dbz.com', 'password' => 'krillin'],
    ['name' => 'Tien Shinhan', 'email' => 'tien.shinhan@dbz.com', 'password' => 'tienshinhan'],
    ['name' => 'Picollo', 'email' => 'picollo@dbz.com', 'password' => 'picollo']
  ];

  public function run()
  {
    foreach ($this->users as $k => $u) {
      $now = (new \Datetime("now"))->format('Y-m-d H:i:s');
      $this->users[$k]['email_verified_at'] = $now;
      $this->users[$k]['created_at'] = $now;
      $this->users[$k]['updated_at'] = $now;
      $this->users[$k]['password'] = bcrypt($u['password']);
    }

    DB::table('users')->insert($this->users);
  }
}

代码包含了一堆用户用于测试,我们可以使用这些中的任何一个来登录。要运行seeder,请运行以下命令

$ php artisan db:seed --class=UserSeeder
Database seeding completed successfully.

因此,现在passport正在运行,并且有用户可以登录。现在我们可以使用composer安装marcohern/dimages

$ composer require marcohern/dimages

在此阶段,库已安装。但您需要安装配置文件。

运行以下命令

$ php artisan vendor:publish

将显示一个发布列表,选择标记为config的选项。

在此时刻,您将看到以下输出信息

Copied File [\vendor\marcohern\dimages\publishables\config\dimages.php] To [\config\dimages.php]

这意味着配置已经部署。最后,通过键入以下命令

$ php artisan route:list --columns=method,uri

将显示一个路由列表。如果出现带有前缀dimagedimaagesdimagesettings的路由,则表示库正在运行并已安装。

+----------+--------------------------------------------------------------------+
| Method   | URI                                                                |
+----------+--------------------------------------------------------------------+
| GET|HEAD | /                                                                  |
| GET|HEAD | api/user                                                           |
| POST     | dimage/attach/{tenant}/{session}/{entity}/{identity}               |
| POST     | dimage/stage/{tenant}/{session}                                    |
| POST     | dimage/{tenant}/{entity}/{identity}                                |
| DELETE   | dimage/{tenant}/{entity}/{identity}                                |
| GET|HEAD | dimage/{tenant}/{entity}/{identity}/{index?}                       |
| DELETE   | dimage/{tenant}/{entity}/{identity}/{index}                        |
| POST     | dimage/{tenant}/{entity}/{identity}/{index}                        |
| GET|HEAD | dimage/{tenant}/{entity}/{identity}/{profile}/{density}/{index?}   |
| GET|HEAD | dimages                                                            |
| GET|HEAD | dimages/session                                                    |
| GET|HEAD | dimages/status                                                     |
| GET|HEAD | dimages/{tenant}                                                   |
| GET|HEAD | dimages/{tenant}/{entity}                                          |
| POST     | dimages/{tenant}/{entity}/{identity}/normalize                     |
| GET|HEAD | dimages/{tenant}/{entity}/{identity}/sources                       |
| POST     | dimages/{tenant}/{entity}/{identity}/switch/{source}/with/{target} |
| GET|HEAD | dimagesettings/{tenant}                                            |
| POST     | dimagesettings/{tenant}/density                                    |
| DELETE   | dimagesettings/{tenant}/density/{density}                          |
| POST     | dimagesettings/{tenant}/profile                                    |
| DELETE   | dimagesettings/{tenant}/profile/{profile}                          |
| POST     | dimagesettings/{tenant}/reset                                      |
+----------+--------------------------------------------------------------------+

现在您已经准备好使用该库。

使用dimages

dimages公开了一些端点,帮助您管理图片,并为特定配置文件和密度下载它们。

为了确保以JSON格式获取结果,请确保添加头部信息 Accept: application/json

上传一个或多个图片

POST /dimage/{tenant}/{entity}/{identity}

示例

POST /dimage/john-doe/games/death-stranding

参数

tenant:图片的用户或租户,可以是代码、slug或用户名。例如 mikeuser1234

entity:图片的实体。例如 useruser-profilealbums

identity:与图片相关联的对象的引用,例如用户名或slug。示例:john-doemy-album-2020-01-22

image:(在主体中) 在名为 image 的字段中上传文件。Content-Type必须是 application/x-www-form-urlencoded

返回值

{
  "index": 0
}

每次请求只能上传一张图片。如果您上传了多张图片(通过多个请求),每张图片将返回一个索引号,第一个为0,然后为1,依此类推。

下载上传的图片

GET /dimage/{tenant}/{entity}/{identity}/{index?}

示例

GET /dimage/john-doe/games/death-stranding
GET /dimage/john-doe/games/death-stranding/0
GET /dimage/john-doe/games/death-stranding/2

参数

tenant:图片的用户或租户,可以是代码、slug或用户名。例如 mikeuser1234

entity:图片的实体。例如 useruser-profilealbums

identity:与图片相关联的对象的引用,例如用户名或slug。示例:john-doemy-album-2020-01-22

index:(可选) 图片的索引。如果没有指定索引,则使用零(0)。

返回值

返回原始大小的源图片。

以不同大小下载上传的图片

GET /dimage/{tenant}/{entity}/{identity}/{profile}/{density}/{index?}

示例

GET /dimage/john-doe/games/death-stranding/icons/ldpi
GET /dimage/john-doe/games/death-stranding/launcher-icons/mdpi/0
GET /dimage/john-doe/games/death-stranding/ref/hdpi/2

参数

tenant:图片的用户或租户,可以是代码、slug或用户名。例如 mikeuser1234

entity:图片的实体。例如 useruser-profilealbums

identity:与图片相关联的对象的引用,例如用户名或slug。示例:john-doemy-album-2020-01-22

配置文件:图像的配置文件。配置文件本质上是指图像将用于什么,例如,作为一个 图标 或一个 封面。它基本上定义了 长宽比

密度:图像上附加对象的参考,如用户名或别名。例如:john-doemy-album-2020-01-22

index:(可选) 图片的索引。如果没有指定索引,则使用零(0)。

返回值

适当大小的图像。

配置图像大小

可以在 config/dimages.php 文件中配置图像大小。有一些初始设置。

return [
  'densities' => [
    'ldpi'    => 0.75,
    'mdpi'    => 1.00,
    'hdpi'    => 1.50,
    'xhdpi'   => 2.00,
    'xxhdpi'  => 3.00,
    'xxxhdpi' => 4.00,

    'single'  => 1.00,
    'double'  => 2.00,
  ],
  'profiles' => [
    'ref'                => [480, 320],
    'launcher-icons'     => [48, 48],
    'actionbar-icons'    => [24, 24],
    'small-icons'        => [16, 16],
    'notification-icons' => [22, 22],
  ]
];

密度:每像素英寸的密度列表。这将用于计算具有指定密度的任何请求的图像大小。如果您愿意,可以添加新的密度乘数。

配置文件:像素中的图像大小列表。这是密度等于 1(mdpi)时图像的大小。大小通过将指定密度乘以指定配置文件中每个维度的指定值来计算。原则上,随着您的应用需求,您将添加更多配置文件。

示例

Request    Profile Size        Density Mult  Requested Size
ref/ldpi   ref     [480, 320]  ldpi    0.75  [360, 240]
ref/mdpi   ref     [480, 320]  mdpi    1.00  [480, 320]
ref/hdpi   ref     [480, 320]  hdpi    1.50  [720, 480]
ref/xhdpi  ref     [480, 320]  xhdpi   2.00  [950, 640]
ref/xxhdpi ref     [480, 320]  xhdpi   3.00  [1440, 960]

删除图像

DELETE /dimage/{tenant}/{entity}/{identity}/{index?}

示例

# Delete a single image
DELETE /dimage/john-doe/games/death-stranding/1

# Delete all images associated with identity
DELETE /dimage/john-doe/games/death-stranding

参数

租户:图像的用户或租户。

实体:图像的实体。

身份:身份。

索引:(可选)图像的索引。具有匹配索引的图像将被删除。如果没有指定,将与身份关联的所有图像都将被删除。

其他可用端点

#Update an image
POST dimage/attach/{tenant}/{session}/{entity}/{identity}

#Upload an image into staging
POST dimage/stage/{tenant}/{session}

#Upload an image directly to an identity
POST dimage/{tenant}/{entity}/{identity}

#Delete all images associated to an identity
DELETE dimage/{tenant}/{entity}/{identity}

#Get a source image
GET dimage/{tenant}/{entity}/{identity}/{index?}

#Delete a single image and all its derivatives
DELETE dimage/{tenant}/{entity}/{identity}/{index}

#Update an existing image
POST dimage/{tenant}/{entity}/{identity}/{index}

#Get a derivative image
GET dimage/{tenant}/{entity}/{identity}/{profile}/{density}/{index?}

#Get list of tenants
GET dimages

#Get a usable session Id
GET dimages/session

#get status
GET dimages/status

#Get list of entities
GET dimages/{tenant}

#Get list of identities
GET dimages/{tenant}/{entity}

#Remove any index gaps if they exists
POST dimages/{tenant}/{entity}/{identity}/normalize

#Get list of existing source images
GET dimages/{tenant}/{entity}/{identity}/sources

#Switch an index to another. If target image allready exists, switch them.
POST dimages/{tenant}/{entity}/{identity}/switch/{source}/with/{target}

# Get tenant settings
GET dimagesettings/{tenant}

# Add or Update density entry
POST dimagesettings/{tenant}/density
{
  "name": "the_density",
  "value": 2.00
}

# Delete density entry
DELETE dimagesettings/{tenant}/density/{density}

# Add or Update profile entry
POST dimagesettings/{tenant}/profile
{
  "name": "the_profile",
  "width": 300,
  "height": 400
}

# Delete profile entry
DELETE dimagesettings/{tenant}/profile/{profile}

#Reset settings to default
POST dimagesettings/{tenant}/reset

开发环境的安装

要为 dimages 开发,请按照以下步骤操作。

  1. 安装一个新的 Laravel 应用程序,您将使用它来运行和测试您的开发。让我们称它为 laravel_packages
  2. 在根目录中,创建一个 packages/marcohern 文件夹。
  3. packages/marcohern 文件夹中,在 dimages 文件夹中检出 dimages 源。最后,克隆仓库的路径应该是:/path/to/your/workspace/laravel_packages/marcohern/dimages/

在这个阶段,您必须在 Laravel 中注册此包

  1. 将 DimagesServiceProvider 添加到 config/app.php 文件中的 providers 列表。
  'providers' => [
    ...
    
    /*
     * Package Service Providers...
     */
    Marcohern\Dimages\DimagesServiceProvider::class,
    
    ...
  ]    
  1. composer.json 文件的 autoload 部分中包含包源文件。在 psr-4 列表中包含 dimages 命名空间/源文件夹,并在 files 列表中包含 Helper.php
{

  "autoload":{
  
    "psr-4": {
      "App\\": "app/",
      "Marcohern\\Dimages\\": "packages/marcohern/dimages/src"
    },
    "files": [
      "packages/marcohern/dimages/src/Helpers.php"
    ]
  }
  
}
  1. 最后,运行 composer dump-autoload
.../laravel_packages>composer dump-autoload

到这一点,包模块应该可以正常工作。通过打开模块的主要路由来测试它。

  1. 在 Laravel 项目上运行 php artisan serve
.../laravel_packages>php artisan serve
  1. 打开浏览器并使用以下 url 访问包模块:https://:8000/mh/dim/api/status。这应该返回 200 和一个包含 success = true 的 json。您已经设置好了!