在透明背景的绘制文本上添加图案覆盖 [英] Add pattern overlay over drawn text with transparent background

查看:50
本文介绍了在透明背景的绘制文本上添加图案覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ImageMagick使用PHP从文本创建透明图像.

I'm using ImageMagick to create transparent images from text with PHP.

我想知道是否有可能(以及如何)在生成的图像上添加图案叠加层.这个想法是让背景保持透明,并在文本上应用纹理:

I would like to know if it's possible (and how) to add a pattern overlay over the generated image. The idea is to leave the background transparent and to apply the texture over the text :

实际结果:

所需结果:

任何建议将不胜感激.

推荐答案

使用 ImagickDraw 进行创建一个绘制"对象(就像一个图层),然后将其添加到图像上.这是一个示例:

Use ImagickDraw to create a "draw" object (it's like a layer), then add it over your image. Here's an example:

// create canvas
$draw = new ImagickDraw();
$draw->setfont('/path/to/your/font.ttf');

// load your overlay image
$overlay = new Imagick('/path/to/your/pattern.png');

// define pattern
$draw->pushPattern('myOverlay', 0, 0,
   $overlay->getImageWidth(), $overlay->getImageHeight());

// fill canvas with the pattern (tile)
$draw->composite(Imagick::COMPOSITE_COPY, 0, 0,
   $overlay->getImageWidth(), $overlay->getImageHeight(), $overlay);

// destroy pattern
$draw->popPattern();   
$draw->setFillPatternURL('#myOverlay');

// put text
$draw->setFontSize(100);
$draw->annotation(0, 0, 'My TextImage');

// create your image (800x400)
$output = new Imagick();
$output->newimage(800, 400, 'transparent');

// this will center your canvas
$output->setGravity(Imagick::GRAVITY_CENTER);

// render canvas on this image
$output->drawImage($draw);

$output->setImageFormat('png');
header('Content-Type: image/png');
print $output;

如果您希望将叠加层拉伸到适合800x600的大小,则在合成时使用该尺寸而不是图案尺寸

If you want the overlay to be stretched to fit 800x600, then use that size instead of the pattern size when compositing

这篇关于在透明背景的绘制文本上添加图案覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆