x000000/storage-manager

支持缩略图的存储管理器

0.3.2 2016-08-01 14:59 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:41:53 UTC


README

该项目是一个PHP库,旨在使上传和存储文件尽可能简单。它还可以生成您想要的缩略图。

要求与依赖

该库有以下要求

  • PHP 5.6+
  • 该库只有一个依赖项 - Imagine。只有当您想使用存储管理器生成缩略图时才需要此依赖项。

安装

安装此库的首选方式是通过composer。

运行

php composer.phar require --prefer-dist x000000/storage-manager

或添加

"x000000/storage-manager": "^0.3"

到您的composer.json文件的require部分。

用法

存储

首先,您需要实例化Storage对象

<?php

$baseDir = '/path/to/the/storage/directory';
$baseUrl = '/static';
$storage = new \x000000\StorageManager\Storage(
    // this is a full path to the directory where files will be stored in
    $basedir,
    // this is a url to the directory above (how directory will be available by HTTP)
    $baseUrl
);

现在我们需要存储一些文件。

我们可以通过$storage->processFile()来实现,并传递要存储的文件的完整路径

<?php

$image = $storage->processFile('/full/path/to/image.png');

或者,如果您想处理新上传的文件,可以使用$storage->processUploadedFile()并传递来自$_FILES数组的一个条目

<?php

$image = $storage->processUploadedFile($_FILES['your_file_key']);

现在,如果一切顺利,我们将$image存储在$baseDir中,并相应地重命名它为它的md5内容。例如,它可能是类似于aed0d71a5adaff3a43b75fb4ccc7ff48.png的东西。是的,我们已经失去了它的原始名称,但我们也没有存储重复的。

在您的物理磁盘上成功存储文件后,您可能还想将其保存到数据库中。您不需要保存文件的完整路径,您只需保存文件名$image即可。

显示

现在您可能想显示一个$image或只是分享一个指向它的链接

<img src="<?= $storage->getSource($image); ?>" />

就是这样。通过调用$storage->getSource(),我们可以获取源文件的完整url(不包括域名)。

缩略图

在某些情况下,您可能不想显示原始的$image。或者您想显示一个漂亮的方形缩略图(你好Instagram)。或者只是调整大小/加水印的$image

为了实现这一点,有一个辅助方法$storage->thumb()

This $image resized to fit 300 width
<img src="<?= $storage->thumb($image)->resize(300, null); ?>" />

This $image resized to fit 600 height
<img src="<?= $storage->thumb($image)->resize(null, 600); ?>" />

This $image resized to fit 300x600 pixels size
<img src="<?= $storage->thumb($image)->resize(300, 600); ?>" />

This $image resized by the half with saving original aspect ratio
<img src="<?= $storage->thumb($image)->resize('50%', null); ?>" />


This $image cropped with a rectangle sized 50% x 60% from original image with center in 20x30 on original image
<img src="<?= $storage->thumb($image)->crop(20, 30, '50%', '60%'); ?>" />

This $image cropped with a square with a side size of a lowest $image side.
This is Instagram-ish square crop.
<img src="<?= $storage->thumb($image)->crop('50%', '50%', '100%', '100%', Crop::COVER); ?>" />

This $image cropped with a square with a side size of a greatest $image side.
This is Instagram-ish square crop either, but it adds padding instead of cropping the original.
<img src="<?= $storage->thumb($image)->crop('50%', '50%', '100%', '100%', Crop::CONTAIN); ?>" />

This $image cropped with a rectangle with aspect ratio 16:9.
<img src="<?= $storage->thumb($image)->crop('50%', '50%', '100%', null, 16/9); ?>" />


And of course you can chain resize, crop etc...
<img src="<?= $storage->thumb($image)
    ->crop('50%', '50%', '100%', '100%', Crop::COVER)
    ->resize(300, null); ?>" />


If you do not chain any transform url to the source file will be returned:
<img src="<?= $storage->thumb($image); ?>" />
equals to
<img src="<?= $storage->getSource($image); ?>" />

目前,仅支持调整大小和裁剪,但您可以使用自己的图像转换。有关更多信息,请参阅Transform::add()