Skip to content

Commit 0ab5a1f

Browse files
committed
upd to 0.11.9
1 parent 8b4bd4a commit 0ab5a1f

File tree

8 files changed

+292
-162
lines changed

8 files changed

+292
-162
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22
$arModuleVersion = [
3-
'VERSION' => '0.11.8',
4-
'VERSION_DATE' => '2025-07-23 19:30:00',
3+
'VERSION' => '0.11.9',
4+
'VERSION_DATE' => '2025-07-27 20:55:00',
55
];

utf8/dev2fun.imagecompress/lib/Compress.php

Lines changed: 112 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
/**
33
* @author darkfriend <hi@darkfriend.ru>
44
* @copyright dev2fun
5-
* @version 0.11.8
5+
* @version 0.11.9
66
*/
77

88
namespace Dev2fun\ImageCompress;
99

10+
use Bitrix\Main\Application;
1011
use Bitrix\Main\Localization\Loc;
1112
use Bitrix\Main\Config\Option;
13+
use Throwable;
1214

1315
IncludeModuleLangFile(__FILE__);
1416

@@ -349,68 +351,85 @@ public function compressImageByID(int $intFileID)
349351

350352
$strFilePath = $_SERVER["DOCUMENT_ROOT"] . \CFile::GetPath($intFileID);
351353

352-
if (\file_exists($strFilePath)) {
353-
$oldSize = $arFile["FILE_SIZE"]; // filesize($strFilePath);
354-
if ($this->enableImageResize) {
355-
$this->resize($intFileID, $strFilePath);
356-
}
357-
switch ($arFile["CONTENT_TYPE"]) {
358-
case 'image/jpeg':
359-
$isCompress = $this->compressJPG($strFilePath);
360-
break;
361-
case 'image/png':
362-
$isCompress = $this->compressPNG($strFilePath);
363-
break;
364-
case 'application/pdf':
365-
$isCompress = $this->compressPdf($strFilePath);
366-
break;
367-
case 'image/svg':
368-
$isCompress = $this->process(
369-
$strFilePath,
370-
Option::get($this->MODULE_ID, 'opti_algorithm_svg', '')
371-
);
372-
break;
373-
case 'image/gif':
374-
$isCompress = $this->process(
375-
$strFilePath,
376-
Option::get($this->MODULE_ID, 'opti_algorithm_gif', '')
377-
);
378-
break;
379-
default:
380-
$this->LAST_ERROR = Loc::getMessage('DEV2FUN_IMAGECOMPRESS_CONTENT_TYPE', [
381-
'#TYPE#' => $arFile["CONTENT_TYPE"],
382-
]);
383-
return null;
384-
}
385-
386-
if ($isCompress) {
387-
\clearstatcache(true, $strFilePath);
388-
$newSize = filesize($strFilePath);
389-
if ($newSize != $oldSize) {
390-
// $DB->Query("UPDATE b_file SET FILE_SIZE='" . $DB->ForSql($newSize, 255) . "' WHERE ID=" . intval($intFileID));
391-
$this->saveSizeBitrix($intFileID, $newSize);
354+
try
355+
{
356+
if (\file_exists($strFilePath)) {
357+
$oldSize = $arFile["FILE_SIZE"]; // filesize($strFilePath);
358+
if ($this->enableImageResize) {
359+
$this->resize($intFileID, $strFilePath);
392360
}
393-
$arFields = [
394-
'FILE_ID' => $intFileID,
395-
'SIZE_BEFORE' => $oldSize,
396-
'SIZE_AFTER' => $newSize,
397-
];
398-
$rs = ImageCompressTable::getById($intFileID);
399-
if ($rs->getSelectedRowsCount() <= 0) {
400-
$res = ImageCompressTable::add($arFields);
361+
switch ($arFile["CONTENT_TYPE"]) {
362+
case 'image/jpeg':
363+
$isCompress = $this->compressJPG($strFilePath);
364+
break;
365+
case 'image/png':
366+
$isCompress = $this->compressPNG($strFilePath);
367+
break;
368+
case 'application/pdf':
369+
$isCompress = $this->compressPdf($strFilePath);
370+
break;
371+
case 'image/svg':
372+
$isCompress = $this->process(
373+
$strFilePath,
374+
Option::get($this->MODULE_ID, 'opti_algorithm_svg', '')
375+
);
376+
break;
377+
case 'image/gif':
378+
$isCompress = $this->process(
379+
$strFilePath,
380+
Option::get($this->MODULE_ID, 'opti_algorithm_gif', '')
381+
);
382+
break;
383+
default:
384+
$this->LAST_ERROR = Loc::getMessage('DEV2FUN_IMAGECOMPRESS_CONTENT_TYPE', [
385+
'#TYPE#' => $arFile["CONTENT_TYPE"],
386+
]);
387+
return null;
388+
}
389+
390+
Application::getInstance()->getConnection()->connect();
391+
392+
if ($isCompress) {
393+
\clearstatcache(true, $strFilePath);
394+
$newSize = filesize($strFilePath);
395+
396+
if ($newSize != $oldSize) {
397+
// $DB->Query("UPDATE b_file SET FILE_SIZE='" . $DB->ForSql($newSize, 255) . "' WHERE ID=" . intval($intFileID));
398+
$this->saveSizeBitrix($intFileID, $newSize);
399+
}
400+
$arFields = [
401+
'FILE_ID' => $intFileID,
402+
'SIZE_BEFORE' => $oldSize,
403+
'SIZE_AFTER' => $newSize,
404+
];
405+
406+
$rs = ImageCompressTable::getById($intFileID);
407+
if ($rs->getSelectedRowsCount() <= 0) {
408+
$res = ImageCompressTable::add($arFields);
409+
} else {
410+
$res = ImageCompressTable::update($intFileID, $arFields);
411+
}
412+
401413
} else {
402-
$res = ImageCompressTable::update($intFileID, $arFields);
414+
$this->LAST_ERROR = '';
403415
}
404416
} else {
405-
$this->LAST_ERROR = '';
417+
$res = $this->addCompressTable($intFileID, [
418+
'FILE_ID' => $intFileID,
419+
'SIZE_BEFORE' => 0,
420+
'SIZE_AFTER' => 0,
421+
]);
422+
}
423+
424+
} catch (Throwable $e) {
425+
$this->LAST_ERROR = $e->getMessage();
426+
$this->log($e->getMessage());
427+
$connection = Application::getInstance()->getConnection();
428+
if (!$connection->isConnected()) {
429+
$connection->connect();
406430
}
407-
} else {
408-
$res = $this->addCompressTable($intFileID, [
409-
'FILE_ID' => $intFileID,
410-
'SIZE_BEFORE' => 0,
411-
'SIZE_AFTER' => 0,
412-
]);
413431
}
432+
414433
return $res;
415434
}
416435

@@ -429,7 +448,9 @@ public function resize(int $fileId, string $strFilePath)
429448
$width = Option::get($this->MODULE_ID, 'resize_image_width', '');
430449
$height = Option::get($this->MODULE_ID, 'resize_image_height', '');
431450
$algorithm = Option::get($this->MODULE_ID, 'resize_image_algorithm', '');
432-
if (!$algorithm) $algorithm = BX_RESIZE_IMAGE_PROPORTIONAL;
451+
if (!$algorithm) {
452+
$algorithm = BX_RESIZE_IMAGE_PROPORTIONAL;
453+
}
433454

434455
$destinationFile = $_SERVER['DOCUMENT_ROOT'] . "/upload/{$this->MODULE_ID}/" . basename($strFilePath);
435456
$res = \CFile::ResizeImageFile(
@@ -504,7 +525,7 @@ public function addCompressTable(int $intFileID, array $arFields)
504525
*/
505526
public static function CompressImageOnSectionEvent(&$arFields)
506527
{
507-
if(!static::$enable) {
528+
if (!static::$enable) {
508529
return;
509530
}
510531
$instance = self::getInstance();
@@ -528,7 +549,9 @@ public static function CompressImageOnSectionEvent(&$arFields)
528549
*/
529550
public static function CompressImageOnElementEvent(&$arFields)
530551
{
531-
if(!static::$enable) return;
552+
if (!static::$enable) {
553+
return;
554+
}
532555
$instance = self::getInstance();
533556
if (!$instance->enableElement) {
534557
return;
@@ -867,13 +890,13 @@ public function getFileList(array $arOrder = [], array $arFilter = [], int $limi
867890
{
868891
global $DB;
869892
$strSql = $this->queryBuilder($arOrder, $arFilter);
870-
// if($limit) {
871-
// $strSql .= ' LIMIT '.$limit;
872-
// }
873-
//
874-
// if($offset) {
875-
// $strSql .= ' OFFSET '.$offset;
876-
// }
893+
// if($limit) {
894+
// $strSql .= ' LIMIT '.$limit;
895+
// }
896+
//
897+
// if($offset) {
898+
// $strSql .= ' OFFSET '.$offset;
899+
// }
877900
return $DB->Query($strSql, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
878901
}
879902

@@ -1019,5 +1042,27 @@ public function getError()
10191042
return $this->LAST_ERROR;
10201043
}
10211044

1045+
/**
1046+
* @param string $msg
1047+
* @param string $type SECURITY, ERROR, INFO, DEBUG или WARNING, для иного система установит UNKNOWN
1048+
* @return int
1049+
*/
1050+
public function log(string $msg, string $type = 'ERROR'): int
1051+
{
1052+
return \CEventLog::Add([
1053+
"SEVERITY" => $type,
1054+
"AUDIT_TYPE_ID" => $type,
1055+
"MODULE_ID" => \Dev2funImageCompress::MODULE_ID,
1056+
"ITEM_ID" => '',
1057+
"DESCRIPTION" => $msg,
1058+
]);
1059+
}
1060+
1061+
// public function saveNewSize(int $fileId, int $newSize, int $oldSize)
1062+
// {
1063+
// if ($newSize != $oldSize) {
1064+
// $this->saveSizeBitrix($intFileID, $newSize);
1065+
// }
1066+
// }
10221067

10231068
}

utf8/dev2fun.imagecompress/lib/Jpegoptim.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @author darkfriend <hi@darkfriend.ru>
44
* @copyright dev2fun
5-
* @version 0.11.8
5+
* @version 0.11.9
66
*/
77

88
namespace Dev2fun\ImageCompress;
@@ -138,16 +138,31 @@ public function compress($strFilePath, $quality = 80, $params = [])
138138
if (!empty($params['progressiveJpeg'])) {
139139
$strCommand .= '--all-progressive';
140140
}
141-
$strCommand .= ' --strip-all -t';
141+
// $strCommand .= ' --strip-all -t';
142+
$strCommand .= ' --strip-com --strip-iptc --strip-xmp --strip-jfif --strip-jfxx --strip-Adobe --totals --preserve --preserve-perms';
142143
if ($quality) {
143144
$strCommand .= " -m{$quality}";
144145
}
145-
exec($this->jpegOptimPath . "/jpegoptim $strCommand '$strFilePath' 2>&1", $res);
146146

147-
if (!empty($params['changeChmod'])) {
148-
chmod($strFilePath, $params['changeChmod']);
147+
$exif = exif_read_data($strFilePath);
148+
if (!empty($exif['Orientation'])) {
149+
switch ($exif['Orientation']) {
150+
case 3: // Rotate 180 degrees
151+
case 6: // Rotate 90 degrees CW
152+
case 8: // Rotate 90 degrees CCW
153+
$strCommand .= ' --keep-exif';
154+
break;
155+
default:
156+
$strCommand .= ' --strip-exif';
157+
}
149158
}
150159

160+
exec($this->jpegOptimPath . "/jpegoptim $strCommand '$strFilePath' 2>&1", $res);
161+
162+
// if (!empty($params['changeChmod'])) {
163+
// chmod($strFilePath, $params['changeChmod']);
164+
// }
165+
151166
$event = new \Bitrix\Main\Event(
152167
$this->MODULE_ID,
153168
"OnAfterResize",

utf8/dev2fun.imagecompress/lib/Optipng.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @author darkfriend <hi@darkfriend.ru>
44
* @copyright dev2fun
5-
* @version 0.11.8
5+
* @version 0.11.9
66
*/
77

88
namespace Dev2fun\ImageCompress;
@@ -136,20 +136,25 @@ public function compress($strFilePath, $quality = 3, $params = [])
136136
$event->send();
137137

138138
exec($this->pngOptimPath . "/optipng -v", $out);
139-
$execString = "-strip all -o{$quality} '$strFilePath' 2>&1";
139+
$execString = "-preserve -strip all -o{$quality} '$strFilePath' 2>&1";
140+
140141
if (!empty($out[0])) {
141142
if (preg_match('#optipng.(.*?)\:#i', $out[0], $vMatch)) {
142143
$vMatch = preg_replace('#(\.)#', '', $vMatch[1]);
143-
if ($vMatch && $vMatch < 70) {
144-
$execString = "-o{$quality} '$strFilePath' 2>&1";
144+
if (preg_match('#(\d+)#', $vMatch, $vMatchResult)) {
145+
$vMatchResult = (int)($vMatchResult[1] ?? 0);
146+
if ($vMatchResult && $vMatchResult < 70) {
147+
$execString = "-preserve -o{$quality} '$strFilePath' 2>&1";
148+
}
145149
}
146150
}
147151
}
152+
148153
exec($this->pngOptimPath . "/optipng {$execString}", $res);
149154

150-
if (!empty($params['changeChmod'])) {
151-
chmod($strFilePath, $params['changeChmod']);
152-
}
155+
// if (!empty($params['changeChmod'])) {
156+
// chmod($strFilePath, $params['changeChmod']);
157+
// }
153158

154159
$event = new \Bitrix\Main\Event(
155160
$this->MODULE_ID,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22
$arModuleVersion = [
3-
'VERSION' => '0.11.8',
4-
'VERSION_DATE' => '2025-07-23 19:30:00',
3+
'VERSION' => '0.11.9',
4+
'VERSION_DATE' => '2025-07-27 20:55:00',
55
];

0 commit comments

Comments
 (0)