widgets-nl/image-processor

一个用于调整/转换图片的PHP库

1.0 2018-08-02 13:02 UTC

This package is not auto-updated.

Last update: 2024-09-29 15:40:50 UTC


README

一个用于调整/转换图片的PHP库

目录

要求

安装

使用Composer

运行以下命令

composer require widgets-nl/image-processor

手动安装

  • 克隆或下载项目
  • ImageProcessor.phpImageResource.php复制到您的选择目录。
  • 确保两个文件已自动加载/包含,例如。
require_once 'ImageProcessor.php';
require_once 'ImageResource.php';

使用方法

基本用法

<?php

    use WidgetsNL\ImageProcessor\ImageProcessor;
    
    $image = new ImageProcessor('sample_images/landscape.jpg');
    $image
        ->setWidth(100)
        ->setHeight(100)
        ->save('output.jpg')
    ;

指定图片类型

<?php

    use WidgetsNL\ImageProcessor\ImageProcessor;
    
    $image = new ImageProcessor('sample_images/landscape.jpg');
    $image
        ->setType(ImageProcessor::TYPE_PNG)
        ->save('output.png')
    ;

输出为字符串

<?php

    use WidgetsNL\ImageProcessor\ImageProcessor;
    
    $image = new ImageProcessor('sample_images/landscape.jpg');
    $image
        ->setWidth(100)
        ->setHeight(100)
    ;
    $imageData = $image->getFileData();
    
    header('Content-Type: image/jpeg');
    echo $imageData;

输出为base64编码的字符串/数据URI

<?php

    use WidgetsNL\ImageProcessor\ImageProcessor;
    
    $image = new ImageProcessor('sample_images/landscape.jpg');
    $image
        ->setWidth(100)
        ->setHeight(100)
    ;
//    $imageData = $image->getFileData(ImageProcessor::DATA_ENCODING_BASE64);
    $imageDataURI = $image->getFileData(ImageProcessor::DATA_ENCODING_BASE64_DATA_URI);

?>

<img src="<?php echo $imageDataURI; ?>">
<!-- This will output src="data:image/jpeg;base64, ... ... " -->

参考

class ImageProcessor

public function __construct(string $pathOrFileData = '') : ImageProcessor

类构造函数

参数

示例

$image = new ImageProcessor();

// Or

$image = new ImageProcessor('test.jpg');

// Or

$image = new ImageProcessor(file_get_contents('test.jpg'));

public function setResourceFromPath(string $path) : ImageProcessor

从指定的路径加载图片。内部创建一个ImageResource实例。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor();
$image->setResourceFromPath('test.jpg');

public function setResourceFromFileData(string $fileData) : ImageProcessor

从原始图片数据加载图片。内部创建一个ImageResource实例。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor();
$image->setResourceFromFileData(file_get_contents('test.jpg'));

public function getWidth() : int

返回当前宽度设置。


public function setWidth(int $width) : ImageProcessor

设置输出图片的宽度。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.png');
$image
    ->setWidth(100)
    ->save('test_copy.png')
;

public function getHeight() : int

返回当前高度设置。


public function setHeight(int $height) : ImageProcessor

设置输出图片的高度。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.png');
$image
    ->setHeight(100)
    ->save('test_copy.png')
;

对象适应

对象适应设置受object-fit CSS属性的启发,工作方式几乎相同。

有3个可能的值

public function getObjectFit() : int

返回当前的object fit设置。


public function setObjectFit(int $objectFit = self::OBJECT_FIT_CONTAIN) : ImageProcessor

设置应用在输出图片上的object fit设置。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.jpg');
$image
    ->setWidth(100)
    ->setHeight(100)
    ->setObjectFit(ImageProcessor::OBJECT_FIT_COVER)
    ->save('test_copy.jpg')
;

画布适应

画布适应设置确定输出图片画布是否必须裁剪以匹配图片。此设置仅在将object fit设置为ImageProcessor::OBJECT_FIT_CONTAIN时相关。

有2个可能的值

public function getCanvasFit() : int

返回当前的canvas fit设置。


public function setCanvasFit(int $canvasFit = self::CANVAS_FIT_CROP) : ImageProcessor

canvas fit 设置应用于输出图像。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.jpg');
$image
    ->setWidth(100)
    ->setHeight(100)
    ->setCanvasFit(ImageProcessor::CANVAS_FIT_KEEP)
    ->save('test_copy.jpg')
;

类型(MIME类型)

当从文件路径加载图像时,默认输出文件类型将与原始类型相同。当无法确定类型(例如,从原始图像数据加载图像时),默认使用 image/jpeg

您还可以指定要使用的输出类型。

目前支持3种文件类型

public function getType() : string

返回当前 type 设置。


public function setType(string $type = self::TYPE_JPEG) : ImageProcessor

type 设置为输出图像使用。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.jpg');
$image
    ->setType(ImageProcessor::TYPE_PNG)
    ->save('test.png')
;

质量

设置输出图像使用的图像质量(压缩级别)。

此设置不适用于 gif 图像,并且对大多数 png 图像几乎没有影响。

质量设置使用 1-100 的值,但有一些内置默认值

public function getQuality() : int

返回当前 quality 设置。


public function setQuality(int $quality = self::QUALITY_HIGH) : ImageProcessor

quality 设置应用于输出图像。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.jpg');
$image
    ->setQuality(ImageProcessor::QUALITY_LOW)
    ->save('test_copy.jpg')
;

背景填充

设置输出图像使用的背景填充样式/颜色。

背景可以设置为任何有效的十六进制颜色(例如,#FF0000),但短格式 (#F00) 不受支持。井号/哈希符号是可选的。

对于 pnggif 图像,您可以使用 TRANSPARENT 设置。一些内置值

public function getBackground() : string

返回当前 background 设置。


public function setBackground(string $background = self::QUALITY_HIGH) : ImageProcessor

background 设置应用于输出图像。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.png');
$image
//    ->setBackground('#FF0000')
    ->setBackground(ImageProcessor::BACKGROUND_WHITE)
    ->save('test_copy.png')
;

public function getFileData(int $encoding = self::DATA_ENCODING_RAW) : string

将输出图像作为图像数据字符串返回。

可以使用 encoding 参数指定数据编码,该参数可以设置为以下之一

参数

示例

$image = new ImageProcessor('test.jpg');
$image->setWidth(100);
$rawImageData = $image->getFileData();

public function save(string $outputPath) : ImageProcessor

将输出图像保存到指定的路径。

为了链式调用,此方法返回ImageProcessor实例。

参数

示例

$image = new ImageProcessor('test.png');
$image
    ->setWidth(100)
    ->save('test_copy.jpg')
;

class ImageResource

此类用于内部使用,没有可访问的公共成员或方法。