定制 bravo3/image-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bravo3/image-manager

Composer 安装命令:

composer require bravo3/image-manager

包简介

A PHP 5.4 library to control dynamic image assets in a cloud environment

README 文档

README

A PHP 5.4 image manager intended for cloud use. This image manager is designed to be low-level and work with 'keys' - not directly attach to an entity.

Features

  • Easily push and pull images to any remote filesystem (eg Amazon S3)
  • Request an image with specific dimensions - allow the manager to transparently create & store this variation
  • Request that an image dimension exists (will be created if it doesn't), allowing for the storage device to be used as a CDN end-point
  • Use a caching service to maintain a knowledge base of image dimensions available to improve performance
  • Load & save images from memory or a file
  • Convert image format & quality with ease
  • Customisable encoders
  • GD and Imagick support
  • PDF support (via Imagick)

Examples

Storing an image

// Use the local filesystem as a fake remote (replace with S3, etc)
$im = new ImageManager(new Filesystem(new LocalAdapter('/tmp/images')));

// Load local "image.png" and give it a key of 'content_123_image_1' (using the filename is suitable too)
$image = $im->loadFromFile('image.png', 'content_123_image_1');

// Save it on the remote
$im->push($image);

Retrieving an image

$image = new Image('content_123_image_1');
$im->pull($image);

// Save to local filesystem
$im->save($image, '/tmp/image.png');

// Output to client
echo $image->getData();

Retrieving an automatic variation

// Define a dimension that the image will fit in a height of 200px
$dimensions = new ImageDimensions(null, 200);

// Create a specification for a JPEG format, quality 75% and use the above dimensions
$image = new ImageVariation('content_123_image_1', ImageFormat::JPEG(), 75, $dimensions);

// Automatically create the variation when we pull
$im->pull($image);

if (!$im->isPersistent()) {
    // Make sure our new variation exists on the remote
    $im->push($image);
}

echo $image->getData();

Create a variation manually

$source = $im->loadFromFile('image.png', 'content_123_image_1');
$resized = $im->createVariation($source, ImageFormat::JPEG(), 90, new ImageDimensions(100, 100));
$im->push($resized);

Check if a variation exists

$image = new ImageVariation('content_123_image_1', ImageFormat::JPEG(), 75, $dimensions);

if (!$im->exists($image)) {
    $im->pull($image);  // Creates the variation
    $im->push($image);  // Save it on the remote
}

echo '<img src="http://cdn.example.com/'.$im->getKey().'" />';

Caching

Because remote storage services have a moderate degree of lag while talking to, it's probably not appropriate to do "exists" checks on every image variation during page generation. To avoid this you can either pre-render the image and assume it will exist, or using a quick caching mechanic to store an inventory of all images available.

Your caching mechanic MUST be persistent - if a cache key is lost the image manager will assume the remote file does not exist. Using a database or disk backed key/value storage is recommended (eg Redis).

To use caching, just include a \Bravo3\Cache\PoolInterface implementation in the ImageManager's constructor.

Encoders

By default the image manager will use the InterventionEncoder (@see http://image.intervention.io/), which supports base image formats. You can switch this for the ImagickEncoder which also allows for PDF documents to be used as an input.

To add support for native Imagick, but use Intervention where possible (thus giving you PDF support):

$im = new ImageManager(...);
$im->addEncoder(new ImagickEncoder());

To use only the Imagick encoder:

$im = new ImageManager($filesystem, $cache_pool, [new ImagickEncoder()]);

Future Considerations

  • Allow for image manipulations (eg add text, rotate, etc)
  • Allow for customisable variation naming schemes

统计信息

  • 总下载量: 8.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 8
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固