ImageExtractor
in package
Extract sub-images from an image.
Table of Contents
Constants
- DEFAULT_PDF_DPI : mixed = 300.0
- Resolution PDF pages are rendered at before cropping, in dots per inch. Images are cropped at their own resolution.
- CROP_QUALITY : mixed = 70
- JPEG quality used for extracted crops, same as the Python SDK (sufficient for OCR).
Properties
- $pageCount : int
- $filename : string
- $inputSource : LocalInputSource
- $pageImages : array<string|int, Image>
- $pdfDpi : float
- $saveFormat : string
- $sourceData : string
Methods
- __construct() : mixed
- extractMultipleImagesFromSource() : array<string|int, ExtractedImage>
- Extracts images from a page.
- extractPolygonFromPage() : ExtractedImage
- Extracts a cropped portion from an image.
- getInputSource() : LocalInputSource
- Getter for the local input source.
- cropPolygons() : array<string|int, Image>
- Crops every polygon's bounding box from a page, in a single native call.
- encodeCrop() : ExtractedImage
- Encodes a cropped image and wraps it.
- polygonToPairs() : array<string|int, array{0: float, 1: float}>
- Converts a polygon to the `[[x, y], ...]` shape the native extension expects.
Constants
DEFAULT_PDF_DPI
Resolution PDF pages are rendered at before cropping, in dots per inch. Images are cropped at their own resolution.
public
mixed
DEFAULT_PDF_DPI
= 300.0
CROP_QUALITY
JPEG quality used for extracted crops, same as the Python SDK (sufficient for OCR).
private
mixed
CROP_QUALITY
= 70
Properties
$pageCount
public
int
$pageCount
Number of pages in the document.
$filename
protected
string
$filename
Name of the file.
$inputSource
protected
LocalInputSource
$inputSource
Local input object used by the ImageExtractor.
$pageImages
protected
array<string|int, Image>
$pageImages
= []
Array of extracted page images.
$pdfDpi
protected
float
$pdfDpi
= self::DEFAULT_PDF_DPI
$saveFormat
protected
string
$saveFormat
Format to save the image as.
$sourceData
protected
string
$sourceData
Contents of the input source, read once: some sources can only be read once.
Methods
__construct()
public
__construct(LocalInputSource $localInput[, null|string $saveFormat = null ][, float $pdfDpi = self::DEFAULT_PDF_DPI ]) : mixed
Parameters
- $localInput : LocalInputSource
-
Local input, accepts all compatible formats.
- $saveFormat : null|string = null
-
Save format, will be coerced to jpg by default.
- $pdfDpi : float = self::DEFAULT_PDF_DPI
-
Resolution PDF pages are rendered at before cropping; ignored for images.
Tags
extractMultipleImagesFromSource()
Extracts images from a page.
public
extractMultipleImagesFromSource(array<string|int, Polygon|array<string|int, Point>> $polygons, int $pageIndex[, null|string $filenamePrefix = null ][, null|string $format = null ][, null|array<string|int, int> $indexes = null ]) : array<string|int, ExtractedImage>
Parameters
- $polygons : array<string|int, Polygon|array<string|int, Point>>
-
List of polygons to extract.
- $pageIndex : int
-
The page index to extract, begins at 0.
- $filenamePrefix : null|string = null
-
Output filename prefix.
- $format : null|string = null
-
Save format for extracted images. Defaults to the original format.
- $indexes : null|array<string|int, int> = null
-
Index of each polygon, used in its image's file name and index, in the same order as the polygons. Defaults to 0, 1, 2...
Tags
Return values
array<string|int, ExtractedImage> —An array of created images
extractPolygonFromPage()
Extracts a cropped portion from an image.
public
extractPolygonFromPage(Polygon|array<string|int, Point> $polygon, int $pageIndex, int $index[, null|string $filename = null ][, null|string $format = null ]) : ExtractedImage
Parameters
- $polygon : Polygon|array<string|int, Point>
-
Polygon to extract.
- $pageIndex : int
-
Page index to extract from.
- $index : int
-
Index to use for naming the extracted image.
- $filename : null|string = null
-
Output filename.
- $format : null|string = null
-
Output format.
Tags
Return values
ExtractedImage —Extracted image data.
getInputSource()
Getter for the local input source.
public
getInputSource() : LocalInputSource
Return values
LocalInputSourcecropPolygons()
Crops every polygon's bounding box from a page, in a single native call.
protected
cropPolygons(array<string|int, Polygon|array<string|int, Point>> $polygons, int $pageIndex) : array<string|int, Image>
Parameters
- $polygons : array<string|int, Polygon|array<string|int, Point>>
-
Polygons to crop.
- $pageIndex : int
-
Page index, begins at 0.
Tags
Return values
array<string|int, Image> —One image per polygon, in the same order.
encodeCrop()
Encodes a cropped image and wraps it.
protected
encodeCrop(Image $crop, int $pageIndex, int $index, string $filename, string $format) : ExtractedImage
Parameters
- $crop : Image
- $pageIndex : int
- $index : int
- $filename : string
- $format : string
Tags
Return values
ExtractedImagepolygonToPairs()
Converts a polygon to the `[[x, y], ...]` shape the native extension expects.
protected
static polygonToPairs(Polygon|array<string|int, Point> $polygon) : array<string|int, array{0: float, 1: float}>