ANTICHAT

ANTICHAT (https://forum.antichat.xyz/index.php)
-   Общие вопросы программирования (https://forum.antichat.xyz/forumdisplay.php?f=206)
-   -   Как сделать форматирование текста по выключке последней строки влево? (https://forum.antichat.xyz/showthread.php?t=1443428)

Roman Grudov 01.08.2022 22:23

Всем привет! Пишу на PHP скрипт, который будет наносить текст на изображение и столкнулся с проблемой, что в моей библиотеке php-image нет форматирования по абзацу.

Цитата:

Сообщение от Спойлер

На Стеке в разделе Питона прочитал что нужно «разбить его пробелами и вставить пробелы, чтобы заполнить оставшееся пространство»

Цитата:

Сообщение от Спойлер


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

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.

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


Время: 19:08