path_objects/import_path_from_image
Import a path object from an image file and use it for filling, stroking or clipping.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Import a path object from an image file and use it for filling, stroking
* or clipping
*
* Use a TIFF or JPEG image containing a clipping path. Import the path
* (while ignoring the pixel data) and use the resulting path object
* for filling, stroking or clipping.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: TIFF or JPEG image containing a clipping path
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Import path from image";
$imagefile = "child_clipped.jpg";
$exitcode = 0;
$target_x = 50; $target_y = 50; // target location
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){
echo("Error: " . $p->get_errmsg(p));
exit(1);
}
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
/* Start a page */
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Load the image file with its integrated clipping path
* Add the following option to retrieve a particular path by
* name: "clippingpathname={Path 47}".
* "infomode" means that the pixel data is ignored and
* not loaded into the PDF to reduce output file size.
*/
$image = $p->load_image("auto", $imagefile, "infomode");
if ($image == -1)
throw new Exception("Error: " . $p->get_errmsg());
/*
* You can supply formatting options here so that the
* path will be formatted the same way as an image.
*/
$formatting_options = "boxsize={300 300}";
/* Supply the formatting options when retrieving
* the clipping path to ensure correct scaling and placement.
*/
$path = (int) $p->info_image($image, "clippingpath", $formatting_options);
if ($path == 0)
throw new Exception("Error: image '" . $imagefile . "' doesn't contain clipping path");
/* Mark the target location with a red circle for illustration */
$p->set_graphics_option("fillcolor=red");
$p->circle($target_x, $target_y, 5);
$p->fill();
$p->save();
/* Optional: retrieve the bounding box of the placed image and
* clip the imported path to this box. This may be useful if the
* image contains irrelevant additional paths outside the image
* area. However, if you need the additional paths you should
* skip the bbox clipping.
*/
$bbox = (int) $p->info_image($image, "boundingbox", $formatting_options);
$p->draw_path($bbox, $target_x, $target_y, "close clip");
/* Draw the path at the target location */
$pathoptlist =
"close stroke strokecolor=red fill fillcolor=blue " .
"linewidth=2 linejoin=round linecap=round ";
$p->draw_path($path, $target_x, $target_y, $pathoptlist . $formatting_options);
$p->restore();
// Delete the path objects
$p->delete_path($path);
$p->delete_path($bbox);
/* Close the image */
$p->close_image($image);
$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=import_path_from_image.pdf");
print $buf;
}
catch (PDFlibException $e) {
echo("PDFlib exception occurred in import_path_from_image sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
exit(1);
}
catch (Throwable $e) {
echo($e);
exit(1);
}
$p = 0;
?>