You are here

public static function ExternalPackageController::convertPptSlidesToImages in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Controller/ExternalPackageController.php \Drupal\opigno_module\Controller\ExternalPackageController::convertPptSlidesToImages()

Converts PowerPoint files to images per slide.

Parameters

\Drupal\File\entity\File $file: PowerPoint file (ppt/pptx).

string $ppt_dir_real_path: Ppt conversion directory real path.

Return value

array Array of image files links.

1 call to ExternalPackageController::convertPptSlidesToImages()
AddExternalPackageForm::validateForm in src/Form/AddExternalPackageForm.php
Form validation handler.

File

src/Controller/ExternalPackageController.php, line 376

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

public static function convertPptSlidesToImages(File $file, $ppt_dir_real_path) {
  $current_dir = getcwd();
  \Drupal::logger('ppt_converter')
    ->notice('Current dir: ' . $current_dir);
  if (chdir($ppt_dir_real_path)) {
    $path_info = pathinfo($file
      ->getFilename());
    \Drupal::logger('ppt_converter')
      ->notice('Changed dir: ' . getcwd());
    \Drupal::logger('ppt_converter')
      ->notice('File $path_info: <pre><code>' . print_r($path_info, TRUE) . '</code></pre>');
    \Drupal::logger('ppt_converter')
      ->notice('Starting convert to PDF.');

    // Convert to pdf.
    $libreoffice_configs_path = self::getLibreofficeConfigsDir();
    $libreoffice_bin_path = self::getLibreOfficeBinPath();
    $a1 = microtime(TRUE);
    $result = exec($libreoffice_bin_path . ' -env:UserInstallation=file://' . $libreoffice_configs_path . ' --headless --invisible --convert-to pdf ' . $path_info['basename']);
    $a2 = microtime(TRUE);
    $converting_time = $a2 - $a1;
    \Drupal::logger('ppt_converter')
      ->notice('Convert to pdf finished. Time of converting: ' . $converting_time);
    if ($result) {
      \Drupal::logger('ppt_converter')
        ->notice('Starting convert to jpg.');

      // Convert to images.
      $imagemagick_bin_path = self::getImagemagickBinPath();
      exec($imagemagick_bin_path . " " . $path_info['filename'] . ".pdf -geometry x720 -gravity Center " . $path_info['filename'] . ".jpg");
      \Drupal::logger('ppt_converter')
        ->notice('Convert to jpg finished.');

      /** @var \Drupal\Core\File\FileSystemInterface $file_system */
      $file_system = \Drupal::service('file_system');
      $files = $file_system
        ->scanDirectory($ppt_dir_real_path, '/.*\\.(jpg)$/');

      // Sort images by slides order.
      foreach ($files as &$f) {
        $filename_exploded = explode('-', $f->name);
        $f->weight = end($filename_exploded);
      }
      usort($files, 'self::opignoH5pSlidesSortByWeight');
      chdir($current_dir);
      \Drupal::logger('ppt_converter')
        ->notice('Return to dir: ' . getcwd());
      return $files;
    }
  }
  chdir($current_dir);
  \Drupal::logger('ppt_converter')
    ->notice('Return to dir: ' . getcwd());
  return [];
}