PHP code example of kraenzle-ritter / puidentify

1. Go to this page and download the library: Download kraenzle-ritter/puidentify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kraenzle-ritter / puidentify example snippets


use Puidentify\Identifier;
use Puidentify\Engine\FidoEngine;
use Puidentify\Engine\SiegfriedEngine;

// Create identifier with engines (priority order)
$identifier = new Identifier([
    new SiegfriedEngine(), // Primary
    new FidoEngine()       // Fallback
]);

// Identify a file
$result = $identifier->identify('/path/to/file.pdf');

if ($result) {
    echo $result; // "PUID: fmt/276 (PDF/A-1b) via Siegfried"
    
    // Access individual properties
    echo "PUID: " . $result->puid;
    echo "Format: " . $result->formatName;
    echo "Engine: " . $result->engine;
}

use Puidentify\Engine\SiegfriedEngine;
use Puidentify\Engine\FidoEngine;

$identifier = new Identifier([
    new SiegfriedEngine('/usr/local/bin/sf'),
    new FidoEngine('/opt/fido/bin/fido')
]);

$availableEngines = $identifier->getAvailableEngines();
echo "Available engines: " . count($availableEngines);

$result = $identifier->identify('/path/to/file.pdf');

// Access basic properties
echo "PUID: " . $result->puid;
echo "Format: " . $result->formatName;
echo "Engine: " . $result->engine;

// Access extended properties (if available)
if ($result->hasMimeType()) {
    echo "MIME: " . $result->mimeType;
}

if ($result->hasVersion()) {
    echo "Version: " . $result->version;
}

if ($result->hasConfidence()) {
    echo "Confidence: " . ($result->confidence * 100) . "%";
}

// Convert to array with all properties
$array = $result->toArray();
// [
//     'puid' => 'fmt/276',
//     'formatName' => 'PDF/A-1b', 
//     'engine' => 'Siegfried',
//     'mimeType' => 'application/pdf',
//     'fileExtension' => 'pdf',
//     'version' => '1.4',
//     'confidence' => 0.95,
//     'rawOutput' => [...] // Full engine output
// ]

// Enhanced string representation
echo $result; // "PUID: fmt/276 (PDF/A-1b) v1.4 [95%] via Siegfried"

use Puidentify\Exception\FileNotFoundException;
use Puidentify\Exception\EngineException;

try {
    $result = $identifier->identify('/path/to/file.pdf');
} catch (FileNotFoundException $e) {
    echo "File not found or not readable: " . $e->getMessage();
} catch (EngineException $e) {
    echo "Engine error: " . $e->getMessage();
}