Added text to transcode message to indicate file size change
This commit is contained in:
parent
126412fb4b
commit
546f0701a9
2 changed files with 22 additions and 3 deletions
|
@ -733,3 +733,10 @@ function SHM_USER_FORM(User $duser, string $target, string $title, $body, $foot)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BYTE_DENOMINATIONS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
function human_filesize(int $bytes, $decimals = 2)
|
||||||
|
{
|
||||||
|
$factor = floor((strlen(strval($bytes)) - 1) / 3);
|
||||||
|
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @BYTE_DENOMINATIONS[$factor];
|
||||||
|
}
|
||||||
|
|
|
@ -179,16 +179,20 @@ class TranscodeImage extends Extension
|
||||||
if ($user->can(Permissions::EDIT_FILES)) {
|
if ($user->can(Permissions::EDIT_FILES)) {
|
||||||
$format = $_POST['transcode_format'];
|
$format = $_POST['transcode_format'];
|
||||||
$total = 0;
|
$total = 0;
|
||||||
|
$size_difference = 0;
|
||||||
foreach ($event->items as $image) {
|
foreach ($event->items as $image) {
|
||||||
try {
|
try {
|
||||||
$database->beginTransaction();
|
$database->beginTransaction();
|
||||||
|
|
||||||
$this->transcode_and_replace_image($image, $format);
|
$before_size = $image->filesize;
|
||||||
|
|
||||||
|
$new_image = $this->transcode_and_replace_image($image, $format);
|
||||||
// If a subsequent transcode fails, the database needs to have everything about the previous
|
// If a subsequent transcode fails, the database needs to have everything about the previous
|
||||||
// transcodes recorded already, otherwise the image entries will be stuck pointing to
|
// transcodes recorded already, otherwise the image entries will be stuck pointing to
|
||||||
// missing image files
|
// missing image files
|
||||||
$database->commit();
|
$database->commit();
|
||||||
$total++;
|
$total++;
|
||||||
|
$size_difference += ($before_size - $new_image->filesize);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
log_error("transcode", "Error while bulk transcode on item {$image->id} to $format: ".$e->getMessage());
|
log_error("transcode", "Error while bulk transcode on item {$image->id} to $format: ".$e->getMessage());
|
||||||
try {
|
try {
|
||||||
|
@ -198,7 +202,13 @@ class TranscodeImage extends Extension
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$page->flash("Transcoded $total items");
|
if ($size_difference>0) {
|
||||||
|
$page->flash("Transcoded $total items, reduced size by ".human_filesize($size_difference)." bytes");
|
||||||
|
} elseif ($size_difference<0) {
|
||||||
|
$page->flash("Transcoded $total items, increased size by ".human_filesize(-1*$size_difference)." bytes");
|
||||||
|
} else {
|
||||||
|
$page->flash("Transcoded $total items, no size difference");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -234,7 +244,7 @@ class TranscodeImage extends Extension
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function transcode_and_replace_image(Image $image_obj, String $target_format)
|
private function transcode_and_replace_image(Image $image_obj, String $target_format): Image
|
||||||
{
|
{
|
||||||
$original_file = warehouse_path(Image::IMAGE_DIR, $image_obj->hash);
|
$original_file = warehouse_path(Image::IMAGE_DIR, $image_obj->hash);
|
||||||
|
|
||||||
|
@ -258,6 +268,8 @@ class TranscodeImage extends Extension
|
||||||
@unlink($tmp_filename);
|
@unlink($tmp_filename);
|
||||||
|
|
||||||
send_event(new ImageReplaceEvent($image_obj->id, $new_image));
|
send_event(new ImageReplaceEvent($image_obj->id, $new_image));
|
||||||
|
|
||||||
|
return $new_image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue