priler/text2image

最实用且易于使用的PHP库,可将任何文本转换为图片。使用GD扩展。

dev-master 2018-01-07 13:16 UTC

This package is not auto-updated.

Last update: 2024-09-24 04:23:00 UTC


README

最实用且易于使用的PHP库,可将任何文本转换为图片
版本:1.0 Beta
需求:PHP-GD

Text2Image主要创建用于将任何文本信息转换为图像表示。
重点是,没有其他库能完成这项任务,至少没有像这样容易使用的库。
所以你只需要引入这个库的头文件,创建一个实例,然后调用"output"或"save"函数,就是这样! :)

###关于模式
它中有两种模式,"简单"和"智能"。
它们之间的主要区别在于"简单"模式比"智能"模式快得多。
但是,"简单"模式不支持文本大小和角度参数。
使用"智能"模式,你可以使用任何你想要的字体,当然,前提是它们被GD支持,例如,TTF工作得很好。
见下文"所有公共方法列表"以了解如何在这两种模式之间切换。

###所有公共参数列表

  • $width = 720, // 图片框的宽度,文本将在这个框内换行 [int]
  • $font = 5, // 字体名称/家族,对于简单模式可以是整数字体索引,对于智能模式是TrueType字体的路径 [int] 或 [string]
  • $line_height = 'auto', // 行间距,可以是整数或'auto' [int] 或 [string]
  • $background_color = array(38, 50, 56), // 背景颜色,可以是RGB值数组,或十六进制字符串 [array] 或 [string]
  • $text_color = array(255, 255, 255), // 文本颜色,可以是RGB值数组,或十六进制字符串 [array] 或 [string]
  • $padding = 30, // 四周的填充 [int]
  • $angle = 0, // 智能模式下的文本角度 [int]
  • $text_size = 17, // 智能模式下的字体大小 [int]
  • $user_fonts = array(); // 用户定义的字体

###所有公共方法列表

  • get_mode() - 返回当前模式,true是"简单",false是"智能" [boolean]
  • set_mode($mode) - 允许你在模式之间切换,第一个参数接受"simple"或"smart"字符串
  • get_text() - 返回源文本
  • set_text($new_text) - 允许你设置新文本
  • add_font($label, [force_mode = null]) - 使用此方法可以轻松添加任何你想要的字体,通过添加"label"你可以按你的意愿命名添加的字体,"path"定义了字体的路径,"force_mode"可以在加载字体操作时强制使用模式(1是"简单",0是"智能")
  • get_font($label) - 使用此方法通过标签获取已加载的字体索引,然后你可以将此索引传递给"font"参数
  • is_imagetype_supported($type) - 如果传递的类型被你的PHP构建支持,则返回True,如果不支持则返回false
  • output($type = 'png', $quality = 100) - 将结果图像输出到浏览器
  • save($path, $type = 'png', $quality = 100) - 允许你将结果图像保存到图像,"quality"参数仅支持'jpg'或'png'图像类型

另外,请参阅"examples"文件夹以获取更多信息。

###基本使用示例

<?php

require "../src/magic.class.php"; // manual include, instead you can use composer

$test = new Priler\Text2Image\Magic('Hello world!');
$test->output();

示例输出

fonts example

###使用角度的美丽智能模式文本

<?php

require "../src/magic.class.php"; // manual include, instead you can use composer

$test = new Priler\Text2Image\Magic(
"
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
"
);

// force mode into smart
$test->set_mode('smart');

// load custom font
// smart-mode work's exactly with .TTF, but other font's may also be supported, see PHP GD docs for more info
$test->add_font('MyFont', './assets/foughtknight.ttf');
$test->font = $test->get_font('MyFont');

// also, smart mode supports text-size property and angle property (last one shown in 5th example)
$test->text_size = 20;
//ANGLE GOES HERE
$test->angle = 3;//negative values also supported

// let's change some basic stuff
$test->background_color = '#eee'; // custom background color
$test->text_color = '#FF5370'; // custom text color
$test->padding = 100; // custom padding


// this settings is same as on 4th example
$test->width = 720; // custom width
$test->line_height = 30; // custom line height


$test->output();

line height example