mike-jc/image-bot

用于调整图片大小并将其上传到存储的机器人

安装: 2

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 0

开放问题: 1

类型:项目

1.0.0 2017-06-05 06:48 UTC

This package is auto-updated.

Last update: 2024-09-22 18:25:45 UTC


README

机器人可以(重新)安排、调整大小并将图片存储到云端。

工作流程应分为以下独立步骤

  • 安排待处理图片的列表。
  • 调整安排的图片大小。
  • 将调整大小的图片上传到云端存储。

安装

通过Composer

运行

$ php composer require mike-jc/image-bot

之后您可以运行任何其他机器人命令

$ php vendor/bin/bot schedule ./images
$ php vendor/bin/bot resize -n 10
$ php vendor/bin/bot upload

或者作为Docker镜像

运行以下命令

$ mkdir sandbox && cd sandbox
$ curl -sS https://raw.githubusercontent.com/mike-jc/ImageBot/master/Dockerfile > Dockerfile 
$ docker build -t bots/image-bot .
$ docker run -it --volume=<absolute-path-to-images-directory>:/images --workdir="/images" --entrypoint=/bin/bash bots/image-bot

之后Docker容器将运行,并且您可以进入其中。

在运行的Docker容器中

$ vim /home/ImageBot/config/config.yml

... edit configuration (at least, add cloud storage credentials)

$ /etc/init.d/rabbitmq-server start

... now you're in images directory, so:

$ bot schedule ./ 
$ bot resize

... use any other bot command you want

配置

配置放在config/config.yml中,必须以YAML格式编写。

您至少需要选择云存储并添加其凭据。目前支持以下云存储

  • Amazon S3
  • Dropbox
  • Google Drive

Amazon S3配置

    type: amazon-s3
    parameters:
        region: YOUR_REGION_IN_AMAZON
        bucket-name: YOUR_BUCKET_NAME # by default: `default-bucket`
    credentials:
        key: YOU_CLIENT_KEY
        sercret: YOU_CLIENT_SECRET

Dropbox配置

storage:
    type: dropbox # can be: amazon-s3 for Amazon S3 storage, dropbox for Dropbox, g-drive for Google Drive
    credentials: # different structure for each storage type
        key: YOUR_CLIENT_KEY
        secret: YOUR_CLIENT_SECRET
        access-token: YOUR_DEVELOPER_ACCESS_TOKEN

Google Drive配置

    type: g-drive
    credentials:
        config-file: PATH_TO_FILE  # file with credentials in JSON for service account (not for web client!)

其他可配置参数

queues:
    host: localhost     # connection parameters for RabbitMQ
    port: 5672
    user: admin
    password: admin
    names:              # names of queues in RabbitMQ
        resizer: resize
        storage: upload
        success: done
        failure: failed

resizer:
    side: 640           # size to which images should be resized
    bg-color: '#ffffff' # what colour to use for background if image does not fit new ratio
    delete-origin: true # delete origin images after successful resized or not

上述所有设置都是可选的,可以在您的config.yml文件中省略。

用法

安排图片列表

使用schedule命令,接受包含图片的目录路径,并安排它们进行调整大小

$ bot schedule ./images

调整安排的图片大小

使用接受选项-n <count>resize命令。从调整大小队列中获取下一个count个图片,并将它们调整为640x640像素的JPEG格式。如果图片不是正方形形状,调整器应通过添加白色背景使其变为正方形。

下一个命令将调整不超过12张图片

$ bot resize -n 12

调整队列中的所有图片

$ bot resize

调整大小的图片将存储在名为<origin directory>_resized的目录中。即(如果不存在)将创建一个新目录,其名称为放置图片的原始目录名称加上后缀_resized。如果调整大小成功,原始图片将从原始目录中删除(此行为可以在配置中更改)。

将调整大小的图片上传到存储

使用接受选项-n <count>upload命令。将上传队列中的下一个count个图片上传到远程存储之一。云存储类型和相应的凭据应在配置文件中设置。目前只能设置一个远程存储。

下一个命令将上传不超过5张图片

$ bot upload -n 5

上传队列中的所有图片

$ bot upload

监控

您可以使用status命令获取过程中所有步骤的状态。它将输出所有队列及其中的图片数量

$ bot status
Queue   Count
resize  0       # origin images scheduled for resizing
upload  12      # resized images scheduled for uploading 
done    42      # uploaded images
failed  4       # images which could not be resized/uploaded for some reason

重新安排“失败”的图片

有时图片可能无法上传(例如,由于网络问题)或调整大小。使用接受选项-n <count>retry命令。它将所有(或所选count)"失败"图片再次移到队列中进行调整大小。

下一个命令将重新安排不超过3张图片

$ bot retry -n 3

重新尝试队列中的所有图片

$ bot retry