From 1022d750a501166cfadb833b6ae7e4546f1c7b50 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Wed, 23 Dec 2015 21:17:25 +0000 Subject: [PATCH] Towards import of cleaned galleries from wordpress --- _scripts/generate-gallery-yaml.php | 38 +++++++++++++------ _scripts/generate-gallery.php | 50 +++++++++++++++++------- _scripts/wordpress-galleries.php | 61 ++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 _scripts/wordpress-galleries.php diff --git a/_scripts/generate-gallery-yaml.php b/_scripts/generate-gallery-yaml.php index 20593bc..bc200e2 100644 --- a/_scripts/generate-gallery-yaml.php +++ b/_scripts/generate-gallery-yaml.php @@ -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)); } \ No newline at end of file diff --git a/_scripts/generate-gallery.php b/_scripts/generate-gallery.php index dea8542..aa8e2a8 100644 --- a/_scripts/generate-gallery.php +++ b/_scripts/generate-gallery.php @@ -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('Import directory does not exist'); @@ -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(' [skipping]'); + 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(' [skipping]'); + 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])) { diff --git a/_scripts/wordpress-galleries.php b/_scripts/wordpress-galleries.php new file mode 100644 index 0000000..1ec0998 --- /dev/null +++ b/_scripts/wordpress-galleries.php @@ -0,0 +1,61 @@ +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'); +} \ No newline at end of file