leongrdic/imgman

一个简单的PHP图像处理库,可以缩小和压缩图像,例如缩略图和个人资料图片

1.0.0 2022-01-26 21:07 UTC

This package is auto-updated.

Last update: 2024-09-16 02:40:35 UTC


README

此库使用GD和EXIF(可选)PHP扩展,请确保已安装它们。

它还使用了最新的PHP 8.1功能,但尚未支持向下兼容。

安装

composer require leongrdic/imgman
use \Le\ImgMan\{ImgMan, ImageFormat};

支持的格式

输入

  • 任何由php-gd支持的图像格式

输入方法: fromDataUrl()fromString()fromFile()

输出

使用所需的输出格式调用 output() 方法

  • ImageFormat::jpeg
  • ImageFormat::png
  • ImageFormat::webp(请确保您的php-gd配置已启用与webp的兼容性)

之后使用: toDataUrl()toString()toFile()

示例用法

$rawImageBytes = (new ImgMan)
    ->fromDataUrl($dataUrlFromJS)
    ->cacheExif()
    ->downscale(2048)
    ->rotateFromExif() // rotating after downscaling should use less memory and be a bit faster
    ->output(ImageFormat::jpeg, quality: 75)
    ->toString();
(new ImgMan)
    ->fromFile('example.png')
    ->downscale(1920, 1080)
    ->output(ImageFormat::png)
    ->toFile(); // use input filename (replace original file)
$dataUrl = (new ImgMan)
    ->fromString($rawImageBytes)
    ->output(ImageFormat::webp, quality: 80)
    ->toDataUrl();

注意

此库尚未完全测试,请自行承担责任使用。欢迎任何反馈和改进建议!