Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active September 23, 2024 07:56
Show Gist options
  • Save spyesx/3718e013af53d6603b8326ffbd62c31c to your computer and use it in GitHub Desktop.
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.
<?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");
}
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