定制 mistralys/application-utils-image 二次开发

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

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

mistralys/application-utils-image

Composer 安装命令:

composer require mistralys/application-utils-image

包简介

PHP image helper library for basic image operations and color management.

README 文档

README

PHP image helper library for basic image editing and color management.

Features

  • Dedicated image file classes, e.g. JPGFile, SVGFile.
  • Resample images by width, height, or both.
  • Get image dimensions, SVG files included.
  • Crop images.
  • Sharpen images.
  • Trim images.
  • Work with alpha transparency.
  • Add text to images.
  • Calculate the average color of an image.
  • Calculate the brightness or luma value of an image.
  • Work with a mix of HEX, 8Bit, 7Bit and percentage-based color values.

Requirements

  • PHP 7.4 or higher
  • Composer
  • GD extension

Usage

Image file classes

The library provides classes for handling different image formats:

  • GIFFile
  • JPGFile and JPEGFile
  • PNGFile
  • SVGFile

These classes work like the FileInfo class provided by the File Helper, so they provide all typical file operation methods as well as specialized image-related methods.

Simple image operations

The image file class methods will immediately save the modifications to the file system. If you want to perform multiple operations before saving, you will have to use an ImageHelper instance instead.

Resampling by width

This resamples the image to a new width, adjusting the height to keep the original image aspect ratio.

use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

JPGFile::factory('image.jpg')
    ->resampleByWidth('resized.jpg', 200);

Resampling by height

This resamples the image to a new height, adjusting the width to keep the original image aspect ratio.

use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

JPGFile::factory('image.jpg')
    ->resampleByHeight('resized.jpg', 200);

Resampling without the aspect ratio

This resamples the image to a new width and height, ignoring the original image aspect ratio.

use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

JPGFile::factory('image.jpg')
    ->resample('resized.jpg', 200, 400);

Displaying an image in the browser

Send an image file to the browser for display. This automatically sets the correct headers for the image format.

use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

JPGFile::factory('image.jpg')
    ->send('optional-name.jpg');

// You must manually exit the script after sending the image.
exit;

Trigger the download of an image

This will send the image to the browser, and force it to treat it as a download and show the download dialog.

use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

JPGFile::factory('image.jpg')
    // Set the second parameter to true to force the download.
    ->send('optional-name.jpg', true);

// You must manually exit the script after sending the image.
exit;

Advanced editing

The ImageHelper class

While the image file classes are practical for simple operations, the ImageHelper class provides more advanced editing capabilities, as well as combining multiple operations in a single chain.

Creating an instance

use AppUtils\ImageHelper;
use AppUtils\ImageHelper\ImageFiles\FileTypes\JPGFile;

// From a file path
$helper = ImageHelper::createFromFile('image.jpg');

// From an image class
$helper = JPGFile::factory('image.jpg')->createImageHelper();

// From a GD resource    
$resource = imagecreatefromjpeg('image.jpg');
$helper = ImageHelper::createFromResource($resource);

// New blank image
$helper = ImageHelper::createNew(200, 100);

Examples

Resampling an image

use AppUtils\ImageHelper;

ImageHelper::createFromFile('image.jpg')
    ->resampleByWidth(200)
    ->save('resized.jpg');

Sharpening an image

This will sharpen the image by the given percentage.

NOTE: For best results, do some testing to find the optimal sharpening percentage for your target image size.

use AppUtils\ImageHelper;

ImageHelper::createFromFile('image.jpg')
    ->resampleByWidth(200)
    ->sharpen(50)
    ->setQuality(80)
    ->save('sharpened.jpg');

Get image dimensions (including SVG)

use AppUtils\ImageHelper;

$size = ImageHelper::getImageSize('image.jpg');

echo $size->toReadableString();

Freeing resources

When you're done editing, call the dispose() method to release file handles and memory resources. By default, the save() method will automatically do this for you. In case you're not saving the image, you should call dispose() manually.

use AppUtils\ImageHelper;

$average = ImageHelper::createFromFile('image.jpg')
    ->calcAverageColorRGB();

// Not needed anymore? Free up resources.    
$helper->dispose();

mistralys/application-utils-image 适用场景与选型建议

mistralys/application-utils-image 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.19k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mistralys/application-utils-image 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mistralys/application-utils-image 我们能提供哪些服务?
定制开发 / 二次开发

基于 mistralys/application-utils-image 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 24
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-03