Skip to content

Instantly share code, notes, and snippets.

@sashabeep
Last active April 19, 2024 13:13
Show Gist options
  • Save sashabeep/d5354de17aa0333e02a9c791e31c9105 to your computer and use it in GitHub Desktop.
Save sashabeep/d5354de17aa0333e02a9c791e31c9105 to your computer and use it in GitHub Desktop.
Simple Gallery import from static folders
<?php
define('MODX_API_MODE', true);
define('MODX_BASE_PATH', __DIR__ . '/');
define('MODX_BASE_URL', '/');
define('MODX_SITE_URL', 'http://www.sitename.ru/');
include_once("index.php");
$modx->db->connect();
if (empty ($modx->config)) {
$modx->getSettings();
}
include_once(MODX_BASE_PATH.'assets/plugins/simplegallery/lib/table.class.php');
$modx->invokeEvent("OnManagerPageInit");
$fs = \Helpers\FS::getInstance();
$sg = new \SimpleGallery\sgData($modx);
$galFolders = [];
$files = scandir("./assets/galleries");
$scanned_directory = array_diff($files, array('..', '.'));
//var_dump($scanned_directory);
foreach($scanned_directory as $key => $folder){
echo("adding to ".$folder.PHP_EOL);
$path = "./assets/galleries/".$folder."/";
$files = scandir($path);
$files = array_diff($files, array('..', '.'));
// var_dump($files);
// echo(PHP_EOL);
foreach($files as $filename){
if ($fs->checkFile($path.$filename)) {
$sg->create();
$sg->set('sg_rid',$folder);
$sg->set('sg_title',$filename);
$sg->set('sg_image',$path.$filename);
$name = MODX_BASE_PATH . "assets/galleries/".$folder."/".$filename;
$info = getimagesize($name);
$properties = array(
'width' => $info[0],
'height' => $info[1],
'size' => filesize($name)
);
$sg->set('sg_properties',$properties);
$sg->save();
echo("added ".$path.$filename.PHP_EOL);
}else{
echo("check failed ".$path.$filename.PHP_EOL);
}
}
}
?>
@sashabeep
Copy link
Author

  1. Upload pictures to folders corresponding to document ID. e.g. assets/galleries/100, assets/galleries/101, assets/galleries/N
  2. Run sctipt in terminal php sgimport-static.php. Don't forget to define site URL on the line 5.
  3. Warning: script is processing ALL the directories and if you have galleries created from manager - move them outside the /galleries/ folder and return back after import to prevent doubling pictures in existing galleries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment