Repository navigation
Expand file tree
/
Copy pathImageTTFText.php
More file actions
279 lines (212 loc) · 6.48 KB
/
Copy pathImageTTFText.php
File metadata and controls
279 lines (212 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
class ImageTTFText
{
private $quality = 85; // Качество jpg по-умолчанияю
private $fontDir = '/css_js/fonts'; // Каталог шрифтов
private $src = false; // Исходное изображение
//текущие настройки для нанесения текста
private $props = array(
'font' => 'georgia_bi', // Файл шрифта
'fontK' => 1, // Коэффициент для размера шрифта
'size' => 14, // Размер шрифта
'color' => '#000000', // Цвет
'align' => 'left', // Выравнивание
'leading' => false, // Интерлиньяж в пикселях
'def_leading' => 1.6, // Интерлиньяж по умолчанию от размера шрифта
'box' => 0 // Ширина бокса, в который нужно разместить текст
);
//вернет путь до корня сайта
private static function root() {
return ROOT_DIR;
}
//вернет путь до файла шрифта
private function fontPath() {
return self::root().$this->fontDir.'/'.$this->font;
}
public function __isset($name) {
return isset($this->props) ? true : false;
}
public function __get($name) {
if (isset($this->props[$name])) {
return $this->props[$name];
}
return NULL;
}
public function __set($name, $value) {
if (isset($this->props[$name])) {
if ($name == 'align') {
$arr = array('center', 'left', 'right');
if (in_array($value, $arr)) {
$this->props[$name] = $value;
}
} else if ($name == 'color') {
$this->color($value);
} else if ($name == 'font') {
if (file_exists(self::root().$this->fontDir.'/'.$value.'.ttf')) {
$this->props[$name] = $value.'.ttf';
}
} else {
$this->props[$name] = $value;
}
}
}
public function __construct($target) {
$target = trim($target);
if (empty($target)) {
return false;
}
if (substr($target, 0, 1) == '/') {
$target = substr($target, 1);
}
$target = self::root().'/'.$target;
if (file_exists($target)) {
$type = exif_imagetype($target);
switch ($type)
{
case IMAGETYPE_GIF: $this->src = @imagecreatefromgif($target); break;
case IMAGETYPE_JPEG: $this->src = @imagecreatefromjpeg($target); break;
case IMAGETYPE_PNG: $this->src = @imagecreatefrompng($target); break;
}
}
}
public function __destruct() {
if ($this->src) imagedestroy($this->src);
}
/**
* Массовое присваивание параметров через массив
* @param array $arr
* @return $this
*/
public function set(array $arr) {
if (is_array($arr)) {
foreach ($arr as $key => $val) {
if (isset($this->props[$key])) {
$this->{$key} = $val;
}
}
}
return $this;
}
public function size($val) { $this->{__FUNCTION__} = $val; return $this; }
public function align($val) { $this->{__FUNCTION__} = $val; return $this; }
public function fontK($val) { $this->{__FUNCTION__} = $val; return $this; }
public function leading($val) { $this->{__FUNCTION__} = $val; return $this; }
public function font($val) { $this->{__FUNCTION__} = $val; return $this; }
public function box($val) { $this->{__FUNCTION__} = $val; return $this; }
/**
* @param $color
* @param int $alpha - от 0 до 127
*/
public function color($color, $alpha = 0) {
if ($alpha < 0) $alpha = 0;
if ($alpha > 127) $alpha = 127;
$this->color = false;
if ($this->src) {
list($r, $g, $b) = array_map('hexdec', str_split(ltrim($color, '#'), 2));
if ($alpha > 0) {
$this->color = imagecolorallocatealpha($this->src, $r+1, $g+1, $b+1, $alpha);
} else {
$this->color = imagecolorallocate($this->src, $r+1, $g+1, $b+1);
}
}
return $this;
}
public function text($x, $y, $text, $angle = 0) {
if ($this->font && $this->src && $this->color) {
$text = htmlspecialchars_decode($text);
$text = str_replace(array('<br>', '<br/>', '<br />'), "\n", $text);
$data = $data_tmp = explode("\n", $text);
$font_size = $this->size*$this->fontK;
//проверка на выход строки за пределы бокса
if (!empty($this->box)) {
$data = array();
//пробегаем по строкам
foreach ($data_tmp as $line) {
$words = explode(' ', $line);
$width = 0;
$str = '';
//пробегаем по словам
foreach ($words as $key => $word) {
if ($width > $this->box) {
$width = 0;
$data[] = trim($str);
$str = '';
}
$sizes = imagettfbbox($font_size, $angle, $this->fontPath(), $word);
$width += ($sizes[2] - $sizes[0]);
$str .= $word.' ';
}
$data[] = trim($str);
}
}
//пробегаем по строкам
foreach ($data as $item) {
$shift = 0;
$sizes = imagettfbbox($font_size, $angle, $this->fontPath(), $item);
$width = $sizes[2] - $sizes[0];
if ($this->align == 'center') {
$shift = $width/2;
} else if ($this->align == 'right'){
$shift = $width;
}
imagettftext(
$this->src,
$font_size,
$angle,
$x - $shift,
$y + $font_size,
$this->color,
$this->fontPath(),
$item
);
if ($this->leading) {
$y += $this->leading;
} else {
$y += $font_size*$this->def_leading;
}
}
}
return $this;
}
/**
* Сохраняет изображение в файл
* @param $target
* @param bool $replace
* @return bool
*/
public function save($target, $replace = true) {
$target = trim($target);
if (empty($target)) {
return false;
}
if (substr($target, 0, 1) == '/') {
$target = substr($target, 1);
}
$target = self::root().'/'.$target;
if (file_exists($target) && !$replace) return false;
$path_info = pathinfo($target);
$ext = strtolower($path_info['extension']);
switch ($ext)
{
case "gif":
imagegif ($this->src, $target);
break;
case "jpg" :
case "jpeg":
imagejpeg($this->src, $target, $this->quality);
break;
case "png":
imagepng($this->src, $target);
break;
default: return false;
}
return true;
}
public function render($filename) {
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=' . $filename.'.png');
imagepng($this->src);
imagedestroy($this->src);
}
}
?>