pupiq/image-scaler

图像调整大小的实用工具。生成适合网页的优化图像。

v0.7.3 2024-05-27 14:57 UTC

This package is auto-updated.

Last update: 2024-09-27 20:25:43 UTC


README

Build Status

图像调整大小的实用工具。生成适合网页的优化图像。

基本用法

$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

缩放选项

方法 scaleTo() 中有很多选项,其中一些是相互矛盾的,因此不应同时使用。

$scaler->scaleTo($width,$height,[
  "orientation" => 0, // automatically detected 

  // this is the source area on the original image
  "x" => 0,
  "y" => 0,
  "width" => $image_width,
  "height" => $image_height,

  "keep_aspect" => false,

  "crop" => null, // null, "auto", "top", "bottom"

  "strip_meta_data" => true,
  "sharpen_image" => null, // true, false, null (auto)
  "compression_quality" => 85,
  "auto_convert_cmyk_to_rgb" => true,

  "output_format" => "jpeg", // "jpeg", "png"

  "background_color" => "#ffffff", // by default it is "transparent" for png images
]);

典型用法

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

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

// Scale image to size 200x200.
// Original image will be inscribed into 200x200 box.
// There will be no aspect ratio distortion,
// but rather some parts of the output image may be padded with the background_color.
$scaler->scaleTo(200,200,["background_color" => "#ffffff"]);

// Transform original image into max 200px width and max 200px height
// so either the final width or the final height may be lower than 200px.
$scaler->scaleTo(200,200,["keep_aspect" => true]);

// Transform and crop the original image into 200x200 box.
$scaler->scaleTo(200,200,["crop" => true]);

// Transform and crop the original image into 200x200 box
// and preserve the top part of the image.
// This is a great option e.g. for magazine or book covers.
$scaler->scaleTo(200,200,["crop" => "top"]);

过滤器

图像处理可能会受到过滤器使用的影响。有两种类型的过滤器。

  • 缩放后过滤器
    这些过滤器在缩放后立即执行。将 Imagick 对象传递给它们。
    缩放后过滤器必须是 Pupiq\ImageScaler\AfterScaleFilter 的实例
  • 保存后过滤器
    这些过滤器在将图像保存到输出文件后立即执行。将文件名传递给它们。
    保存后过滤器必须是 Pupiq\ImageScaler\AfterSaveFilter 的实例

在这两种类型的过滤器中,也传递了有关所需转换的详细信息。

此软件包附带了一些可以立即使用的过滤器。

灰度滤镜

灰度滤镜将当前处理的图像转换为灰度。它是一个缩放后过滤器。

$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 优化器滤镜

对于 PNG 图像,有 Pngquant 优化器滤镜。它可以显着减小最终 PNG 图像的大小。需要在系统中安装二进制 pngquant。

Pngquant 优化器滤镜是一个保存后过滤器。

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

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

对于 JPEG 图像,此过滤器简单地不做任何事情。

水印滤镜

此过滤器将给定的水印放置在当前处理的图像中。

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

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

当然,过滤器可以组合使用。它们按照给定的顺序进行处理。

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

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

$scaler->scaleTo(300,300);
$scaler->saveTo("/path/to/output_image.jpeg"); // watermaked and grayscaled image

安装

只需使用 Composer 即可

composer require pupiq/image-scaler

测试

安装开发所需的依赖项

composer update --dev

运行测试

cd test
../vendor/bin/run_unit_tests

许可

ImageScaler 是免费软件,根据 MIT 许可证分发 条款