@pepebe wrote the following code to import a directory of images into MoreGallery. Put it in a snippet, or prepend the boilerplate code to load the modx object externally and run it from the CLI.
Shouldn’t be too hard to adapt to loop over different directories to create multiple galleries at once, or to import from Gallery, so figured it would be useful to share:
<?php
/*
import images into a moreGallery resource
*/
$resourceId = $modx->getOption('resourceId', $scriptProperties, 1582);
$dir = $modx->getOption('dir', $scriptProperties, 'id/0000/100/10/9/');
$extensions = array('jpeg', 'jpg', 'png');
$dir = MODX_BASE_PATH . $dir;
$msg = array();
$images = array();// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$pathinfo = pathinfo($file);
if (filetype($dir . $file) == 'file' and in_array($pathinfo['extension'], $extensions)) {
//$msg['dir'][] = array('filename' => $file, 'extension' => $pathinfo['extension']);
$images[] = $file;
}
}
closedir($dh);
}
}
asort($images);
//return "<pre>".print_r($images,true)."<pre>";
foreach ($images as $image) {
$filepath = $dir . $image;
$response = $modx->runProcessor(
'mgr/images/import_file'
, array(
'file' => $filepath,
'filename' => basename($filepath),
'resource' => $resourceId,
)
, array(
'processors_path' => 'core/components/moregallery/processors/'
)
);
if ($response->isError()) {
$msg['error'][] = $response->getMessage();
} else {
$arr = $response->getObject();
$msg['success'][] = $arr['filename'] . " imported";
}
}
return "<pre>" . print_r($msg) . "</pre>";