До сих пор актуально. Нашел несколько решений в этой статье и соединил в одно(для jpg и png)
Записать нужно в init.php
СпасибоИван Турченко , и Андрей Воробьев .
Записать нужно в init.php
Код |
---|
\Bitrix\Main\EventManager::getInstance()->addEventHandler( "main", "OnAfterResizeImage", array( "Handlers", "checkBadPictures" ) ); class Handlers{ public static function checkBadPictures( $file, $options, &$callbackData, &$cacheImageFile, &$cacheImageFileTmp, &$arImageSize ) { if (file_exists($cacheImageFileTmp)) { if (stripos($cacheImageFileTmp, 'png') !== false) { $maxColor = 251; $oldImg = imagecreatefrompng($cacheImageFileTmp); $sizes = getimagesize($cacheImageFileTmp); $newImg = imagecreatetruecolor($sizes[0], $sizes[1]); imagealphablending($newImg, false); imagesavealpha($newImg, true); imagecopyresampled($newImg, $oldImg, 0, 0, 0, 0, $sizes[0], $sizes[1], $sizes[0], $sizes[1]); $colorWhite = imagecolorallocate($newImg, 255, 255, 255); for ($y = 0; $y < ($sizes[1]); ++$y) { for ($x = 0; $x < ($sizes[0]); ++$x) { $isTransparent = false; $colorat = imagecolorat($newImg, $x, $y); if(($colorat & 0x7F000000) >> 24) { $isTransparent = true; } $r = ($colorat >> 16) & 0xFF; $g = ($colorat >> 8) & 0xFF; $b = $colorat & 0xFF; // Если цвет пикселя нас не устраивает, заменяем его на белый if (($r >= $maxColor && $g >= $maxColor && $b >= $maxColor) || ($isTransparent)) { imagesetpixel($newImg, $x, $y, $colorWhite); } } } imagepng($newImg, $cacheImageFileTmp, 9); } if (stripos($cacheImageFileTmp, 'jpg') !== false || stripos($cacheImageFileTmp, 'jpeg') !== false) { $maxColor = 251; $oldImg = imagecreatefromjpeg($cacheImageFileTmp); $sizes = getimagesize($cacheImageFileTmp); $newImg = imagecreatetruecolor($sizes[0], $sizes[1]); imagecopyresampled($newImg, $oldImg, 0, 0, 0, 0, $sizes[0], $sizes[1], $sizes[0], $sizes[1]); $colorWhite = imagecolorallocate($newImg, 255, 255, 255); for ($y = 0; $y < ($sizes[1]); ++$y) { for ($x = 0; $x < ($sizes[0]); ++$x) { $colorat = imagecolorat($newImg, $x, $y); $r = ($colorat >> 16) & 0xFF; $g = ($colorat >> 8) & 0xFF; $b = $colorat & 0xFF; // Если цвет пикселя нас не устраивает, заменяем его на белый if (($r >= $maxColor && $g >= $maxColor && $b >= $maxColor)) { imagesetpixel($newImg, $x, $y, $colorWhite); } } } imagejpeg($newImg, $cacheImageFileTmp, 100); } } } } |
Спасибо