PDA

Просмотр полной версии : Как сделать форматирование текста по выключке последней строки влево?


Roman Grudov
01.08.2022, 22:23
Всем привет! Пишу на PHP скрипт, который будет наносить текст на изображение и столкнулся с проблемой, что в моей библиотеке php-image (https://github.com/kus/php-image) нет форматирования по абзацу.




https://forum.antichat.xyz/attachments/28103901/




На Стеке в разделе Питона прочитал (https://www.blast.hk/redirect/aHR0cHM6Ly9zdGFja292ZXJmbG93LmNvbS9hLzcxNzAxMzg2Lz E5NjY0MjA3) что нужно «разбить его пробелами и вставить пробелы, чтобы заполнить оставшееся пространство»




Python:






from
PIL
import
Image
,
ImageDraw
,
ImageFont

font
=
ImageFont
.
truetype
(
"arial.ttf"
,
20
)
text
=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
\
"Pellentesque accumsan nec felis ut vulputate."
image_width
=
600
im
=
Image
.
new
(
"RGB"
,
(
image_width
,
80
)
,
"white"
)
d
=
ImageDraw
.
Draw
(
im
)
y
=
10
for
line
in
text
.
split
(
"\n"
)
:
words
=
line
.
split
(
" "
)
words_length
=
sum
(
d
.
textlength
(
w
,
font
=
font
)
for
w
in
words
)
space_length
=
(
image_width
-
words_length
)
/
(
len
(
words
)
-
1
)
x
=
0
for
word
in
words
:
d
.
text
(
(
x
,
y
)
,
word
,
font
=
font
,
fill
=
"black"
)
x
+=
d
.
textlength
(
word
,
font
=
font
)
+
space_length
y
+=
30







Как на php реализовать этот же код? Или есть какие-то другие решения?

MADELINE
03.08.2022, 18:26
Для работы с изображениями рекомендуется использовать ImageMagick - https://www.php.net/manual/ru/book.imagick.php (https://www.blast.hk/redirect/aHR0cHM6Ly93d3cucGhwLm5ldC9tYW51YWwvcnUvYm9vay5pbW FnaWNrLnBocA)

Roman Grudov
05.08.2022, 12:15
В личке Телеграмма попросил добавить автора эту функцию.

GitHub - antonlukin/poster-editor: Wrapper for PHP's GD Library for easy image manipulation. Support for scaling multi-line text, shapes, filters and smart resize. (https://github.com/antonlukin/poster-editor#draw-and-justify-multiline-text)

Wrapper for PHP's GD Library for easy image manipulation. Support for scaling multi-line text, shapes, filters and smart resize. - antonlukin/poster-editor

github.com