承接 pupiq/image-scaler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pupiq/image-scaler

Composer 安装命令:

composer require pupiq/image-scaler

包简介

PHP library for image resizing, cropping and format conversion. Supports JPEG, PNG, animated GIF, WebP, AVIF and HEIC.

README 文档

README

Build Status

Handy tool for image resizing. Produces well optimized images for web.

Supported input and output formats: JPEG, PNG, GIF (including animated), WebP, AVIF, HEIC.

Requirements

  • PHP >= 7.4
  • ext-imagick >= 3.4
  • pngquant (optional, required only for PngquantOptimizer filter)

Basic Usage

$scaler = new Pupiq\ImageScaler("/path/to/image.jpg");

// getting info about image
$scaler->getImageWidth(); // e.g. 1920
$scaler->getImageHeight(); // e.g. 1080
$scaler->getOrientation(); // 0, 1, 2 or 3 (i.e. 0, 90, 180, 270 degrees clockwise)
$scaler->getMimeType(); // "image/jpeg"

// performing a transformation
$scaler->scaleTo(300,200,["keep_aspect" => true]);

// saving to output file
$scaler->saveTo("/path/to/output_file.jpg");
// or saving to string
// $image = $scaler->saveToString();

// checking the result
$scaler_out = new Pupiq\ImageScaler("/path/to/output_file.jpg");
$scaler_out->getImageWidth(); // 300
$scaler_out->getImageHeight(); // 169

EXIF orientation is detected automatically for JPEG images. The image is rotated accordingly so that the output is always correctly oriented.

Scaling options

There are plenty of options in method scaleTo(). Some of them are mutually exclusive and should not be used together.

$scaler->scaleTo($width, $height, [
  "orientation" => 0, // automatically detected from EXIF; 0, 1, 2 or 3 (i.e. 0, 90, 180, 270 degrees clockwise)

  // source crop area within the original image
  "x" => 0,
  "y" => 0,
  "width" => $image_width,
  "height" => $image_height,

  // keep_aspect and crop are mutually exclusive
  "keep_aspect" => false,
  "crop" => null, // null, true/"auto", "top", "bottom"

  "strip_meta_data" => true,
  "sharpen_image" => null, // true, false, null (auto: sharpens when downscaling by more than 20%)
  "compression_quality" => 85,
  "auto_convert_cmyk_to_rgb" => true,

  "output_format" => "jpeg", // "jpeg", "png", "gif", "webp", "avif", "heic"

  "background_color" => "#ffffff", // defaults to "transparent" for png, gif, webp and avif
]);

Typical usages

// Scale image to 200px width, preserve aspect ratio.
$scaler->scaleTo(200);

// Scale image to 200px height, preserve aspect ratio.
$scaler->scaleTo(null, 200);

// Scale image to fit into a 200x200 box.
// Aspect ratio is preserved; any remaining space is filled with background_color.
$scaler->scaleTo(200, 200, ["background_color" => "#ffffff"]);

// Scale image to fit within 200x200.
// The output may be smaller than 200x200 in one dimension.
$scaler->scaleTo(200, 200, ["keep_aspect" => true]);

// Scale and crop the image to exactly 200x200.
// The crop is centred.
$scaler->scaleTo(200, 200, ["crop" => true]);

// Scale and crop to 200x200, preserving the top part of the image.
// Great for portrait photos, magazine covers, etc.
$scaler->scaleTo(200, 200, ["crop" => "top"]);

// Scale and crop to 200x200, preserving the bottom part of the image.
$scaler->scaleTo(200, 200, ["crop" => "bottom"]);

Format conversion

The output format can be changed independently of the input format. To convert a JPEG to WebP:

$scaler = new Pupiq\ImageScaler("/path/to/image.jpg");
$scaler->scaleTo(800, null, ["output_format" => "webp"]);
$scaler->saveTo("/path/to/output_image.webp");

Any combination of supported input and output formats is accepted.

Filters

Image processing can be extended with filters. There are two types of filters.

  • After scale filters
    Executed right after scaling. The Imagick object is passed to them.
    Must be an instance of Pupiq\ImageScaler\AfterScaleFilter.
  • After save filters
    Executed right after the image is saved to the output file. The filename is passed to them.
    Must be an instance of Pupiq\ImageScaler\AfterSaveFilter.

In both types the scaling options array is also passed to the filter.

This package comes with several built-in filters.

Grayscale filter

Converts the processed image to grayscale. It is an after scale filter.

$scaler = new Pupiq\ImageScaler("/path/to/image.jpg");
$scaler->appendAfterScaleFilter(new Pupiq\ImageScaler\GrayscaleFilter());

$scaler->scaleTo(300, 300);
$scaler->saveTo("/path/to/output_image.jpg"); // grayscale

Pngquant Optimizer filter

Significantly reduces the file size of PNG images using the pngquant binary. It is an after save filter and has no effect on non-PNG output formats.

$scaler = new Pupiq\ImageScaler("/path/to/image.png");
$scaler->appendAfterSaveFilter(new Pupiq\ImageScaler\PngquantOptimizer([
  "pngquant_binary" => "/usr/bin/pngquant", // default: "pngquant"
  "quality_range" => "70-90"                // default: "70-90"
]));

$scaler->scaleTo(300, 300);
$scaler->saveTo("/path/to/output_image.png");

Watermark filter

Places a watermark image over the processed image. It is an after scale filter.

$scaler = new Pupiq\ImageScaler("/path/to/image.jpg");
$scaler->appendAfterScaleFilter(new Pupiq\ImageScaler\WatermarkFilter("/path/to/watermark_image.png", [
  "opacity" => 50,          // percentage, default: 50
  "position" => "center",   // "center", "left-top", "left-bottom", "right-top", "right-bottom"; default: "center"
]));

$scaler->scaleTo(300, 300);
$scaler->saveTo("/path/to/output_image.jpg"); // watermarked image

Combining filters

Filters can be combined and are applied in the order they were added.

$scaler = new Pupiq\ImageScaler("/path/to/image.jpg");

$scaler->appendAfterScaleFilter(new Pupiq\ImageScaler\WatermarkFilter("/path/to/watermark_image.png"));
$scaler->appendAfterScaleFilter(new Pupiq\ImageScaler\GrayscaleFilter());

$scaler->scaleTo(300, 300);
$scaler->saveTo("/path/to/output_image.jpg"); // watermarked and grayscaled

Installation

Use the Composer:

composer require pupiq/image-scaler

Testing

Install required dependencies:

composer update

Run tests:

cd test
../vendor/bin/run_unit_tests

License

ImageScaler is free software distributed under the terms of the MIT license

pupiq/image-scaler 适用场景与选型建议

pupiq/image-scaler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 844 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「resize」 「watermark」 「scale」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 844
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-23