Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

php - Add pattern overlay over drawn text with transparent background

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 :

The actual result:

Actual result

The desired result:

enter image description here

Any advice would be greatly appreciated.

question from:https://stackoverflow.com/questions/65911254/annotate-text-drawing-in-imagick

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...