├── .gitignore ├── composer.json ├── readme.md └── src └── Beije └── CameraRaw ├── Bridge └── Laravel │ ├── CameraRawFacade.php │ └── CameraRawServiceProvider.php └── CameraRaw.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vkollin/php-camera-raw", 3 | "type": "library", 4 | "description": "A class that can fetch preview images from camera raw files.", 5 | "keywords": [ 6 | "image", 7 | "raw", 8 | "laravel" 9 | ], 10 | "homepage": "https://github.com/beije/php-camera-raw", 11 | "minimum-stability": "stable", 12 | "authors": [ 13 | { 14 | "name": "Benjamin Horn", 15 | "email": "beije@benjaminhorn.se", 16 | "homepage": "http://benjaminhorn.io", 17 | "role": "Developer" 18 | } 19 | ], 20 | "autoload": { 21 | "psr-4": { 22 | "Beije\\CameraRaw\\": "src/Beije/CameraRaw" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Camera Raw parsing with php 2 | 3 | A class that can fetch preview images from camera raw files. 4 | 5 | In order to use this class you'll need a few php mods and 6 | some other software installed on your server. 7 | 8 | - imagemagick (Manage images) 9 | - ufraw (Reading and converting raw files) 10 | - exiv2 (Extracting previews from raw files) 11 | 12 | [Click here for more information and documentation.](http://benjaminhorn.io/code/extracting-thumbnails-from-camera-raw-files-cr2-and-nef-with-php/) -------------------------------------------------------------------------------- /src/Beije/CameraRaw/Bridge/Laravel/CameraRawFacade.php: -------------------------------------------------------------------------------- 1 | app->bindShared('cameraraw', function () { 23 | return new CameraRaw(); 24 | }); 25 | } 26 | 27 | /** 28 | * Get the services provided by the provider. 29 | * 30 | * @return string[] 31 | */ 32 | public function provides() 33 | { 34 | return array('cameraraw'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Beije/CameraRaw/CameraRaw.php: -------------------------------------------------------------------------------- 1 | setImageFormat('jpg'); 75 | $im->setImageCompressionQuality($quality); 76 | $im->stripImage(); 77 | $im->thumbnailImage($width, $height, true); 78 | $im->writeImage($targetFilePath); 79 | $im->clear(); 80 | $im->destroy(); 81 | } 82 | 83 | /** 84 | * Extracts a preview image from an image and saves the 85 | * image to the designated path. 86 | * 87 | * @param string $sourceFilePath the path to the original file 88 | * @param string $targetFilePath the path to the new file 89 | * @param int $previewNumber which preview (defaults to the highest preview) 90 | * 91 | * @throws \InvalidArgumentException 92 | * @throws \Exception 93 | */ 94 | public static function extractPreview($sourceFilePath, $targetFilePath, $previewNumber = null) 95 | { 96 | // check that the source file exists. 97 | if (!file_exists($sourceFilePath)) { 98 | throw new \InvalidArgumentException('Incorrect source filepath given '.$sourceFilePath); 99 | } 100 | 101 | // fetch the all the preview images (and verify that there indeed exit previews) 102 | $numberOfPreviews = self::embeddedPreviews($sourceFilePath); 103 | if (count($numberOfPreviews) == 0) { 104 | throw new \Exception('No embedded previews detected'); 105 | } 106 | 107 | // default to the last preview 108 | if (is_null($previewNumber)) { 109 | $previewNumber = count($numberOfPreviews); 110 | } 111 | 112 | $previewNumber = intval($previewNumber); 113 | $filename = pathinfo($sourceFilePath, PATHINFO_FILENAME); 114 | $return = array(); 115 | 116 | // generate the preview with exiv2, save it to the temp directory 117 | exec('exiv2 -ep'.$previewNumber.' -l '.sys_get_temp_dir().' "'.$sourceFilePath.'"', $return); 118 | $previewFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename.'-preview'.$previewNumber.'.jpg'; 119 | 120 | // check that the preview got saved correctly 121 | if (!file_exists($previewFilePath)) { 122 | throw new \Exception('No preview file.'); 123 | } 124 | 125 | // copy the preview to the target path and remove the original one in temp 126 | copy($previewFilePath, $targetFilePath); 127 | unlink($previewFilePath); 128 | } 129 | } 130 | --------------------------------------------------------------------------------