22/**
33 * @author darkfriend <hi@darkfriend.ru>
44 * @copyright dev2fun
5- * @version 0.11.8
5+ * @version 0.11.9
66 */
77
88namespace Dev2fun \ImageCompress ;
99
10+ use Bitrix \Main \Application ;
1011use Bitrix \Main \Localization \Loc ;
1112use Bitrix \Main \Config \Option ;
13+ use Throwable ;
1214
1315IncludeModuleLangFile (__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}
0 commit comments