antonlukin / poster-editor
PHP的GD库的包装器,用于简单图像操作
Requires
- php: >=7.1.0
Requires (Dev)
Suggests
- ext-gd: to use GD library based image processing.
README
此类是Intervention Image包的替代品,用于更灵活地在图像上处理文本。如果需要将文本放在指定区域内,自动计算字体大小和行高,请使用它。文本绘制方法还知道如何返回实际的大小,这将允许,例如,将块并排放置。
除了灵活地处理文本外,该类还提供与Intervention包类似的Image API。包括智能海报缩放、过滤器、绘制形状和叠加其他图像。
如果您在工作中不使用Composer,您可能还对类的简洁性和无依赖性感兴趣。请注意,此类只支持PHP-GD驱动程序。您也可以轻松地继承您的类——所有父类的方法都可以被覆盖。
安装
安装Poster Editor的最佳方式是使用Composer快速轻松地安装。
但是,您可以直接要求该类,而无需使用加载器 - 这不会以任何方式影响性能。
php composer.phar require antonlukin/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())
通过宽度和高度初始化画布。背景颜色选项是在颜色/透明度设置的数组中。默认情况下是黑色画布,无透明度。
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()
返回当前图像的像素高度。
高度
$image->height()
返回当前图像的像素高度。
调整大小
$image->resize(int $width, int $height)
根据给定的宽度和高度调整当前图像的大小。不考虑长宽比。为了按比例调整大小,请使用 upsize 和 downsize 方法。
放大
$image->upsize(int $width = null, int $height = null)
按最大边放大图像。对于空白的宽度和高度,按比例计算。
缩小
$image->downsize(int $width = null, int $height = null)
按最大边缩小图像。对于空白的宽度和高度,按比例计算。
裁剪
$image->crop(int $width, int $height, array $options = array())
使用给定的宽度和高度裁剪当前图像的矩形部分。定义可选的 x,y 坐标以将裁剪部分的左上角移动到特定位置。
适配
$image->fit(int $width, int $height, string $position = 'center')
智能组合裁剪和调整大小,以智能格式化图像。该方法将自动在当前图像上找到最佳适配长宽比,裁剪并调整到给定尺寸。默认可能的方位是 top-left、top、top-right、bottom-left、bottom、bottom-right、right、left 和 center。
线条
$image->line(int $x1, int $y1, int $x2, int $y2, array $options = array())
在当前图像上从 x,y 点1到 x,y 点2绘制一条线。使用 options 参数设置颜色、透明度和宽度值。
矩形
$image->rectangle(int $x, int $y, int $width, int $height, array $options = array())
在当前图像上绘制彩色矩形。使用 options 参数设置颜色、透明度、厚度和轮廓值。
椭圆
$image->ellipse(int $x, int $y, int $width, int $height, array $options = array())
绘制椭圆。使用 options 参数设置颜色、透明度和轮廓值。
亮度
$image->brightness(int $level = 0)
通过给定级别更改当前图像的亮度。使用介于 -100(最小亮度)和 +100(最大亮度)之间的值。0 表示无变化。
对比度
$image->contrast(int $level = 0)
通过给定级别更改当前图像的对比度。使用介于 -100(最小对比度)和 +100(最大对比度)之间的值。0 表示无变化。
灰度
$image->grayscale()
将图像转换为灰度版本。
模糊
$image->blur()
应用模糊图像效果。
反转
$image->invert()
反转图像中的颜色。
黑色
$image->blackout()
在图像上绘制黑色不透明矩形。
旋转
$image->rotate(int $angle, array $options = array())
旋转图像。设置角度(以度为单位)和旋转后未覆盖区域的可选颜色。
文本
$image->text(string $text, array $options = array(), array &$boundary = array())
在图像上绘制文本。可能的选项
- x:文本框起点的 X 坐标。
- y:文本框起点的 Y 坐标。
- width:文本框宽度。对于空白的值,从图像大小计算。
- height:文本框高度。对于空白的值,从图像大小计算。
- fontsize:最大字体大小。如果文本不适合框,将进行缩放。
- color:文本颜色。可以是 rgb 数组或 '#ffffff' 格式。
- lineheight:文本行高。
- opacity:文本透明度从 0 到 100。0 表示无透明度。
- 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();
结果
绘制并居中多行文本
$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);
结果
绘制并调整多行文本对齐
$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');
结果
使用边界在标题之间绘制一个标题
$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();
结果
绘制形状
$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');
结果
贡献
所有项目代码存储在 Github 上。帮助项目的最佳方式是报告错误或添加一些新功能。您还可以在此处打开一个 问题 或发送一个 拉取请求。




