Last active
September 23, 2024 07:56
-
-
Save spyesx/3718e013af53d6603b8326ffbd62c31c to your computer and use it in GitHub Desktop.
Convert RAW images to JPG. Actually whatever you want as long as these formats are supported by ImageMagick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Convert RAW images to JPG. Actually whatever you want as long as these formats are supported by ImageMagick. | |
https://www.imagemagick.org/script/formats.php | |
Best used in a terminal to avoid Apache's timeout :) | |
ex : php raw_to_jpg.php | |
Folder tree | |
. | |
├── convert.php | |
├── jpg | |
└── raw | |
*/ | |
$sourceDirName = 'raw'; | |
$rawExtention = 'CR2'; | |
$outputDirName = 'jpg'; | |
$exportFormat = 'jpg'; | |
$imageCompressionQuality = 90; | |
$imageResolution = array(144,144); | |
function getDirContents($path) { | |
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); | |
$files = array(); | |
foreach ($rii as $file) | |
{ | |
if (!$file->isDir()) | |
{ | |
$files[] = $file->getPathname(); | |
} | |
} | |
return $files; | |
} | |
function addDir($path) | |
{ | |
if( ! file_exists($path) ) | |
{ | |
mkdir($path, 0755, true); | |
} | |
} | |
$raws = getDirContents('./'.$sourceDirName.'/'); | |
foreach ($raws as $raw) | |
{ | |
print($raw."\n"); | |
$file = pathinfo($raw); | |
$path = str_replace($sourceDirName, $outputDirName, $file['dirname']); | |
$filename = $file['filename']; | |
$ext = $file['extension']; | |
addDir($path); | |
if($ext == $rawExtention && file_exists($raw)) | |
{ | |
$handle = fopen($raw, 'rb'); | |
$im = new Imagick(); | |
$im->readImageFile($handle); | |
$im->setImageResolution($imageResolution[0],$imageResolution[1]); | |
$im->setImageFormat($exportFormat); | |
$im->setImageCompressionQuality($imageCompressionQuality); | |
$im->writeImage($path.'/'.$filename.'.'.$exportFormat); | |
$im->clear(); | |
$im->destroy(); | |
} | |
print($path.'/'.$filename.'.'.$exportFormat."\n\n"); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for file in ./raw/**.nef; do convert "$file" -set colorspace RGB -colorspace sRGB -quality 100% "./jpg/$(basename $file).jpg"; printf "$(basename $file) converted to jpg \n"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment