用于编辑图像的 PHP 库

v1.1.0 2023-01-25 20:17 UTC

This package is auto-updated.

Last update: 2024-09-26 15:30:33 UTC


README

Discord

此 PHP 库旨在尽可能易于理解。稍后会有更好的 README,这只是一个关于您拥有的功能的简单文档!该项目主要设计为与 phpcord 交互。

安装

您可以直接从 github 下载此存储库(在这种情况下,您必须创建自己的自动加载器)或通过安装 Composer 来安装它。安装后,只需在您的设备上执行 composer require himmelkreis4865/imagelib 即可。您也可以下载 phar 并用 php 运行它

为了享受 composer 的自动加载功能,您必须在访问第一个类之前添加以下内容

require_once __DIR__ . "/vendor/autoload.php";

说明

  • 填充 是对象的左上角与顶部左侧之间的距离。它告诉对象的位置

文档

创建图像

您可以从文件中获取图像来创建图像

<?php

use image\Image;

$image = Image::fromFile("file.png");

或创建一个新的图像

<?php

use image\Image;
// first parameter (300) is the width, second parameter is the height (200)
// both is being measured in pixels
$image = Image::make(300, 200);

在您的代码末尾,您可以添加 $image->display(); 来显示您的图像或使用 $image->save("your/path/to/file.png");(您必须传递自己的路径和文件名)来保存它

更改背景

use image\color\components\RGB;
use image\color\Gradient;
use image\color\SingleColor;
use image\Image;

$image = Image::make(300, 200);

// fill the background either with a singlecolor (red in this case) or a gradient (red -> yellow in this case)
$image->fill(new SingleColor(new RGB(255)));
// creates a smooth animation between the 2 colors
$image->fill(new Gradient(new RGB(255), new RGB(255, 255)));

向图像添加文本

创建文本有两种方法

简单但更丑陋的方法

<?php

use image\Image;
use image\color\SingleColor;
use image\color\components\RGB;
use image\font\BaseFont;
use position\Vector2;

$image = Image::make(200, 50);

// make the background black
$image->fill(new SingleColor(new RGB(0, 0, 0)));

// add a white default font
$image->addBaseText(new BaseFont("A test string", 4, new Vector2(50, 15), new RGB(255, 255, 255)));

$image->display();

这将创建类似以下输出

basefont_output.png

另一种选择是创建扩展字体

<?php

use image\Image;
use image\color\SingleColor;
use image\color\components\RGB;
use position\Vector2;
use image\font\ExtendedFont;

$image = Image::make(200, 50);

// make the background black
$image->fill(new SingleColor(new RGB(0, 0, 0)));

// add a white font of the family "arial" with fontsize 12(px)
$image->addExtendedText(new ExtendedFont("A test string", "arial.ttf", 12, 0.0, new Vector2(50, 30), new RGB(255, 255, 255)));

$image->display();

这将创建类似以下输出

basefont_output.png

注意: 目前可以使用的字体有 arial.ttfarialbd.ttf(粗体 arial),更多字体即将推出。

创建形状

形状是可添加到图像中的对象,如正方形、圆形等。

简单做

// default usage:
$image->drawShape(BaseShape);

// sample:

use image\color\components\RGB;use image\color\SingleColor;use image\shapes\Circle;use position\Vector2;

// This will create a red circle with a padding of 50px in each direction and a size of 100 pixels
$image->drawShape(new Circle(new Vector2(50, 50), new SingleColor(new RGB(255)), 100));

由于几乎每个形状都有一个不同的构造函数,因此在使用之前,您应该始终查看 API。当前形状:image\shapes\Circleimage\shapes\Ellipseimage\shapes\Lineimage\shapes\Polygonimage\shapes\Rectangleimage\shapes\Square

旋转和翻转

可以使用以下方式旋转 image\Image

$image->rotate($degrees, $change);

$degrees 是一个包含角度的整数(-360 到 360),$change 是一个可选参数,一个布尔值,告诉系统直接修改图像(true)或仅返回图像(false),默认为 true

如果您想翻转图像,可以通过以下方式执行

use utils\FlipTypes;

$image->flip(FlipTypes::TYPE_HORIZONTAL);

可用的翻转模式:FlipTypes::TYPE_HORIZONTAL(像镜子一样)、FlipTypes::TYPE_VERTICAL(像 180° 旋转)和 FlipTypes::TYPE_BOTH 应用两种模式

方形和调整图像大小

“方形图像”指的是宽度和高度相同的图像,这在调整大小和其他工具方面非常有用。这可以通过以下方式完成

有三个不同的方形位置,SquareConverter::TYPE_CENTERSquareConverter::TYPE_LEFTSquareConverter::TYPE_RIGHT

<?php

use image\color\components\RGB;
use image\color\Gradient;
use image\Image;use utils\SquareConverter;

// creating an image with a different width and height
// choosing a greater height than width for a purpose, you'll find out later
$image = Image::make(300, 700);

// adding a gradient to show a difference later
$image->fill(new Gradient(new RGB(255, 255), new RGB(0, 0, 255)));

// now rotating the image because gradients are only applied from top to bottom (we need it vertical)
$image->rotate(90);

// now bringing the center:
$image->toSquare(SquareConverter::TYPE_CENTER);

$image->display();

示例输出

gradient_square_after.png

顺便说一下,在方形之前,图像看起来像这样:gradient_square_before.png

您也可以使用此代码对左或右边的方形进行一些测试,但我们在此不展示它。

要调整图像大小,您可以执行以下操作:$image->resizeTo($new_width, $new_height) 有时在调整大小之前将其方形化是一个好主意。

将图片放入另一个图片中

基本版本

<?php

use image\color\components\RGB;
use image\color\SingleColor;
use image\Image;
use position\Vector2;

// creating the default image:
$image = Image::make(500, 500);
$image->fill(new SingleColor(new RGB(255)));

// creating the target image: (here a sample one you won't find in the code)
$target = Image::fromFile("test.png");

// squaring and resizing it to get the best result
// default parameter is center so I leave it out
$target->toSquare();

$target->resizeTo(400, 400);

// now add the target image to the default one
$image->addImage($target, new Vector2(50, 50));

$image->display();

输出

add_image_basic.png

扩展版本

差异看起来不重要,但实际上很大

<?php

use image\color\components\RGB;
use image\color\SingleColor;
use image\Image;
use position\Vector2;

// creating the default image:
$image = Image::make(500, 500);
$image->fill(new SingleColor(new RGB(255)));

// creating the target image: (here a sample one you won't find in the code)
$target = Image::fromFile("test.png");

// squaring and resizing it to get the best result
// default parameter is center so I leave it out
$target->toSquare();

$target->resizeTo(400, 400);

// now add the target image to the default one
$image->addImage($target, new Vector2(50, 50), true, 70);

$image->display();

输出

add_image_extended.png

这是由于在 addImage() 中增加了2个额外参数导致的

  • true 告诉API将你的图片变为圆形
  • 70 是此对象的透明度(100 表示完全覆盖,0 使其不可见)

更多描述即将到来...

自动加载器

根据您的操作系统和文件夹结构,在一个类似于以下的结构中

 docs
 image
 position
 utils
 index.php <- Your Entry point

您可以使用此自动加载器(在使用第一个类之前

spl_autoload_register(function(string $class) {
	include str_replace("\u{005C}", DIRECTORY_SEPARATOR, $class) . ".php";
});

这也应该是设备无关的