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