wordpressvn/poster-editor

PHP GD 库的包装器,用于轻松进行图像处理

5.16 2024-09-25 04:40 UTC

This package is auto-updated.

Last update: 2024-09-25 04:46:24 UTC


README

此类是Intervention Image包的替代品,用于更灵活地在图像上处理文本。如果您需要将文本适应到指定区域,自动计算字体大小和更改行高,请使用它。文本绘制方法还知道如何返回文字的实际大小,这允许例如将块堆叠放置。

除了灵活处理文本之外,该类还提供类似于Intervention包的Image API。包括智能海报调整大小、过滤器、绘制形状和覆盖其他图像。

如果您在工作中不使用Composer,您也可能对类的简单性和无任何依赖性感兴趣。请注意,此类仅支持PHP-GD驱动程序。您也可以轻松地继承您的类——可以覆盖父类的所有方法。

安装

安装Poster Editor的最佳方式是使用Composer快速轻松地安装。
但是,您可以直接要求类而不使用加载器——这不会以任何方式影响性能。

php composer.phar require wordpressvn/poster-editor

用法

如果发生错误,类方法将返回异常。
因此,最好在try..catch块内调用它们。

示例

// Using composer
require_once __DIR__ . '/vendor/autoload.php';

// Or directly
// require_once __DIR__ . '/PosterEditor.php';

try {
    $image = new PosterEditor\PosterEditor();
    $image->make('images/bridge.jpg')->fit(600, 600);
    $image->show();

} catch(Exception $e) {
    echo $e->getMessage();
}

可用方法

get

$image->get()

获取图像资源以使用原始GD命令。

set

$image->set(instance $resourse)

在使用原始GD命令后设置图像资源。

make

$image->make(mixed $data)

从文件或二进制数据创建新的图像实例。

insert

$image->insert(mixed $data, array $options = array())

将指定的图像源粘贴到当前图像上,可选位置。第一个参数可以是二进制数据或文件路径或另一个类实例。选项是一个数组,包含相对于左上角的x/y坐标偏移量和0到100之间的透明度,其中0是完全透明。默认情况下,图像将居中。

canvas

$image->canvas(int $width, int $height, array $options = array())

通过宽度和高度初始化画布。背景颜色选项是一个包含颜色/透明度设置的数组。默认情况下为不透明度为0的黑色画布。

show

$image->show(string $format = null, int $quality = 90)

发送HTTP响应,包含给定格式和质量的当前图像。格式是文件图像扩展名。默认情况下,响应数据将编码为当前图像的类型。如果尚未定义图像类型,方法将返回jpeg编码的数据。质量对所有文件类型进行了归一化,范围从0(质量差,文件小)到100(质量最佳,文件大)。默认值为90。PNG格式是无损的,质量影响图像大小和压缩速度。

save

$image->save(string $path, int $quality = 90, string $format = null)

将图像对象的当前状态保存到文件系统。可以定义一个特定的路径,其中图像应该被保存。图像类型将由文件扩展名定义。如果没有扩展名可用,则将使用当前图像的类型编码响应数据。如果尚未定义图像类型,则方法将返回jpeg编码的数据。可以可选地使用格式参数覆盖此操作。质量对所有文件类型进行了归一化,范围从0(质量差,文件小)到100(质量最佳,文件大)。默认值为90。PNG格式是无损的,质量影响图像大小和压缩速度。

destroy

$image->destroy()

在PHP脚本结束时释放与当前图像实例关联的内存。通常在脚本完成后自动销毁资源。

width

$image->width()

返回当前图像的像素高度。

height

$image->height()

返回当前图像的像素高度。

resize

$image->resize(int $width, int $height)

根据给定的宽度和高度调整当前图像的大小。不关注长宽比。为了按比例调整大小,请使用 upsizedownsize 方法。

upsize

$image->upsize(int $width = null, int $height = null)

在最大边放大图像。通过nulled宽或高的比率计算。

downsize

$image->downsize(int $width = null, int $height = null)

在最大边缩小图像。通过nulled宽或高的比率计算。

crop

$image->crop(int $width, int $height, array $options = array())

根据给定的宽度和高度裁剪当前图像的矩形部分。定义可选的 x,y 坐标来将裁剪框的左上角移动到指定位置。

fit

$image->fit(int $width, int $height, string $position = 'center')

通过裁剪和调整大小以智能方式格式化图像。该方法将自动在当前图像上找到最佳适应的长宽比,将其裁剪并调整到给定尺寸。默认可能的位子有 top-lefttoptop-rightbottom-leftbottombottom-rightrightleftcenter

line

$image->line(int $x1, int $y1, int $x2, int $y2, array $options = array())

在当前图像上从 x,y 点1画到 x,y 点2的线。使用 options 参数设置颜色、不透明度和宽度值。

rectangle

$image->rectangle(int $x, int $y, int $width, int $height, array $options = array())

在当前图像上画一个彩色矩形。使用 options 参数设置颜色、不透明度、厚度和外框值。

ellipse

$image->ellipse(int $x, int $y, int $width, int $height, array $options = array())

画一个椭圆。使用 options 参数设置颜色、不透明度和外框值。

brightness

$image->brightness(int $level = 0)

通过给定的级别更改当前图像的亮度。使用介于 -100(最小亮度)和 +100(最大亮度)之间的值,0 表示无变化。

contrast

$image->contrast(int $level = 0)

通过给定的级别更改当前图像的对比度。使用介于 -100(最小对比度)和 +100(最大对比度)之间的值,0 表示无变化。

grayscale

$image->grayscale()

将图像转换为灰度版本。

blur

$image->blur()

应用模糊图像效果。

invert

$image->invert()

反转图像的颜色。

blackout

$image->blackout()

在图像上绘制黑色不透明矩形。

rotate

$image->rotate(int $angle, array $options = array())

旋转图像。设置角度(以度为单位)和可选的旋转后未覆盖区域的颜色。

text

$image->text(string $text, array $options = array(), array &$boundary = array())

在图像上绘制文本。可能的选项

  • x: 文本框起点的 X-坐标。
  • y: 文本框起点的 Y-坐标。
  • width: 文本框的宽度。对于 nulled 值,从图像大小计算。
  • height: 文本框的高度。对于 nulled 值,从图像大小计算。
  • fontsize: 最大字体大小。如果文本不适合框,将缩小。
  • color: 文本的颜色。可以是 rgb 数组或 '#ffffff' 格式。
  • lineheight: 文本的行高。
  • opacity: 文本的不透明度,从 0(无透明度)到 100。
  • horizontal: 水平对齐。可以是 left/right/center/justify。
  • vertical: 垂直对齐。可以是 top/center/bottom/justify。
  • fontpath: .ttf 或 .otf 字体文件的路径。
  • debug: 如果为 true,则绘制文本框矩形。

使用边界来获取绘制的文本框的实际尺寸。下面有详细示例。

示例

将缩小后的图像附加到黑色背景

$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->fit(1200, 630, 'bottom')->blackout(50);

$logo = new PosterEditor\PosterEditor();
$logo->make('images/logo.png')->downsize(150, null);

$image->insert($logo, array('x' => 50, 'y' => 50))->show();

结果

append image

绘制并居中多行文本

$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->fit(1200, 630);
$image->grayscale()->brightness(-40);

$image->text(
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Lorem ipsum dolor sit amet', // phpcs:ignore
    array(
        'x'          => 100,
        'y'          => 100,
        'width'      => 1000,                 
        'height'     => 400,                  
        'horizontal' => 'center',             
        'vertical'   => 'center',             
        'fontpath'   => 'fonts/opensans.ttf',
        'fontsize'   => 24,
        'lineheight' => 1.75,
        'color'      => '#ffffff',
        'opacity'    => 0,
        'debug'      => true,
    )
);

$image->show('jpg', 70);

结果

multiline centered text

绘制并调整多行文本

$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->fit(900, 600);
$image->blackout(70);

$image->text(
    'Lorem ipsum dolor d d 4 g sit amet, consectetur adipiscing et, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eas commodo consequat sdfsdfl', // phpcs:ignore
    array(
        'x'          => 100,
        'y'          => 100,
        'width'      => 600,                   
        'height'     => 400,                   
        'horizontal' => 'justify',             
        'vertical'   => 'justify',             
        'fontpath'   => 'fonts/opensans.ttf',
        'fontsize'   => 20,
        'lineheight' => 1.5,
        'color'      => '#ffffff',
        'opacity'    => 0,
        'debug'      => true,
    )
);

$image->show('png');

结果

multiline justified text

使用边界在一个标题后面绘制另一个标题

$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->crop(
    900, 600,
    array(
        'x' => '0',
        'y' => '100'
    )
);

$image->grayscale()->brightness(-40);

$image->text(
    'Large title with unknown height. Can be multi-line',
    array(
        'x'          => 50,
        'y'          => 100,
        'width'      => 800,
        'fontpath'   => '/fonts/merriweather.ttf',
        'fontsize'   => 48,
        'lineheight' => 1.5,
        'color'      => '#9999ff',
    ),
    $boundary
);

$image->text(
    'This text appears right after title using smart boundaries',
    array(
        'x'          => 50,
        'y'          => $boundary['y'] + $boundary['height'],
        'width'      => 800,
        'fontpath'   => '/fonts/opensans.ttf',
        'fontsize'   => 20,
        'lineheight' => 1.5,
        'color'      => '#ff9999',
    ),
    $boundary
);

$image->show();

结果

boundary

绘制形状

$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->fit(1000, 630, 'bottom');
$image->contrast(5)->brightness(-30)->blur();

$image->rectangle(
    20, 20, 960, 590,
    array(
        'color'   => '#ffffff',
        'outline' => true,
        'width'   => 4,
    )
);

$image->ellipse(
    200, 200, 200, 200,
    array(
        'color'   => '#00ff00',
        'opacity' => 50,
    )
);

$image->ellipse(
    800, 200, 200, 200,
    array(
        'color'   => '#ff0000',
        'opacity' => 50,
    )
);

$image->rectangle(
    480, 280, 80, 140,
    array(
        'color'   => '#0000ff',
    )
);

$image->line(
    200, 500, 800, 500,
    array(
        'color'   => array(255, 255, 0),
        'opacity' => 10,
        'width'   => 4,
    )
);

$image->show('png');

结果

shapes result

贡献

所有项目代码存储在 Github 上。帮助项目的最佳方式是报告一个错误或添加一些新功能。您也可以在此处打开一个 问题 或发送一个 拉取请求