images/clip_image
Clip image.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Clip image:
*
* Use the "matchbox" option with the "clipping" suboption to place some
* part of an imported image. The following variants are demonstrated:
* - clip a rectangular area of the image and place it in its original scaling.
* - clip a rectangular area of the image and force-fit into a box with
* specified size.
*
* 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 = "Clip image";
$exitcode = 0;
$imagefile = "nesrin.jpg";
try {
$llx = 50; $lly = 550;
$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());
$font = $p->load_font("NotoSerif-Regular", "unicode", "");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
// ---------------------------------------------------------
// Output the original image with transparency as background
// ---------------------------------------------------------
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
$p->setfont($font, 10);
/* Output some descriptive text */
$text = "Place the full image in its original size with transparency (for comparison)";
$p->fit_textline($text, $llx, $lly-=30, "");
/* Create an extended graphics state with transparency */
$gstate = $p->create_gstate("opacityfill=.4");
/* Fit the image in its original size with the transparency gstate */
$p->fit_image($image, $llx, $lly-=500, "gstate=" . $gstate);
/* ----------------------------------------------
* Place the clipped image in its original size
* ----------------------------------------------
*/
/* Define a rectangular part of the image with the "matchbox" option
* and the "clipping" suboption to set the lower left and upper right
* corners of the clipping rectangle.
*/
$c_llx = 350; $c_lly = 180; $c_urx = 550; $c_ury = 360;
$clipping_optlist = "matchbox={clipping={" .
$c_llx . " " . $c_lly . " " . $c_urx . " " . $c_ury . "}}";
/* Output some descriptive text */
$text = "Place the clipped image in its original size:";
$p->fit_textline($text, $llx + $c_llx, 410, "");
$text = "p.fit_image(image, " . ($llx + $c_llx) . " , " . ($lly + $c_lly) .
", \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx + $c_llx, 395, "");
/* Display the clipped area at the same position where it would
* appear when displaying the complete image.
*/
$p->fit_image($image, $llx + $c_llx, $lly + $c_lly, $clipping_optlist);
/* Output some descriptive text */
$text = "Place the clipped image in its original size";
$p->fit_textline($text, $llx, 245, "");
$text = "at the lower left corner of the full image:";
$p->fit_textline($text, $llx, 230, "");
$text = "p.fit_image(image, " . $llx . " , " . $lly . ", \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, 215, "");
// Place the clipped image at the lower left corner of the complete image.
$p->fit_image($image, $llx, $lly, $clipping_optlist);
$p->end_page_ext("");
/* -----------------------------------------------------
* Display the clipped area in a box with specified size
* -----------------------------------------------------
*/
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
$p->setfont($font, 10);
$lly = 550;
/* Define a rectangular part of the mage with the "matchbox" option
* and the "clipping" suboption. The lower left and upper right corners
* of the rectangle are specified as percentages of the original image size.
*/
$clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}}";
$text = "Place the clipped area in its original size:";
$p->fit_textline($text, $llx, $lly-=30, "");
$text = "p.fit_image(image, x, y, \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, $lly-=15, "");
/* Place the clipped image */
$p->fit_image($image, $llx, $lly-=220, $clipping_optlist);
/* Display the clipped image in a box of the specified size. Scale the
* clipped image proportionally so that it fits into the specified box
* with "fitmethod=meet".
*/
$clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}} " .
"boxsize={200 100} fitmethod=meet showborder";
/* Output some descriptive text */
$text = "Place the clipped area in a specified box:";
$p->fit_textline($text, $llx, $lly-=50, "");
$text = "p.fit_image(image, x, y, \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, $lly-=15, "");
$p->fit_image($image, $llx, $lly-=120, $clipping_optlist);
$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=clip_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;
?>