images/frame_around_image
Draw a frame around an image.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Frame around image:
* Draw a frame around an image
*
* Place an image and draw a thick border around it using the "matchbox" option
* and its "borderwidth" and "offset" suboptions.
* On the second page, retrieve the matchbox of the placed image, create a
* path object from it and draw the path with the desired line properties.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: image file
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Frame around Image";
$imagefile = "nesrin.jpg";
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 image */
$image = $p->load_image("auto", $imagefile, "");
if ($image == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Start page */
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Define the option list for fit_image():
* Place the image in the center of a box using the "boxsize" and
* "position" options. Maintain its proportions using "fitmethod=meet".
* Use the "matchbox" option with the "borderwidth" suboption to draw a
* thick rectangle around the image. The "strokecolor" suboption
* determines the border color, and the "linecap" and "linejoin"
* suboptions are used to round the corners. The matchbox is always
* drawn before the image which means it would be hidden by the image.
* To avoid this use the "offset" suboptions with 50 percent of the
* border width to enlarge the frame beyond the area covered by the
* image.
*/
$optlist =
"boxsize={400 300} position={center} fitmethod=meet " .
"matchbox={borderwidth=10 offsetleft=-5 offsetright=5 " .
"offsetbottom=-5 offsettop=5 linecap=round linejoin=round " .
"strokecolor {rgb 0.0 0.3 0.3}}";
/* Fit the image using the option list defined above */
$p->fit_image($image, 200, 150, $optlist);
$p->end_page_ext("");
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
/*
* Second variant: Retrieve matchbox and create a path from it.
* Draw the path with the desired line properties. This method
* works also with other fitmethods than "meet".
*/
$optlist =
"boxsize={400 300} position={center} fitmethod=clip " .
"matchbox={name=border}";
$p->fit_image($image, 200, 150, $optlist);
$path = (int) $p->info_matchbox("border", 1, "boundingbox");
$p->draw_path($path, 0, 0,
"stroke close linewidth=10 linecap=round linejoin=round " .
"strokecolor {rgb 0.0 0.3 0.3}");
$p->delete_path($path);
$p->end_page_ext("");
$p->close_image($image);
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=frame_around_image.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;
?>