Clean convert.php, use Hungarian notation

This commit is contained in:
2015-12-19 22:55:15 +00:00
parent 1666c9109e
commit 66adb1a1a4

View File

@@ -1,5 +1,4 @@
<?php <?php
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
@@ -10,19 +9,17 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
$console = new Application(); $oConsole = new Application();
$console $oConsole
->register('run') ->register('run')
->setDefinition( ->setDefinition([
[
new InputOption('export', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Target image export sizes'), new InputOption('export', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Target image export sizes'),
new InputOption('layout', null, InputOption::VALUE_REQUIRED, 'Rendering layout for individual photos', 'gallery-photo'), new InputOption('layout', null, InputOption::VALUE_REQUIRED, 'Rendering layout for individual photos', 'gallery-photo'),
new InputArgument('name', null, InputArgument::REQUIRED, 'Gallery name'), new InputArgument('name', null, InputArgument::REQUIRED, 'Gallery name'),
new InputArgument('assetdir', InputArgument::OPTIONAL, 'Asset directory for exported images', 'asset/gallery'), new InputArgument('assetdir', InputArgument::OPTIONAL, 'Asset directory for exported images', 'asset/gallery'),
new InputArgument('mdowndir', InputArgument::OPTIONAL, 'Markdown directory for dumping individual photo details', 'gallery'), new InputArgument('mdowndir', InputArgument::OPTIONAL, 'Markdown directory for dumping individual photo details', 'gallery'),
] ])
)
->setDescription('Parse a YAML-like gallery configuration and export it.') ->setDescription('Parse a YAML-like gallery configuration and export it.')
->setHelp(' ->setHelp('
The export option will accept values like: The export option will accept values like:
@@ -30,244 +27,209 @@ The export option will accept values like:
1280 - photo will inset with the largest dimension being 1280 1280 - photo will inset with the largest dimension being 1280
') ')
->setCode( ->setCode(
function (InputInterface $input, OutputInterface $output) { function (InputInterface $oInput, OutputInterface $oOutput) {
$gallery = $input->getArgument('name'); $sGallery = $oInput->getArgument('name');
$assetPath = $input->getArgument('assetdir') . '/' . $gallery; $sAssetPath = $oInput->getArgument('assetdir') . '/' . $sGallery;
$renderPath = $input->getArgument('mdowndir') . '/' . $gallery; $sRenderPath = $oInput->getArgument('mdowndir') . '/' . $sGallery;
$layout = $input->getOption('layout'); $sLayout = $oInput->getOption('layout');
$exports = $input->getOption('export'); $sExports = $oInput->getOption('export');
$stdin = stream_get_contents(STDIN); $sStdin = stream_get_contents(STDIN);
$imagine = new Imagine\Gd\Imagine(); $oImagine = new Imagine\Gd\Imagine();
if (!is_dir($assetPath)) { if (!is_dir($sAssetPath)) {
mkdir($assetPath, 0700, true); mkdir($sAssetPath, 0700, true);
} }
if (!is_dir($renderPath)) { if (!is_dir($sRenderPath)) {
mkdir($renderPath, 0700, true); mkdir($sRenderPath, 0700, true);
} }
$stdinPhotos = explode('------------', trim($stdin)); $sStdinPhotos = explode('------------', trim($sStdin));
$photos = []; $aPhotos = [];
// load data // load data
foreach ($sStdinPhotos as $i => $aPhotoRaw) {
$aPhotoSplit = explode('------', trim($aPhotoRaw), 2);
foreach ($stdinPhotos as $i => $photoRaw) { if (empty($aPhotoSplit[0])) {
$photoSplit = explode('------', trim($photoRaw), 2);
if (empty($photoSplit[0])) {
continue; continue;
} }
$photo = array_merge( $aPhoto = array_merge([
[
'ordering' => $i, 'ordering' => $i,
'comment' => isset($photoSplit[1]) ? $photoSplit[1] : null, 'comment' => isset($aPhotoSplit[1]) ? $aPhotoSplit[1] : null,
], ], Yaml::parse($aPhotoSplit[0]));
Yaml::parse($photoSplit[0])
);
$photo['id'] = substr(sha1_file($photo['path']), 0, 7) . '-' . preg_replace('/(-| )+/', '-', preg_replace('/[^a-z0-9 ]/i', '-', preg_replace('/\'/', '', strtolower(preg_replace('/\p{Mn}/u', '', Normalizer::normalize($photo['title'], Normalizer::FORM_KD)))))); $aPhoto['id'] = substr(sha1_file($aPhoto['path']), 0, 7) . '-' . preg_replace('/(-| )+/', '-', preg_replace('/[^a-z0-9 ]/i', '-', preg_replace('/\'/', '', strtolower(preg_replace('/\p{Mn}/u', '', Normalizer::normalize($aPhoto['title'], Normalizer::FORM_KD))))));
$aPhoto['date'] = \DateTime::createFromFormat(
$photo['date'] = \DateTime::createFromFormat(
'l, F j, Y \a\t g:i:s A', 'l, F j, Y \a\t g:i:s A',
$photo['date'] $aPhoto['date']
); );
$photo['exif'] = exif_read_data($photo['path']); $aPhoto['exif'] = exif_read_data($aPhoto['path']);
$aPhotos[] = $aPhoto;
$photos[] = $photo;
} }
// manipulate // manipulate
foreach ($aPhotos as $i => $aPhoto) {
$oOutput->write('<info>' . $aPhoto['id'] . '</info>');
foreach ($photos as $i => $photo) { $aPhoto['sizes'] = [];
$output->write('<info>' . $photo['id'] . '</info>');
$photo['sizes'] = [];
// image exports // image exports
if (0 < count($exports)) { if (0 < count($sExports)) {
$sourceJpg = $imagine->open($photo['path']); $oSourceJpg = $oImagine->open($aPhoto['path']);
if (isset($aPhoto['exif']['Orientation'])) {
if (isset($photo['exif']['Orientation'])) { switch ($aPhoto['exif']['Orientation']) {
switch ($photo['exif']['Orientation']) {
case 2: case 2:
$sourceJpg->mirror(); $oSourceJpg->mirror();
break; break;
case 3: case 3:
$sourceJpg->rotate(180); $oSourceJpg->rotate(180);
break; break;
case 4: case 4:
$sourceJpg->rotate(180)->mirror(); $oSourceJpg->rotate(180)->mirror();
break; break;
case 5: case 5:
$sourceJpg->rotate(90)->mirror(); $oSourceJpg->rotate(90)->mirror();
break; break;
case 6: case 6:
$sourceJpg->rotate(90); $oSourceJpg->rotate(90);
break; break;
case 7: case 7:
$sourceJpg->rotate(-90)->mirror(); $oSourceJpg->rotate(-90)->mirror();
break; break;
case 8: case 8:
$sourceJpg->rotate(-90); $oSourceJpg->rotate(-90);
break; break;
} }
} }
$sourceSize = $sourceJpg->getSize(); $oSourceSize = $oSourceJpg->getSize();
$oOutput->write('[' . $oSourceSize->getWidth() . 'x' . $oSourceSize->getHeight() . ']...');
$output->write('[' . $sourceSize->getWidth() . 'x' . $sourceSize->getHeight() . ']...'); foreach ($sExports as $sExport) {
$oOutput->write('<comment>' . $sExport . '</comment>');
foreach ($exports as $export) { if (false !== strpos($sExport, 'x')) {
$output->write('<comment>' . $export . '</comment>'); list($iW, $iH) = explode('x', $sExport);
$sExportImage = $oSourceJpg->thumbnail(
if (false !== strpos($export, 'x')) { new \Imagine\Image\Box($iW, $iH),
list($w, $h) = explode('x', $export);
$exportImage = $sourceJpg->thumbnail(
new \Imagine\Image\Box($w, $h),
\Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND
); );
} else { } else {
if ('w' == substr($export, -1)) { if ('w' == substr($sExport, -1)) {
$mx = (int) $export; $iX = (int) $sExport;
$my = ($mx * $sourceSize->getHeight() ) / $sourceSize->getWidth(); $iY = ($iX * $oSourceSize->getHeight()) / $oSourceSize->getWidth();
} elseif ('h' == substr($export, -1)) { } elseif ('h' == substr($sExport, -1)) {
$my = (int) $export; $iY = (int) $sExport;
$mx = ($my * $sourceSize->getWidth() ) / $sourceSize->getHeight(); $iX = ($iY * $oSourceSize->getWidth()) / $oSourceSize->getHeight();
} elseif ($sourceSize->getWidth() == max($sourceSize->getWidth(), $sourceSize->getHeight())) { } elseif ($oSourceSize->getWidth() == max($oSourceSize->getWidth(), $oSourceSize->getHeight())) {
$mx = (int) $export; $iX = (int) $sExport;
$my = ($mx * $sourceSize->getHeight()) / $sourceSize->getWidth(); $iY = ($iX * $oSourceSize->getHeight()) / $oSourceSize->getWidth();
} elseif ($sourceSize->getHeight() == max($sourceSize->getWidth(), $sourceSize->getHeight())) { } elseif ($oSourceSize->getHeight() == max($oSourceSize->getWidth(), $oSourceSize->getHeight())) {
$my = (int) $export; $iY = (int) $sExport;
$mx = ($my * $sourceSize->getWidth()) / $sourceSize->getHeight(); $iX = ($iY * $oSourceSize->getWidth()) / $oSourceSize->getHeight();
} }
$sExportImage = $oSourceJpg->thumbnail(
$exportImage = $sourceJpg->thumbnail( new \Imagine\Image\Box(ceil($iX), ceil($iY)),
new \Imagine\Image\Box(ceil($mx), ceil($my)),
\Imagine\Image\ImageInterface::THUMBNAIL_INSET \Imagine\Image\ImageInterface::THUMBNAIL_INSET
); );
} }
$exportSize = $exportImage->getSize(); $sExportsize = $sExportImage->getSize();
$photo['sizes'][$export] = [ $aPhoto['sizes'][$sExport] = [
'width' => $exportSize->getWidth(), 'width' => $sExportsize->getWidth(),
'height' => $exportSize->getHeight(), 'height' => $sExportsize->getHeight(),
]; ];
$output->write('[' . $exportSize->getWidth() . 'x' . $exportSize->getHeight() . ']'); $oOutput->write('[' . $sExportsize->getWidth() . 'x' . $sExportsize->getHeight() . ']');
$exportPath = $assetPath . '/' . $photo['id'] . '~' . $export . '.jpg'; $sExportPath = $sAssetPath . '/' . $aPhoto['id'] . '~' . $sExport . '.jpg';
file_put_contents( file_put_contents(
$exportPath, $sExportPath,
$exportImage->get('jpeg', [ 'quality' => 90 ]) $sExportImage->get('jpeg', ['quality' => 90])
); );
touch($exportPath, $photo['date']->getTimestamp()); touch($sExportPath, $aPhoto['date']->getTimestamp());
$sExportImage = null;
$exportImage = null; $oOutput->write('...');
}
$output->write('...'); $oSourceJpg = null;
} }
$sourceJpg = null; $oOutput->write('<comment>markdown</comment>...');
}
$output->write('<comment>markdown</comment>...'); $aMatter = [
'layout' => $sLayout,
$matter = [ 'title' => $aPhoto['title'],
'layout' => $layout, 'date' => $aPhoto['date']->format('Y-m-d H:i:s'),
'title' => $photo['title'], 'ordering' => $aPhoto['ordering']
'date' => $photo['date']->format('Y-m-d H:i:s'),
'ordering' => $photo['ordering'],
]; ];
if ($photo['exif']) { if ($aPhoto['exif']) {
$matter['exif'] = [ $aMatter['exif'] = [
'make' => $photo['exif']['Make'], 'make' => $aPhoto['exif']['Make'],
'model' => $photo['exif']['Model'], 'model' => $aPhoto['exif']['Model'],
'aperture' => $photo['exif']['COMPUTED']['ApertureFNumber'], 'aperture' => $aPhoto['exif']['COMPUTED']['ApertureFNumber'],
'exposure' => $photo['exif']['ExposureTime'], 'exposure' => $aPhoto['exif']['ExposureTime'],
]; ];
} }
if (isset($photos[$i - 1])) { if (isset($aPhotos[$i - 1])) {
$matter['previous'] = '/gallery/' . $gallery . '/' . $photos[$i - 1]['id']; $aMatter['previous'] = '/gallery/' . $sGallery . '/' . $aPhotos[$i - 1]['id'];
} }
if (isset($photos[$i + 1])) { if (isset($aPhotos[$i + 1])) {
$matter['next'] = '/gallery/' . $gallery . '/' . $photos[$i + 1]['id']; $aMatter['next'] = '/gallery/' . $sGallery . '/' . $aPhotos[$i + 1]['id'];
} }
if ($photo['latitude']) { if ($aPhoto['latitude']) {
$matter['location'] = [ $aMatter['location'] = [
'latitude' => $photo['latitude'], 'latitude' => $aPhoto['latitude'],
'longitude' => $photo['longitude'], 'longitude' => $aPhoto['longitude'],
]; ];
} }
if ($photo['sizes']) { if ($aPhoto['sizes']) {
$matter['sizes'] = $photo['sizes']; $aMatter['sizes'] = $aPhoto['sizes'];
} }
ksort_recursive($matter); ksort_recursive($aMatter);
uasort( uasort(
$matter['sizes'], $aMatter['sizes'],
function ($a, $b) { function ($aA, $aB) {
$aa = $a['width'] * $a['height']; $iSurfaceA = $aA['width'] * $aA['height'];
$bb = $b['width'] * $b['height']; $iSurfaceB = $aB['width'] * $aB['height'];
return $iSurfaceA == $iSurfaceB
if ($aa == $bb) { ? 0
return 0; : (($aa > $bb)? -1 : 1);
}
return ($aa > $bb) ? -1 : 1;
} }
); );
file_put_contents( file_put_contents(
$renderPath . '/' . $photo['id'] . '.md', $sRenderPath . '/' . $aPhoto['id'] . '.md',
'---' . "\n" . Yaml::dump($matter, 4, 2) . '---' . "\n" . ((!empty($photo['comment'])) ? ($photo['comment'] . "\n") : '') '---' . "\n" . Yaml::dump($aMatter, 4, 2) . '---' . "\n" . ((!empty($aPhoto['comment'])) ? ($aPhoto['comment'] . "\n") : '')
); );
$output->writeln('done'); $oOutput->writeln('done');
} }
} });
)
;
$console->run(new ArgvInput(array_merge([ $_SERVER['argv'][0], 'run' ], array_slice($_SERVER['argv'], 1)))); $oConsole->run(new ArgvInput(array_merge([$_SERVER['argv'][0], 'run' ], array_slice($_SERVER['argv'], 1))));
function ksort_recursive (&$array, $sort_flags = SORT_REGULAR) { function ksort_recursive(&$aArray, $mSortFlags = SORT_REGULAR) {
if (!is_array($array)) { if (!is_array($aArray)) {
return false; return false;
} }
foreach ($aArray as &$aSubarray) {
foreach ($array as &$subarray) { ksort_recursive($aSubarray, $mSortFlags);
ksort_recursive($subarray, $sort_flags);
} }
ksort($aArray, $mSortFlags);
ksort($array, $sort_flags);
return true; return true;
} }