acoustep / canvas
简单的GD库图像缩放类
v0.1
2013-06-22 20:26 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-23 14:18:29 UTC
README
Canvas是一个简单的PHP类,用于使用GD库创建和操作图像大小。
使用方法
设置画布
有两种方式来设置画布。
- 通过构造函数
$canvas = \Acoustep\Canvas(500, 250, '#000000');
- 通过方法
$canvas = \Acoustep\Canvas()->width(500)->height(250)->background('#000000');
添加图像
通过image方法添加图像,您可以添加尽可能多的层。
第一个添加的图像将添加到输出底端。其余的将按照添加的顺序叠加在顶部。
image方法接受两个参数:一个字符串(文件及其路径)和一个参数数组。
$canvas = \Acoustep\Canvas()->width(500)
->height(250)
->background('#000000')
->image('images/test.jpg', array('x' => 'left',
'y' => 'top',
'scale' => 'best'));
X可以是整数,表示从画布像素偏移量或left(左)、centre(中心)或right(右)。
Y也可以是整数,用于特定偏移量,top(顶部)、middle(中间)或bottom(底部)。
缩放可以是width(宽度)、height(高度)、best(最佳)或false(假)。
- 宽度将图像设置为最佳匹配画布宽度。
- 高度将图像设置为最佳匹配画布高度。
- 最佳将尝试确定使用宽度还是高度为您。
- 假将保持图像大小与原始大小相同。
输出图像
有三个方法将图像输出到用户。 output()
、filetype()
和 create()
。
- 文件类型允许您设置图像类型。支持PNG、GIF和JPG。
- 输出允许您指定输出图像的文件名。如果为
NULL
,它将尝试输出到当前文件 - 您需要设置适当的头信息。 - 当您准备好将图像输出到用户时使用create。此方法始终是最后一个调用的。
示例
输出到名为'output.png'的文件
$canvas = \Acoustep\Canvas()->width(500)
->height(250)
->background('#000000')
->image('images/test.jpg', array('x' => 'left',
'y' => 'top',
'scale' => 'best'))
->filetype('png')
->output('output')
->create();
输出到当前页面
header("Content-Type: image/png");
$canvas = \Acoustep\Canvas()->width(500)
->height(250)
->background('#000000')
->image('images/test.jpg', array('x' => 'center',
'y' => 'middle',
'scale' => 'width'))
->filetype('png')
->output(NULL)
->create();