Towards import of cleaned galleries from wordpress
This commit is contained in:
@@ -3,21 +3,35 @@ require __DIR__ . '/../_php/autoload.php';
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
writeMetaYaml($argv[1]);
|
||||
|
||||
function writeMetaYaml($sDir) {
|
||||
$sDir = rtrim($sDir, '"\'/\\');
|
||||
$aMeta = [
|
||||
'gallery' => ['name' => '', 'title' => '', 'description' => ''],
|
||||
'files' => null
|
||||
];
|
||||
if (isset($argv[1])) {
|
||||
$sDir = realpath(rtrim($argv[1]));
|
||||
if (!is_dir($sDir)) {
|
||||
die('Directory does not exist');
|
||||
}
|
||||
$aFiles = glob($sDir . '/*.jpg');
|
||||
foreach ($aFiles as $sFile) {
|
||||
$aMeta['files'][basename($sFile)] = ['title' => '', 'comment' => ''];
|
||||
$aYaml = getMetaYaml($sDir);
|
||||
writeMetaYaml($sDir, $aYaml);
|
||||
}
|
||||
|
||||
function getMetaYaml($sDir, $bFiles = true, $sName = '', $sTitle = '', $sDescription = '') {
|
||||
$sDir = rtrim($sDir, '"\'/\\');
|
||||
$aMeta = [
|
||||
'gallery' => ['name' => $sName, 'title' => $sTitle, 'description' => $sDescription],
|
||||
'files' => null
|
||||
];
|
||||
if ($bFiles) {
|
||||
$aFiles = glob($sDir . '/*.jpg');
|
||||
foreach ($aFiles as $sFile) {
|
||||
$aMeta['files'][basename($sFile)] = ['title' => '', 'comment' => ''];
|
||||
}
|
||||
}
|
||||
$sYaml = str_replace("''", null, Yaml::dump($aMeta, 4, 2));
|
||||
return $aMeta;
|
||||
}
|
||||
|
||||
function writeMetaYaml($sDir, $aYaml) {
|
||||
$sYaml = str_replace("''", null, yamlDump($aYaml));
|
||||
file_put_contents($sDir . '/meta.yaml', $sYaml);
|
||||
}
|
||||
|
||||
function yamlDump($aData) {
|
||||
return str_replace("'", null, Yaml::dump($aData, 4, 2));
|
||||
}
|
||||
@@ -45,7 +45,7 @@ $oConsole
|
||||
if (!is_dir($sRenderPath)) {
|
||||
mkdir($sRenderPath, 0700, true);
|
||||
}
|
||||
|
||||
|
||||
// Check directory and presence of YAML file
|
||||
if (!is_dir($sDir)) {
|
||||
$oOutput->writeln('<error>Import directory does not exist</error>');
|
||||
@@ -69,7 +69,7 @@ $oConsole
|
||||
|
||||
// Loop over files
|
||||
$sHighlight = null;
|
||||
$aPhotos = [];
|
||||
$aPhotos = $aLongitude = $aLatitude = [];
|
||||
foreach ($aFiles as $i => $sFile) {
|
||||
// Build photo information
|
||||
$aPhoto = [
|
||||
@@ -95,13 +95,23 @@ $oConsole
|
||||
if (isset($aPhoto['exif']['GPSLongitude'])) {
|
||||
$aPhoto = array_merge($aPhoto, [
|
||||
'longitude' => coordinateToDegrees($aPhoto['exif']['GPSLongitude'], $aPhoto['exif']['GPSLongitudeRef']),
|
||||
'latitude' => coordinateToDegrees($aPhoto['exif']['GPSLatitude'], $aPhoto['exif']['GPSLatitudeRef']),
|
||||
'altitude' => fractionToFloat($aPhoto['exif']['GPSAltitude']),
|
||||
'direction' => fractionToFloat($aPhoto['exif']['GPSImgDirection'])]);
|
||||
'latitude' => coordinateToDegrees($aPhoto['exif']['GPSLatitude'], $aPhoto['exif']['GPSLatitudeRef'])
|
||||
]);
|
||||
if (isset($aPhoto['exif']['GPSAltitude'])) {
|
||||
$aPhoto['altitude'] = fractionToFloat($aPhoto['exif']['GPSAltitude']);
|
||||
}
|
||||
if (isset($aPhoto['exif']['GPSImgDirection'])) {
|
||||
$aPhoto['direction'] = fractionToFloat($aPhoto['exif']['GPSImgDirection']);
|
||||
}
|
||||
$aLongitude[] = $aPhoto['longitude'];
|
||||
$aLatitude[] = $aPhoto['latitude'];
|
||||
}
|
||||
$aPhoto['date'] = new DateTime($aPhoto['exif']['DateTimeOriginal']);
|
||||
if (isset($aPhoto['exif']['DateTimeOriginal'])) {
|
||||
$aPhoto['date'] = new DateTime($aPhoto['exif']['DateTimeOriginal']);
|
||||
} else {
|
||||
$oDate = new DateTime();
|
||||
$aPhoto['date'] = $oDate->setTimestamp($aPhoto['exif']['FileDateTime']);
|
||||
}
|
||||
$aPhotos[] = $aPhoto;
|
||||
}
|
||||
|
||||
@@ -147,6 +157,10 @@ $oConsole
|
||||
|
||||
if (false !== strpos($sExport, 'x')) {
|
||||
list($iW, $iH) = explode('x', $sExport);
|
||||
if ($iW > $oSourceSize->getWidth() || $iH > $oSourceSize->getHeight()) {
|
||||
$oOutput->writeln(' <comment>[skipping]</comment>');
|
||||
continue;
|
||||
}
|
||||
$sExportImage = $oSourceJpg->thumbnail(
|
||||
new \Imagine\Image\Box($iW, $iH),
|
||||
\Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND
|
||||
@@ -164,7 +178,13 @@ $oConsole
|
||||
} elseif ($oSourceSize->getHeight() == max($oSourceSize->getWidth(), $oSourceSize->getHeight())) {
|
||||
$iY = (int) $sExport;
|
||||
$iX = ($iY * $oSourceSize->getWidth()) / $oSourceSize->getHeight();
|
||||
}
|
||||
}
|
||||
|
||||
if ($iX > $oSourceSize->getWidth() || $iY > $oSourceSize->getHeight()) {
|
||||
$oOutput->writeln(' <comment>[skipping]</comment>');
|
||||
continue;
|
||||
}
|
||||
|
||||
$sExportImage = $oSourceJpg->thumbnail(
|
||||
new \Imagine\Image\Box(ceil($iX), ceil($iY)),
|
||||
\Imagine\Image\ImageInterface::THUMBNAIL_INSET
|
||||
@@ -207,12 +227,16 @@ $oConsole
|
||||
$oEndDate = $i > 0 ? max($oEndDate, $aPhoto['date']) : $aPhoto['date'];
|
||||
|
||||
if (isset($aPhoto['exif']['Make'])) {
|
||||
$aMatter['exif'] = [
|
||||
'make' => $aPhoto['exif']['Make'],
|
||||
'model' => $aPhoto['exif']['Model'],
|
||||
'aperture' => $aPhoto['exif']['COMPUTED']['ApertureFNumber'],
|
||||
'exposure' => $aPhoto['exif']['ExposureTime'],
|
||||
];
|
||||
$aMatter['exif']['make'] = $aPhoto['exif']['Make'];
|
||||
if (isset($aPhoto['exif']['Model'])) {
|
||||
$aMatter['model'] = $aPhoto['exif']['Model'];
|
||||
}
|
||||
if (isset($aPhoto['exif']['COMPUTED']['ApertureFNumber'])) {
|
||||
$aMatter['aperture'] = $aPhoto['exif']['COMPUTED']['ApertureFNumber'];
|
||||
}
|
||||
if (isset($aPhoto['exif']['ExposureTime'])) {
|
||||
$aMatter['exposure'] = $aPhoto['exif']['ExposureTime'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($aPhotos[$i - 1])) {
|
||||
|
||||
61
_scripts/wordpress-galleries.php
Normal file
61
_scripts/wordpress-galleries.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
if (isset($argv[1])) {
|
||||
$sSourceDir = realpath(rtrim($argv[1]));
|
||||
unset($argv[1]);
|
||||
if (!is_dir($sSourceDir)) {
|
||||
die('Directory does not exist');
|
||||
}
|
||||
}
|
||||
|
||||
require __DIR__ . '/generate-gallery-yaml.php';
|
||||
require __DIR__ . '/../_php/autoload.php';
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
$oPDO = new PDO('mysql:dbname=blog;host=localhost;port=3311', 'root', 'root');
|
||||
|
||||
$sQuery = 'SELECT gid, path, title, galdesc FROM wp_ngg_gallery';
|
||||
if ($oStatement = $oPDO->query($sQuery)) {
|
||||
$aCommands = [];
|
||||
foreach ($oStatement as $aRow) {
|
||||
$iId = $aRow['gid'];
|
||||
$sTitle = $aRow['title'];
|
||||
$sPath = $aRow['path'];
|
||||
$sDescription = $aRow['galdesc'];
|
||||
$sQuery = sprintf('SELECT pid, filename, description, imagedate FROM wp_ngg_pictures WHERE galleryid = %d', $iId);
|
||||
$aFiles = [];
|
||||
if ($oStatement = $oPDO->query($sQuery)) {
|
||||
foreach ($oStatement as $aRow) {
|
||||
$iYear = substr($aRow['imagedate'], 0, 4);
|
||||
$aFiles[basename($aRow['filename'])] = [
|
||||
'id' => $aRow['pid'],
|
||||
'title' => '',
|
||||
'comment' => utf8_encode($aRow['description'])
|
||||
];
|
||||
}
|
||||
$sDir = str_replace('wp-content/gallery/', null, $sPath);
|
||||
if (preg_match('/-[\d]{4}/', $sDir, $aMatch)) {
|
||||
$sCleanDir = str_replace($aMatch[0], null, $sDir);
|
||||
} else {
|
||||
$sCleanDir = $sDir;
|
||||
}
|
||||
$sName = sprintf('%d-%s', $iYear, strtolower(str_replace(' ', '-', $sCleanDir)));
|
||||
$aYaml = getMetaYaml($sDir, false, $sName, $sTitle, utf8_encode($sDescription));
|
||||
$aYaml['id'] = $iId;
|
||||
$aYaml['files'] = $aFiles;
|
||||
$sGalleryDir = sprintf('%s/%s', $sSourceDir, $sDir);
|
||||
if (!is_dir($sGalleryDir)) {
|
||||
printf("Path does not exist: %s\n", $sGalleryDir);
|
||||
continue;
|
||||
}
|
||||
writeMetaYaml($sGalleryDir, $aYaml);
|
||||
$aCommands[] = sprintf('call _scripts/generate-gallery %s "%s"', $sName, $sGalleryDir);
|
||||
} else {
|
||||
die('Failed to get pictures data');
|
||||
}
|
||||
}
|
||||
$aCommands[] = 'yekyll build';
|
||||
file_put_contents(sprintf('%s/commands.bat', $sSourceDir), implode("\n", $aCommands));
|
||||
} else {
|
||||
die('Failed to get gallery data');
|
||||
}
|
||||
Reference in New Issue
Block a user