Skip to content

Commit

Permalink
copage/mob: remove unused directory code; fixed area editing
Browse files Browse the repository at this point in the history
  • Loading branch information
alex40724 committed Jan 2, 2025
1 parent 4180ce8 commit e246bc4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function makeMapWorkCopy(
bool $a_output_new_area = false,
string $a_area_type = "",
string $a_coords = ""
): void {
): ilMediaItem {
// old for pc media object
// $media_object = $this->media_object->getMediaItem("Standard");
$media_object = $this->content_obj->getMediaObject();
Expand Down Expand Up @@ -325,6 +325,8 @@ public function makeMapWorkCopy(
$a_coords
);
}

return $st_item;
}

public function getAliasXML(): string
Expand Down
4 changes: 1 addition & 3 deletions components/ILIAS/COPage/classes/class.ilMediaAliasItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ public function makeMapWorkCopy(
string $a_area_type,
string $a_coords
): bool {
$a_st_item->copyOriginal();
$a_st_item->buildMapWorkImage();

// determine ratios (first see whether the instance has w/h defined)
Expand All @@ -886,7 +885,7 @@ public function makeMapWorkCopy(
$width = $a_st_item->getWidth();
$height = $a_st_item->getHeight();
}
$size = getimagesize($a_st_item->getMapWorkCopyName());
$size = getimagesize($a_st_item->getOriginalSource());
$x_ratio = 1;
if ($size[0] > 0 && $width > 0) {
$x_ratio = $width / $size[0];
Expand Down Expand Up @@ -931,7 +930,6 @@ public function makeMapWorkCopy(
);
}

$a_st_item->saveMapWorkImage();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,29 @@ public function getArea(): array
{
return $this->strArray("area");
}

public function getOutEditProperty(): string
{
return $this->str("out_edit_property");
}

public function getOutAreaNr(): int
{
return $this->int("out_area_nr");
}

public function getOutOutputNewArea(): bool
{
return (bool) $this->int("out_output_new_area");
}

public function getOutAreaType(): string
{
return $this->str("out_area_type");
}

public function getOutCoords(): string
{
return $this->str("out_coords");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ public function handleMapParameters(): void

public function showImageMap(): void
{
$item = new ilMediaItem($this->request->getItemId());
$item = $this->makeMapWorkCopy(
$this->request->getOutEditProperty(),
$this->request->getOutAreaNr(),
$this->request->getOutOutputNewArea(),
$this->request->getOutAreaType(),
$this->request->getOutCoords()
);

//$item = new ilMediaItem($this->request->getItemId());
$item->outputMapWorkCopy();
}

Expand Down Expand Up @@ -399,20 +407,19 @@ public function editMapArea(
$form = $this->initAreaEditingForm($a_edit_property);
$this->tpl->setVariable("FORM", $form->getHTML());
}

$this->makeMapWorkCopy(
$a_edit_property,
$a_area_nr,
$a_output_new_area,
$area_type,
$coords
);

$ilCtrl->setParameter($this, "out_edit_property", $a_edit_property);
$ilCtrl->setParameter($this, "out_area_nr", $a_area_nr);
$ilCtrl->setParameter($this, "out_output_new_area", (int) $a_output_new_area);
$ilCtrl->setParameter($this, "out_area_type", $area_type);
$ilCtrl->setParameter($this, "out_coords", $coords);

$edit_mode = ($a_get_next_coordinate)
? "get_coords"
: (($a_output_new_area)
? "new_area"
: "");

$output = $this->getImageMapOutput($edit_mode);
$this->tpl->setVariable("IMAGE_MAP", $output);

Expand Down Expand Up @@ -512,7 +519,7 @@ public function makeMapWorkCopy(
bool $a_output_new_area = false,
string $a_area_type = "",
string $a_coords = ""
): void {
): ilMediaItem {
// create/update imagemap work copy
$st_item = $this->media_object->getMediaItem("Standard");

Expand All @@ -525,6 +532,8 @@ public function makeMapWorkCopy(
if ($a_output_new_area) {
$st_item->addAreaToMapWorkCopy($a_area_type, $a_coords);
}

return $st_item;
}

/**
Expand Down
132 changes: 19 additions & 113 deletions components/ILIAS/MediaObjects/classes/class.ilMediaItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
class ilMediaItem
{
protected \ILIAS\MediaObjects\MediaObjectManager $mob_manager;
protected string $tried_thumb = "";
protected string $text_representation = "";
protected ilDBInterface $db;
Expand Down Expand Up @@ -68,6 +69,7 @@ public function __construct(
$this->setId($a_id);
$this->read();
}
$this->mob_manager = $DIC->mediaObjects()->internal()->domain()->mediaObject();
}

/**
Expand Down Expand Up @@ -610,14 +612,8 @@ public function setHeight(string $a_height): void

public function getOriginalSize(): ?array
{
$mob_dir = ilObjMediaObject::_getDirectory($this->getMobId());

if (ilUtil::deducibleSize($this->getFormat())) {
if ($this->getLocationType() == "LocalFile") {
$loc = $mob_dir . "/" . $this->getLocation();
} else {
$loc = $this->getLocation();
}
$loc = $this->getOriginalSize();

$size = ilMediaImageUtil::getImageSize($loc);
if ($size[0] > 0 && $size[1] > 0) {
Expand Down Expand Up @@ -724,28 +720,6 @@ public function getParameter(string $a_name): string
return (string) ($this->parameters[$a_name] ?? "");
}

/**
* get work directory for image map editing
*/
public function getWorkDirectory(): string
{
return ilFileUtils::getDataDir() . "/map_workfiles/item_" . $this->getId();
}

/**
* create work directory for image map editing
*/
public function createWorkDirectory(): void
{
if (!is_dir(ilFileUtils::getDataDir() . "/map_workfiles")) {
ilFileUtils::createDirectory(ilFileUtils::getDataDir() . "/map_workfiles");
}
$work_dir = $this->getWorkDirectory();
if (!is_dir($work_dir)) {
ilFileUtils::createDirectory($work_dir);
}
}

/**
* get location suffix
*/
Expand All @@ -764,26 +738,6 @@ public function getMapWorkCopyType(): string
return self::getGDSupportedImageType($this->getSuffix());
}

/**
* Get name of image map work copy file
* @param bool $a_reference_copy get name for copy of external referenced image
*/
public function getMapWorkCopyName(
bool $a_reference_copy = false
): string {
$file_arr = explode("/", $this->getLocation());
$o_file = $file_arr[count($file_arr) - 1];
$file_arr = explode(".", $o_file);
unset($file_arr[count($file_arr) - 1]);
$file = implode(".", $file_arr);

if (!$a_reference_copy) {
return $this->getWorkDirectory() . "/" . $file . "." . $this->getMapWorkCopyType();
} else {
return $this->getWorkDirectory() . "/l_copy_" . $o_file;
}
}

/**
* get media file directory
*/
Expand Down Expand Up @@ -857,51 +811,15 @@ public function getThumbnailTarget(
return "";
}

/**
* Copy the original file for map editing
* to the working directory
* @throws ilMapEditingException
*/
public function copyOriginal(): void
public function getOriginalSource() : string
{
$lng = $this->lng;
$this->createWorkDirectory();

if ($this->getLocationType() !== "Reference") {
$this->image_converter->convertToFormat(
$this->getDirectory() . "/" . $this->getLocation(),
$this->getMapWorkCopyName(),
$this->getMapWorkCopyType(),
$this->getWidth() === '' ? null : $this->getWidth(),
$this->getHeight() === '' ? null : $this->getHeight()
return $this->mob_manager->getLocalSrc(
$this->getMobId(),
$this->getLocation()
);
} else {
// first copy the external file, if necessary
if (!is_file($this->getMapWorkCopyName(true)) || (filesize($this->getMapWorkCopyName(true)) == 0)) {
$handle = fopen($this->getLocation(), "r");
$lcopy = fopen($this->getMapWorkCopyName(true), "w");
if ($handle && $lcopy) {
while (!feof($handle)) {
$content = fread($handle, 4096);
fwrite($lcopy, $content);
}
}
fclose($lcopy);
fclose($handle);
}

// now, create working copy
$this->image_converter->convertToFormat(
$this->getMapWorkCopyName(true),
$this->getMapWorkCopyName(),
$this->getMapWorkCopyType(),
$this->getWidth() === '' ? null : $this->getWidth(),
$this->getHeight() === '' ? null : $this->getHeight()
);
}
if (!is_file($this->getMapWorkCopyName())) {
throw new ilMapEditingException($lng->txt("cont_map_file_not_generated"));
}
return $this->getLocation();
}

/**
Expand All @@ -913,13 +831,10 @@ public function makeMapWorkCopy(
int $a_area_nr = 0,
bool $a_exclude = false
): void {
$lng = $this->lng;

$this->copyOriginal();
$this->buildMapWorkImage();

// determine ratios
$size = getimagesize($this->getMapWorkCopyName());
$size = getimagesize($this->getOriginalSource());
$x_ratio = 1;
if ($size[0] > 0 && $this->getWidth() > 0) {
$x_ratio = $this->getWidth() / $size[0];
Expand All @@ -946,8 +861,6 @@ public function makeMapWorkCopy(
);
}
}

$this->saveMapWorkImage();
}

/**
Expand All @@ -959,10 +872,10 @@ public function addAreaToMapWorkCopy(
string $a_shape,
string $a_coords
): void {
$this->buildMapWorkImage();
// $this->buildMapWorkImage();

// determine ratios
$size = getimagesize($this->getMapWorkCopyName());
$size = getimagesize($this->getOriginalSource());
$x_ratio = 1;
if ($size[0] > 0 && $this->getWidth() > 0) {
$x_ratio = $this->getWidth() / $size[0];
Expand All @@ -984,8 +897,6 @@ public function addAreaToMapWorkCopy(
$x_ratio,
$y_ratio
);

$this->saveMapWorkImage();
}

/**
Expand All @@ -997,7 +908,7 @@ public function outputMapWorkCopy(): void
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: image/" . strtolower($this->getMapWorkCopyType()));
readfile($this->getMapWorkCopyName());
$this->outputWorkImage();
}
exit;
}
Expand All @@ -1011,16 +922,16 @@ public function buildMapWorkImage(): void

switch ($im_type) {
case "gif":
$this->map_image = imagecreatefromgif($this->getMapWorkCopyName());
$this->map_image = imagecreatefromgif($this->getOriginalSource());
break;

case "jpg":
case "jpeg":
$this->map_image = imagecreatefromjpeg($this->getMapWorkCopyName());
$this->map_image = imagecreatefromjpeg($this->getOriginalSource());
break;

case "png":
$this->map_image = imagecreatefrompng($this->getMapWorkCopyName());
$this->map_image = imagecreatefrompng($this->getOriginalSource());
break;
}

Expand All @@ -1034,30 +945,25 @@ public function buildMapWorkImage(): void
}
}

/**
* save image map work image as file
*/
public function saveMapWorkImage(): void
public function outputWorkImage(): void
{
$im_type = strtolower($this->getMapWorkCopyType());

// save image work-copy and free memory
switch ($im_type) {
case "gif":
imagegif($this->map_image, $this->getMapWorkCopyName());
imagegif($this->map_image);
break;

case "jpg":
case "jpeg":
imagejpeg($this->map_image, $this->getMapWorkCopyName());
imagejpeg($this->map_image);
break;

case "png":
imagepng($this->map_image, $this->getMapWorkCopyName());
imagepng($this->map_image);
break;
}

imagedestroy($this->map_image);
}

/**
Expand Down

0 comments on commit e246bc4

Please sign in to comment.