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