ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ManipulateThumbnailsInChoiceQuestionsTrait.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  public function rebuildThumbnails(
26  bool $is_single_line,
27  int $thumbnail_size,
28  string $image_path,
29  array $answers
30  ): array {
31  if (!$is_single_line || $thumbnail_size === 0) {
32  return $answers;
33  }
34 
35  foreach ($answers as $answer) {
36  if ($answer->getImage() === '') {
37  continue;
38  }
39 
40  $thumb_path = $image_path . $this->getThumbPrefix() . $answer->getImage();
41  if (file_exists($thumb_path)) {
42  unlink($thumb_path);
43  }
44 
45  $current_file_path = $image_path . $answer->getImage();
46  if (!file_exists($current_file_path)) {
47  continue;
48  }
49  $new_file_name = $this->buildHashedImageFilename($answer->getImage(), true);
50  $new_file_path = $image_path . $new_file_name;
51  rename($current_file_path, $new_file_path);
52  $answer->setImage($new_file_name);
53 
54  $this->generateThumbForFile(
55  $new_file_name,
56  $image_path,
57  $thumbnail_size
58  );
59  }
60 
61  return $answers;
62  }
63 
64  public function getThumbPrefix(): string
65  {
66  return "thumb.";
67  }
68 
69  public function generateThumbForFile(
70  string $file_name,
71  string $image_path,
72  int $thumbnail_size,
73  ): void {
74  $file_path = $image_path . $file_name;
75  if (!file_exists($file_path)) {
76  return;
77  }
78 
79  $thumb_path = $image_path . $this->getThumbPrefix() . $file_name;
80  $path_info = pathinfo($file_path);
81  $ext = "";
82  switch (strtoupper($path_info['extension'])) {
83  case 'PNG':
84  $ext = 'PNG';
85  break;
86  case 'GIF':
87  $ext = 'GIF';
88  break;
89  default:
90  $ext = 'JPEG';
91  break;
92  }
93  \ilShellUtil::convertImage($file_path, $thumb_path, $ext, (string) $thumbnail_size, );
94  }
95 }
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...