PHP code example of ronanlenouvel / raw-preview-extractor

1. Go to this page and download the library: Download ronanlenouvel/raw-preview-extractor 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/ */

    

ronanlenouvel / raw-preview-extractor example snippets


use RonanLenouvel\RawPreviewExtractor\Exception\RawPreviewExtractorException;
use RonanLenouvel\RawPreviewExtractor\RawPreviewExtractor;

$extractor = RawPreviewExtractor::createDefault();

try {
    $preview = $extractor->extract('/path/to/photo.cr2');

    file_put_contents('/path/to/thumbnail.jpg', $preview->jpegData);

    echo $preview->width, 'x', $preview->height;
    echo $preview->sourceFormat->name;
} catch (RawPreviewExtractorException) {
    // Every exception thrown by this package implements this interface,
    // so a single catch is enough to degrade gracefully.
}

$preview->orientation;                  // Orientation::Rotate90
$preview->orientation->degrees();       // 90
$preview->orientation->isUpright();     // false
$preview->orientation->isMirrored();    // false — 4 of the 8 EXIF values are mirrors
$preview->orientation->swapsDimensions();  // true — width and height swap on a quarter turn

printf('<img src="thumb.jpg" style="transform: rotate(%ddeg)">',
    $preview->orientation->degrees());

$image = imagecreatefromstring($preview->jpegData);

if (!$preview->orientation->isUpright()) {
    $image = imagerotate($image, -$preview->orientation->degrees(), 0);
}

if ($extractor->supports($path)) {
    // …
}

$extractor = RawPreviewExtractor::createDefault();

foreach (glob('/photos/*') as $path) {
    try {
        $preview = $extractor->extract($path);
        file_put_contents("/thumbs/" . basename($path) . '.jpg', $preview->jpegData);
    } catch (RawPreviewExtractorException) {
        continue;  // not a RAW, no preview, or corrupt — skip it
    }
}

$preview = $extractor->extract('/path/to/photo.cr2');

$meta = $preview->metadata;              // RawMetadata|null

$meta?->dateTimeOriginal;                // "2024:06:15 12:30:45" (raw EXIF form)
$meta?->fNumber;                         // 2.8   (aperture, float)
$meta?->exposureTime;                    // "1/250" (shutter, fraction kept intact)
$meta?->iso;                             // 400
$meta?->focalLength;                     // 50.0  (millimetres)
$meta?->lensModel;                       // "RF50mm F1.8 STM"
$meta?->cameraMake;                      // "Canon"
$meta?->cameraModel;                     // "Canon EOS R5"

if ($meta !== null && !$meta->isEmpty()) {
    // display the shooting settings
}

// config/bundles.php
return [
    // …
    RonanLenouvel\RawPreviewExtractor\Bridge\Symfony\RawPreviewExtractorBundle::class => ['all' => true],
];
bash
php bin/audit-cameras.php Canon 25    # 25 Canon models, spread across the range
php bin/audit-cameras.php all 500     # everything