D
D
damarkuzz2022-02-21 18:58:41
PHP
damarkuzz, 2022-02-21 18:58:41

How to create an image-text with a background?

How to make a transparent plate with text on top of the image, as in the photo?

$draw = new ImagickDraw(); // Для работы с текстом
$bg = new Imagick('image.jpg'); // работа с самой картинкой

$draw->setTextAlignment(Imagick::ALIGN_CENTER);
$draw->setFillColor("rgb(255, 255, 255)"); // Цвет
$draw->setFontSize(40); // размер шрифта
$draw->setFont(ABSPATH."/wp-content/uploads/fonts/subset-GothamPro.ttf");

$bg->annotateImage($draw, 50, 50, 0, 'Лорем Ипсум');


$bg->writeImage('image.jpg');

6213b6ae7b4cd239438944.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2022-02-21
@damarkuzz

$source = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/image2.jpg');

$text = 'Лорем Ипсум';

$width = $source->getImageWidth();
$height = $source->getImageHeight();

$image = new Imagick(); // подложка
$draw = new ImagickDraw(); // текст
$mask = new Imagick(); // рамка

$draw->setFont($_SERVER['DOCUMENT_ROOT'] . '/impact.ttf');
$draw->setFontSize(60);
$draw->setFillColor(new ImagickPixel('white')); // black , как по мне по красивее получится
$draw->setGravity(Imagick::GRAVITY_CENTER);


$metric = $source->queryFontMetrics($draw, $text);

$x = (int) $metric['textWidth'] * 1.2;
$y = (int) $metric['textHeight'] * 1.2;

$offsetX = (int) (($width - $x) / 2);
$offsetY = (int) (($height - $y) / 2);

$image->newImage($x, $y, new ImagickPixel('white'));

$mask->newImage($x, $y, new ImagickPixel('gray20'));
$mask->annotateImage($draw, 0, 0, 0, $text);
$mask->setImageMatte(false);

$image->setImageCompressionQuality(100);
$image->setImageAlpha(0.2);
$image->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);

$source->compositeImage($image, Imagick::COMPOSITE_DISSOLVE, $offsetX, $offsetY);
$source->setImageCompressionQuality(100);

$source->setImageFormat('png');

header('Content-type: image/png');
echo $source;

#$source->writeImage($_SERVER['DOCUMENT_ROOT'] . '/testus.png');

6213ed0f09870752202295.jpeg6213ed21ace1c417119306.png
random font from windows fonts folder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question