Skip to content

Files, Images & PDF

File handling, image resize, PDF/QR and media conversion.

16 functions. All require apis/main_webservices.php to be included.


On this page: ExtractDataFromImgPdf ยท createJpgFrom64 ยท curl_post ยท darkenColor ยท genQrCodePng ยท img2pdf ยท isBase64 ยท lightenColor ยท logoFile ยท merciPDF ยท newImage ยท newTempFile ยท pdf2png ยท saveFile ยท smart_resize_image ยท writeToImage


ExtractDataFromImgPdf()

Prototype

ExtractDataFromImgPdf($file,$html=false)

Description

  • Extracts tabular data from an image/PDF that contains a table layout (OCR-style).
  • Optionally returns the data as HTML.

Parameters

  • <$file>: PDF File to extract data from
  • <$html>: TRUE if you need data as HTML. Defaults false.

Returns

  • String table data on success, or false if extraction fails.

Return example

$data = ExtractDataFromImgPdf('/path/scan.pdf');
// table text  (or false)

Usage example

$data = ExtractDataFromImgPdf($pdffile);

:material-file-code: Source: apis/core4.php


createJpgFrom64()

Prototype

createJpgFrom64($photodata,$path))

Description

  • Decodes base64 image data and writes it as a JPG file at the given path.
  • Use to persist images captured/posted as base64 (signatures, camera).

Parameters

  • [$photodata]: base64 encoded image data
  • [$path]: path including file name to save the file

Returns

  • Nothing โ€” writes the JPG file as a side effect.

Return example

createJpgFrom64($base64, '/path/sign.jpg');
// file written

Usage example

createJpgFrom64($base64encoded,$filepath);

:material-file-code: Source: apis/core4.php


curl_post()

Prototype

curl_post($url, array $post = NULL, array $options = array())

Description

  • Executes an HTTP request via cURL (POST by default) with optional post data and cURL options.
  • Use to call external APIs/webhooks from server code.

Parameters

  • <$url>: URL to call
  • [$post]: POST parameters as associative array
  • [$options]: CURL Options

Returns

  • String โ€” the response body returned by the endpoint.

Return example

$resp = curl_post('https://api.x.com/v1', ['key'=>'v']);
// raw response body

Usage example

curl_post('ping.com/ping')

:material-file-code: Source: apis/core2_a.php


darkenColor()

Prototype

darkenColor( $color, $darken=0 )

Description

  • Returns a darker shade of a hex colour by the given amount.
  • Use for hover/border variants in generated styles.

Parameters

  • [$color]: hex color code eg. #FF0000

Returns

  • String โ€” the resulting #RGB/#RRGGBB colour.

Return example

echo darkenColor('#3366cc', 20);
// e.g. '#284fa3'

Usage example

$color = darkenColor('#FF0000',50);

:material-file-code: Source: apis/core2_a.php


genQrCodePng()

Prototype

genQrCodePng($fieldvalue)

Description

  • Generates a QR-code PNG image encoding the given value and returns the file name.
  • Use for payment/UPI QR, document verification links, asset tags.

Parameters

  • <$fieldvalue>: Value to be encoded in the QR Code image

Returns

  • String โ€” the file name of the generated QR PNG.

Return example

$qr = genQrCodePng('upi://pay?pa=abc@bank&am=100');
// e.g. 'qr_8f3a.png'

Usage example

$qrimg = genQrCodePng('testing the qr code image creation with this string');

:material-file-code: Source: apis/core2_a.php


img2pdf()

Prototype

img2pdf()

Description

  • Converts an image file into a PDF file.
  • Use to wrap scans/photos into a printable/archivable PDF.

Parameters

  • none

Returns

  • Boolean โ€” true on success, else false.

Return example

$ok = img2pdf();
// returns: true | false

Usage example

$pdf = img2pdf();  //returns the converted filename with full path

:material-file-code: Source: apis/core4.php


isBase64()

Prototype

isBase64($data)

Description

  • Checks whether a value is base64-encoded data.
  • Use to branch between raw and encoded payload handling.

Parameters

  • <$data>: Data to check

Returns

  • Boolean โ€” true if the data looks base64-encoded.

Return example

var_dump(isBase64($x));
// true | false

Usage example

$isEncoded = isBase64($data);

:material-file-code: Source: apis/core4.php


lightenColor()

Prototype

lightenColor( $color )

Description

  • Returns a lighter shade of a hex colour.
  • Use for backgrounds/tints derived from a base colour.

Parameters

  • [$color]: hex color code eg. #FF0000

Returns

  • String โ€” the resulting lighter #RGB/#RRGGBB colour.

Return example

echo lightenColor('#3366cc');
// e.g. '#6f93de'

Usage example

$color = lightenColor('#FF0000',50);

:material-file-code: Source: apis/core2_a.php


logoFile()

Prototype

logoFile($fullpath=true)

Description

  • Returns the path to the current project's logo image (project_xlogo.jpg).
  • Use to embed the client logo in prints, PDFs and headers.

Parameters

  • none

Returns

  • String โ€” full server path (or relative) to the project logo file.

Return example

$logo = logoFile();
// e.g. '/.../shreeart/shreeart_xlogo.jpg'

Usage example

$logo = logoFile();

:material-file-code: Source: apis/core2_a.php


merciPDF()

Prototype

merciPDF($mode='P', $unit='mm', $size='A4')

Description

  • Initialises and returns a PDF object (FPDF-based) for the given orientation, unit and size.
  • Use as the starting point for building printable PDF documents.

Parameters

  • [$mode]: P for Portrait or L for Landscape
  • [$unit]: mm for milimeters, in for inches, cm for Centimeters
  • [$size]: A4 or other standard page size.

Returns

  • Object โ€” a ready-to-use PDF instance.

Return example

$pdf = merciPDF('P', 'mm', 'A4');
$pdf->AddPage();
$pdf->Cell(0, 10, 'Hello');

Usage example

$pdf = merciPDF();
$pdf->say( 0, 0, "0       10        20        30        40        50        60        70        80" );
$pdf->sayimage('http://domain.com/wkrtex/wkrtex_xlogo.jpg',10,10,5,20);
$pdf->skip();
for ($i=1; $i < 70; $i++){
 if ($i == 10){
    $pdf->setBold( true );
 }
 elseif ($i == 15){
    $pdf->setBold( true );
    $pdf->setSize( 22 );
 }
 else{
    $pdf->setBold();
    $pdf->setSize( 12 );
 }
 if ($i == 20){
    $pdf->box(20,2,4,80);  //Box row, box col, box height in rows, box width in columns, fill color defaults to #f2f2f2 greyscale.
 }
 if ($i == 30){
    $pdf->$pdf->line(40,0,0.01,82);  //Line Row, Line Col, Line Height, Line Width
 }
 $pdf->say( 0,  0, "XXX Hello World! " . $pdf->row );
 $pdf->say( 0, 40, "YYY Hello World! " . $pdf->row );
 $pdf->skip();
}
$pdf->end();

:material-file-code: Source: apis/core2_a.php


newImage()

Prototype

NewImage($filebase,$ext='.jpg',$base='')

Description

  • Returns a new temporary image file name for a given base/extension.
  • Use to allocate a target name before writing a generated image.

Parameters

  • <$filebase>: File base name
  • [$ext]: File extension
  • [$base]: Leave blank

Returns

  • String โ€” a new temporary image file name.

Return example

$img = NewImage('item', '.jpg');
// e.g. 'item_5f2a.jpg'

:material-file-code: Source: apis/core2_a.php


newTempFile()

Prototype

newTempFile( $ext='tmp', $prefix='', $basename='' )

Description

  • Returns an available temporary file name in the common folder, with optional extension/prefix.
  • Use to stage generated files before moving/serving them.

Parameters

  • [$ext]: desired extension. Defaults to .tmp
  • [$prefix]: desired file prefix
  • [$basename]: desired file basename

Returns

  • String โ€” a free temporary file path/name.

Return example

$f = newTempFile('csv');
// e.g. '../common/tmp_8f3a2.csv'

Usage example

$tmpzip = newTempFile( 'zip', '', '' );

:material-file-code: Source: apis/core2_a.php


pdf2png()

Prototype

pdf2png()

Description

  • Converts the first page of a PDF into a PNG image.
  • Use to generate document thumbnails/previews.

Parameters

  • none

Returns

  • Boolean โ€” true on successful conversion, else false.

Return example

$ok = pdf2png();
// returns: true | false

Usage example

$imagefile = pdf2png();  //returns the converted filename with full path

:material-file-code: Source: apis/core4.php


saveFile()

Prototype

SaveFile( $target_file, $back, $lresize=true, $var='photo', $lwarn=true, $saveInHome=true )

Description

  • Saves an uploaded file from the browser to the server, with optional image resize and home-folder placement.
  • Use in upload handlers; choose the POST var and whether to warn on issues.

Parameters

  • <$target_file>: Destination file
  • <$back>: Leave BLANK
  • <$lresize>: True to auto-resize the file. False to save in original size. Applies to image files only.
  • <$var>: Field variable name of the table
  • <$lwarn>: Gives online warning to browser client.
  • <$saveInHome>: Save in home folder of the project.

Returns

  • Integer status โ€” 0 on success, -1 if no filename, -2 on save error.

Return example

$st = SaveFile($targetFile, $back, true, 'photo');
// 0 = saved, -1/-2 = error

Usage example

SaveFile( 'myfile.jpg' );

:material-file-code: Source: apis/core2_a.php


smart_resize_image()

Prototype

smart_resize_image($file,$string=null,$width=800,$height=0,$proportional=true,$output='file',$delete_original=true,$use_linux_commands=false,$quality=100)

Description

  • Resizes an image with proportional scaling and quality control, outputting to file or browser.
  • Optionally removes the original and can use native Linux commands for speed.

Parameters

  • $file โ€” Image File name to resize
  • $string โ€” Image buffer as string / base64 encoded
  • $width โ€” new width of image
  • $height โ€” new height of image
  • $proportional โ€” Keep image proportional. Default is true
  • $output โ€” name of new file
  • $delete_original โ€” Delete original file. Defaults to true
  • $use_linux_commands โ€” if set to true will use "rm" to delete the image, if false will use PHP unlink
  • $quality โ€” enter 1-100 (100 is best quality) default is 100

Returns

  • Boolean or image resource โ€” true/resource on success, false on failure.

Return example

$ok = smart_resize_image($path, null, 800, 0, true, 'file');
// returns: true | false

Usage example

$eom = eom(3);

:material-file-code: Source: apis/core2_a.php


writeToImage()

Prototype

writeToImage($image='',$str='')

Description

  • Stamps text onto an image and saves the result as a new image file.
  • Use for watermarks, captions or annotating snapshots.

Parameters

  • $image โ€” image file name with path
  • $str โ€” String to be written to the image

Returns

  • String โ€” the new (annotated) image file name.

Return example

$out = writeToImage('photo.jpg', 'APPROVED');
// e.g. 'photo_wm.jpg'

Usage example

writeToImage($imagePath,'Welcome to Inscriptions');

:material-file-code: Source: apis/core4.php