65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Drupal\images_styles_gen;
|
||
|
|
||
|
|
||
|
// use Drupal\node\Entity\Node;
|
||
|
|
||
|
class CreateImagesStyles {
|
||
|
|
||
|
public static function createStyles($item, &$context){
|
||
|
$context['sandbox']['progress']++;
|
||
|
$context['sandbox']['current_file'] = $item->uri;
|
||
|
// $message = 'Creating Styles ...';
|
||
|
$results = array();
|
||
|
|
||
|
// foreach ($items as $item) {
|
||
|
$results[] = $item->style->createDerivative($item->uri, $item->destination);
|
||
|
// }
|
||
|
// $styles_storage = \Drupal::entityTypeManager()->getStorage('image_style');
|
||
|
// $styles = $styles_storage->loadMultiple();
|
||
|
//
|
||
|
// foreach ($fids as $fid) {
|
||
|
// /** @var \Drupal\file\FileInterface|null $file*/
|
||
|
// $file_entity = \Drupal::entityTypeManager()->getStorage('file')->load($fid);
|
||
|
// $file_uri = $file_entity->getFileUri();
|
||
|
// $image = \Drupal::service('image.factory')->get($file_uri);
|
||
|
// // \Drupal::logger('images_styles_gen')->notice("file_uri is $file_uri");
|
||
|
//
|
||
|
// if ($image->isValid()) {
|
||
|
// /** @var \Drupal\image\Entity\ImageStyle $style */
|
||
|
// foreach ($styles as $style) {
|
||
|
// $destination = $style->buildUri($file_uri);
|
||
|
// // if destination exists skip
|
||
|
// if (file_exists($destination)) continue; // skip existing files
|
||
|
//
|
||
|
// $results[] = $style->createDerivative($file_uri, $destination);
|
||
|
// // \Drupal::logger('images_styles_gen')->notice("Generated style $file_uri to $destination");
|
||
|
// }
|
||
|
//
|
||
|
// }
|
||
|
//
|
||
|
//
|
||
|
// }
|
||
|
// $context['message'] = $message;
|
||
|
// $context['results'] = $results;
|
||
|
$context['message'] = $item->uri . ' processed.';
|
||
|
$context['results'][] = $item->destination;
|
||
|
}
|
||
|
|
||
|
public static function createStylesFinishedCallback($success, $results, $operations) {
|
||
|
// The 'success' parameter means no fatal PHP errors were detected. All
|
||
|
// other error management should be handled using 'results'.
|
||
|
if ($success) {
|
||
|
$message = \Drupal::translation()->formatPlural(
|
||
|
count($results),
|
||
|
'One image processed.', '@count image processed.'
|
||
|
);
|
||
|
}
|
||
|
else {
|
||
|
$message = t('Finished with an error.');
|
||
|
}
|
||
|
drupal_set_message($message);
|
||
|
}
|
||
|
}
|