text_output/image_as_text_fill_color
Create outline text and fill the interior of the glyphs with an image.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Image as text fill color:
* Create outline text and fill the interior of the glyphs with an image.
*
* Define a pattern containing an image and use it as the fill color for the
* text being filled and stroked.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9.0.2
* Required data: none
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Image as Text Fill Color";
$imagefile = "text_filling.tif";
try {
$p = new pdflib();
$p->set_option("searchpath={" . $searchpath . "}");
/* This means we must check return values of load_font() etc. */
$p->set_option("errorpolicy=return");
if ($p->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
/* Load the font. */
$font = $p->load_font("NotoSerif-Bold", "unicode", "");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Load the background image */
$image = $p->load_image("auto", $imagefile, "");
if ($image == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Retrieve the image dimensions (related to its original
* resolution)
*/
$imagewidth = $p->info_image($image, "imagewidth", "");
$imageheight = $p->info_image($image, "imageheight", "");
/* The image may have a resolution above 72 dpi. If we used the
* returned dimensions (in points) for the pattern height and width
* and placed our image into the pattern, it wouldn't match the
* pattern size, and a white margin would be left. To make the pattern
* the same size as the image we calculate the image size
* based on its resolution.
*
* Retrieve the original image resolution
*/
$resx = $p->info_image($image, "resx", "");
$resy = $p->info_image($image, "resy", "");
/* Calculate the image dimensions for 72 dpi */
if ($resx > 0) {
$imagewidth = $imagewidth * 72 / $resx;
$imageheight = $imageheight * 72 / $resy;
}
/* Create a pattern using the retrieved image dimensions.
* The painttype parameter must be set to 1 since a colorized
* image is used (as opposed to an image mask).
*/
$pattern = $p->begin_pattern_ext($imagewidth, $imageheight, "");
$p->fit_image($image, 0, 0, "");
$p->end_pattern();
$p->close_image($image);
/* Start page */
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Set the pattern as the current fill color. Encapsulate the
* setcolor() call with save() and restore() to be able to proceed with
* the original colors.
*/
$p->save();
/* In addition to filling the text (which is the default), stroke the
* text by setting the textrendering mode to 2 (fill and stroke) and
* defining a stroke color different from black.
*/
$p->set_text_option("textrendering=2");
$p->setcolor("stroke", "rgb", 0.5, 0.2, 0.1, 0);
$p->setcolor("fill", "pattern", $pattern, 0, 0, 0);
/* Output the text with the current fill color, i.e. the image in terms
* of the pattern color
*/
$fontsize = 50;
$p->set_text_option("font=" . $font . " fontsize=" . $fontsize);
$p->fit_textline("Hello World!", 50, 500, "");
$p->fit_textline("(says PDFlib GmbH)", 50, 500 - $fontsize, "");
$p->restore();
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=image_as_text_fill_color.pdf");
print $buf;
} catch (PDFlibException $e) {
echo("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
exit(1);
} catch (Throwable $e) {
echo("PHP exception occurred: " . $e->getMessage() . "\n");
exit(1);
}
$p = 0;
?>