ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ManipulateImagesInChoiceQuestionsTrait.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23trait ManipulateImagesInChoiceQuestionsTrait
24{
30 protected function deleteImage($image_filename): void
31 {
32 $imagepath = $this->getImagePath();
33 @unlink($imagepath . $image_filename);
34 $thumbpath = $imagepath . $this->getThumbPrefix() . $image_filename;
35 @unlink($thumbpath);
36 }
37
38 private function cloneImages(
39 int $source_question_id,
40 int $source_parent_id,
41 int $target_question_id,
42 int $target_parent_id,
43 array $answers
44 ): void {
45 $image_source_path = $this->getImagePath($source_question_id, $source_parent_id);
46 $image_target_path = $this->getImagePath($target_question_id, $target_parent_id);
47
48 if (!file_exists($image_target_path)) {
49 \ilFileUtils::makeDirParents($image_target_path);
50 } else {
51 $this->removeAllImageFiles($image_target_path);
52 }
53
54 foreach ($answers as $answer) {
55 $filename = $answer->getImage();
56 if ($filename === null || $filename === '') {
57 continue;
58 }
59
60 if (!file_exists($image_source_path . $filename)
61 || !copy($image_source_path . $filename, $image_target_path . $filename)) {
62 $this->log->root()->warning('Image could not be cloned for object for question: ' . $target_question_id);
63 }
64 if (!file_exists($image_source_path . $this->getThumbPrefix() . $filename)
65 || !copy($image_source_path . $this->getThumbPrefix() . $filename, $image_target_path . $this->getThumbPrefix() . $filename)) {
66 $this->log->root()->warning('Image thumbnail could not be cloned for object for question: ' . $target_question_id);
67 }
68 }
69 }
70
71 public function rebuildThumbnails(
72 bool $is_single_line,
73 int $thumbnail_size,
74 string $image_path,
75 array $answers
76 ): array {
77 if (!$is_single_line || $thumbnail_size === 0) {
78 return $answers;
79 }
80
81 foreach ($answers as $answer) {
82 if ($answer->getImage() === null || $answer->getImage() === '') {
83 continue;
84 }
85
86 $thumb_path = $image_path . $this->getThumbPrefix() . $answer->getImage();
87 if (file_exists($thumb_path)) {
88 unlink($thumb_path);
89 }
90
91 $current_file_path = $image_path . $answer->getImage();
92 if (!file_exists($current_file_path)) {
93 continue;
94 }
95 $new_file_name = $this->buildHashedImageFilename($answer->getImage(), true);
96 $new_file_path = $image_path . $new_file_name;
97 rename($current_file_path, $new_file_path);
98 $answer->setImage($new_file_name);
99
100 $this->generateThumbForFile(
101 $new_file_name,
102 $image_path,
103 $thumbnail_size
104 );
105 }
106
107 return $answers;
108 }
109
110 public function getThumbPrefix(): string
111 {
112 return "thumb.";
113 }
114
115 public function generateThumbForFile(
116 string $file_name,
117 string $image_path,
118 int $thumbnail_size,
119 ): void {
120 $file_path = $image_path . $file_name;
121 if (!file_exists($file_path)) {
122 return;
123 }
124
125 $thumb_path = $image_path . $this->getThumbPrefix() . $file_name;
126 $path_info = pathinfo($file_path);
127 $ext = "";
128 switch (strtoupper($path_info['extension'])) {
129 case 'PNG':
130 $ext = 'PNG';
131 break;
132 case 'GIF':
133 $ext = 'GIF';
134 break;
135 default:
136 $ext = 'JPEG';
137 break;
138 }
139 \ilShellUtil::convertImage($file_path, $thumb_path, $ext, (string) $thumbnail_size, );
140 }
141}
$filename
Definition: buildRTE.php:78
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
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...
if(!file_exists('../ilias.ini.php'))