Skip to content

Instantly share code, notes, and snippets.

@Electrica
Created November 23, 2019 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Electrica/10cda60d8d53fc320b34868727464d9e to your computer and use it in GitHub Desktop.
Save Electrica/10cda60d8d53fc320b34868727464d9e to your computer and use it in GitHub Desktop.
image thumbnail generation
<?php
define('MODX_API_MODE', true);
require 'index.php'; // Этот файл лежит в корне сайта
$modx->getService('error','error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
// Проходимся по всем товарам
$products = $modx->getIterator('msProduct', array('class_key' => 'msProduct'));
foreach ($products as $product) {
// Получаем оригиналы их картинок
$files = $product->getMany('Files', array('parent' => 0));
foreach ($files as $file) {
// Затем получаем их преью
$children = $file->getMany('Children');
foreach ($children as $child) {
// Удаляем эти превью, вместе с файлами
$child->remove();
}
// И генерируем новые
$file->generateThumbnails();
// Если это первый файл в галерее - обновляем ссылку на превью товара
/** @var msProductData $data */
if ($file->get('rank') == 0 && $data = $product->getOne('Data')) {
$thumb = $file->getFirstThumbnail();
$data->set('thumb', $thumb['url']);
$data->save();
}
}
}
echo microtime(true) - $modx->startTime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment