(☞ຈل͜ຈ)☞ Главная  Статьи  Загрузчик Домой

Ok!
Ok!
223
public function convertImage(&$content)
    {
        if (defined('ADMIN_SECTION') || defined('WIZARD_SITE_ID')) {
            return;
        }

        preg_match_all('/"(/upload/[^"]*.(jpg|jpeg|png))"/i', $content, $matches1);
        self::convertImageToWebp($content, $matches1);
        preg_match_all("/'(/upload/[^']*.(jpg|jpeg|png))'/i", $content, $matches2);
        self::convertImageToWebp($content, $matches2);
    }

    private static function convertImageToWebp(&$content, $matches) {
        if (!empty($matches[1])) {
            foreach ($matches[1] as $i => $imageUrl) {
                $root = $_SERVER['DOCUMENT_ROOT'];
                $type = $matches[2][$i];
                $newName = str_replace($type, 'webp', $imageUrl);
                if (file_exists($root . $newName)) {
                    $content = str_replace($imageUrl, $newName, $content);
                    continue;
                }
                if (!file_exists($root . $imageUrl)) {
                    continue;
                }
                $type = strtolower($type);
                switch ($type) {
                    case 'jpeg':
                    case 'jpg':
                        $img = imagecreatefromjpeg($root . $imageUrl);
                        break;
                    case 'png':
                        $img = imagecreatefrompng($root . $imageUrl);
                        imagepalettetotruecolor($img);
                        imagealphablending($img, true);
                        imagesavealpha($img, true);
                        break;
                }
                if (isset($img)) {
                    $result = imagewebp($img, $root . $newName, 75);
					
					if (!$result) {
						continue;
					}

                    $content = str_replace($imageUrl, $newName, $content);
                    imagedestroy($img);
                    unset($img);
                }
            }
        }
    }
bitrix, webp, convert20030convert image to webp bitrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14