<?php
header('Access-Control-Allow-Origin: *');
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
include_once __DIR__ . '/dompdf/autoload.inc.php';
$path = __DIR__ . '/img/';

//writeToLog($_POST, 'POST');
//writeToLog($_FILES, 'FILES');
//writeToLog(count($_POST['cards']),'COUNT');

function compress_img($path,$name,$w, $h) {
    //$temp = mkdir(rand());
    $vars =strrchr($path, "?"); // ?asd=qwe&stuff#hash
    //$name = preg_replace('/'. preg_quote($vars, '/') . '$/', '', basename($path)); // test.png
    $tmp_d = '/var/www/spa-bitrix.ru/masthev/create_presentation/img/'.$name;
    $file_load = file_put_contents($tmp_d,file_get_contents($path));
    $file = $tmp_d;
    //writeToLog (getimagesize($file),'iso');
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $info = getimagesize($file);
    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($file);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($file);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($file);
    //imagejpeg($image, $destination_url, $quality);
    //echo "Image uploaded successfully.";


    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    if ($info['mime'] == 'image/jpeg') $result_img = imagejpeg($dst,$tmp_d);
    elseif ($info['mime'] == 'image/gif') $result_img = imagegif($dst,$tmp_d);
    elseif ($info['mime'] == 'image/png') $result_img = imagepng($dst,$tmp_d);
    writeToLog($result_img);
    //return 'https://spa-bitrix.ru/masthev/create_presentation/img/'.$name;
}

//for ($i = 0; $i < count($_POST['cards']); $i++) {
foreach ($_POST['cards'] as $i => $vv) {
    $img_1 = NULL;
    $img_2 = NULL;
    unset($img_gorizontal);
    $img_all = NULL;
	//unset($array_img);
	$array_img = array();
    $img_block = array();
	$images = NULL;

    $name = $_POST['cards'][$i]['title'];
    $adress = $_POST['cards'][$i]['address'];
    $price = $_POST['cards'][$i]['fullPrice'];
	$price = intval(preg_replace('/[^0-9]+/', '', $price), 10);
    $text = $_POST['cards'][$i]['details'];
    //$text = htmlspecialchars($text);
    $text = nl2br($text);
    if (strlen($text)>1700) {
        $text = substr($text, 0, 1700);
        $text = substr($text, 0, strrpos($text,'.'));
        $text = $text.'.';
    }
    $square = $_POST['cards'][$i]['square'];
    $sleep = $_POST['cards'][$i]['bedroom'];
    $floor = $_POST['cards'][$i]['floor'];
    $finish = $_POST['cards'][$i]['otc'];
    $id = $_POST['cards'][$i]['id'];
    $currency = $_POST['cards'][$i]['currencyArr'];

    $arr = explode(',', $currency);
    //$author = rtrim($arr[0]);
    $currency = ltrim($arr[1]);

    $images = $_POST['cards'][$i]['urlImages'];
    $theme = $_POST['cards'][$i]['presType'];
    //writeToLog($images);

    if (!empty($images)) {
        $newImages = explode(',;,', $images);
        foreach ($newImages as $index => $image) {
            $array_img_url = explode(',', $image);
            //writeToLog($newImages);
            $from = $array_img_url[0];
            $to = $path .$_POST['cards'][$i]['title'].'_'. $index . '_img.png';
            copy($from, $to);
            $array_img[$index] = preg_replace('/\s+/', '%20', 'https://spa-bitrix.ru/masthev/create_presentation/img/' .$_POST['cards'][$i]['title'].'_'. $index . '_img.png');
            //$array_img[$index] = 'https://spa-bitrix.ru/masthev/create_presentation/img/' .$_POST['cards'][$i]['title'].'_'. $index . '_img.png';
            $name_files = $_POST['cards'][$i]['title'].'_'. $index . '_img.png';
            compress_img($array_img[$index],$name_files,1500,960);
            //writeToLog($array_img[$index], 'array_img');
            //ttests
            // if ($array_img_url[1] == 'true') {
            //     $img_1 = $array_img[$index];
            //     //writeToLog($img_1, 'IMAGES_1');
            // } else if ($array_img_url[2] == 'true') {
            //     $img_2 = $array_img[$index];
            //     //writeToLog($img_1, 'IMAGES_2');
            // }
        }
    }
    $img_1 = $array_img[0];
    $img_2 = $array_img[1];

   // writeToLog($array_img);
    for ($y=2; $y<=count($array_img); $y++ ) {
        //compress_img ($array_img[$y],1000,640);
        $size = getimagesize($array_img[$y]);
        $height_img = $size[0]; // Высота картинки
        $width_img = $size[1];  // Ширина картинки
        $width_img = $size[1];  // Ширина картинки
        //writeToLog($array_img[$y],'NAME');
        ///writeToLog($size,'SIZE');
        //writeToLog($y,'Y');
        if ($height_img > $width_img) {
            
            //Горизонтальная
            if ($theme == 'enable') {
                $img_block[] = '
                <div class="slid1">
                <div class="header">
                    <div class="logo">
                        <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                    </div>
                    <div class="info">
                        <h1>+7 (985) 222 47 47</h1>
                        <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                        <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                        <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                    </div>
                </div>
                <div  class="content">
                    <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $array_img[$y] . '" alt="">
                </div>
                </div>';
                //writeToLog($img_block,'img_block');
            }else if ($theme == "disable") {
                $img_block[] = '
            <div class="slid1">
            <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
            <div  class="content">
                <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="'. $array_img[$y].'" alt="">
            </div>
            </div>';
            }
        } else if ($height_img < $width_img) {
            //Вертикальная
            $size = getimagesize($array_img[$y+1]);
            $height_img = $size[0]; // Высота картинки
            $width_img = $size[1];  // Ширина картинки
            if ($height_img > $width_img) {
                //Горизонтальная
                //$y++;
            } else if ($height_img < $width_img) {
                //Вертикальная
                if ($theme == "enable") {
                    $img_block[] = '
                    <div class="slid1">
                    <div class="header">
                        <div class="logo">
                            <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                        </div>
                        <div class="info">
                            <h1>+7 (985) 222 47 47</h1>
                            <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                            <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                            <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                        </div>
                    </div>
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="' . $array_img[$y] . '" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="' . $array_img[$y + 1] . '" alt="">
                    </div>
                    </div>';
                }else if ($theme == "disable"){
                    $img_block[] = '
                    <div class="slid1">
                    <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="'.$array_img[$y].'" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="'.$array_img[$y+1].'" alt="">
                    </div>
                    </div>';
                }
                $y++;

            }
        }

    }
   
    //$text = substr($text, 0, 1000);


    if ($sleep <> '') {
        $block_sleep = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/sleep.png" alt="">  Спальни: ' . $sleep . '</p>';
    } else {
        $block_sleep = "";
    }
    if ($floor <> '') {
        $block_floor = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/floor.png" alt="">  Этаж: ' . $floor . '</p>';
    } else {
        $block_floor = "";
    }
    if ($finish <> '') {
        $block_finish = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/finish.png" alt="">  Отделка: ' . $finish . '</p>';
    } else {
        $block_finish = "";
    }
    if (!empty($_FILES['files']['tmp_name'][$i]['main'])) {
        copy($_FILES['files']['tmp_name'][$i]['main'], $path . $_FILES['files']['name'][$i]['main']);
        $img_1 = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i]['main'];
    }

    if (!empty($_FILES['files']['tmp_name'][$i]['second'])) {
        copy($_FILES['files']['tmp_name'][$i]['second'], $path . $_FILES['files']['name'][$i]['second']);
        $img_2 = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i]['second'];
    }

    for ($x=0; $x<=count($_FILES['files']['name'][$i]); $x++) {
        copy($_FILES['files']['tmp_name'][$i][$x], $path . $_FILES['files']['name'][$i][$x]);
        $img_add[] = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i][$x];
    }

    writeToLog($img_add, "IMG_ADD");
    for ($y=0; $y<=count($img_add); $y++ ) {
        $size = getimagesize($img_add[$y]);
        $height_img = $size[0]; // Высота картинки
        $width_img = $size[1];  // Ширина картинки
        //writeToLog($array_img[$y],'NAME');
        ///writeToLog($size,'SIZE');
        //writeToLog($y,'Y');
        if ($height_img > $width_img) {
            
            //Горизонтальная
            if ($theme == 'enable') {
                $img_block[] = '
                <div class="slid1">
                <div class="header">
                    <div class="logo">
                        <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                    </div>
                    <div class="info">
                        <h1>+7 (985) 222 47 47</h1>
                        <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                        <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                        <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                    </div>
                </div>
                <div  class="content">
                    <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_add[$y] . '" alt="">
                </div>
                </div>';
                //writeToLog($img_block,'img_block');
            }else if ($theme == "disable") {
                $img_block[] = '
            <div class="slid1">
            <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
            <div  class="content">
                <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="'. $img_add[$y].'" alt="">
            </div>
            </div>';
            }
        } else if ($height_img < $width_img) {
            //Вертикальная
            $size = getimagesize($img_add[$y+1]);
            $height_img = $size[0]; // Высота картинки
            $width_img = $size[1];  // Ширина картинки
            if ($height_img > $width_img) {
                //Горизонтальная
                //$y++;
            } else if ($height_img < $width_img) {
                //Вертикальная
                if ($theme == "enable") {
                    $img_block[] = '
                    <div class="slid1">
                    <div class="header">
                        <div class="logo">
                            <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                        </div>
                        <div class="info">
                            <h1>+7 (985) 222 47 47</h1>
                            <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                            <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                            <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                        </div>
                    </div>
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="' . $img_add[$y] . '" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="' . $img_add[$y + 1] . '" alt="">
                    </div>
                    </div>';
                }else if ($theme == "disable"){
                    $img_block[] = '
                    <div class="slid1">
                    <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="'.$img_add[$y].'" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="'.$img_add[$y+1].'" alt="">
                    </div>
                    </div>';
                }
                $y++;

            }
        }

    }





    
    // foreach ($_FILES['files']['tmp_name'][$i] as $key => $value) {

    //     if (!(($key <> 'main') xor ($key <> 'second'))) {
    //         // writeToLog($key, 'key');
    //         copy($_FILES['files']['tmp_name'][$i][$key], $path . $_FILES['files']['name'][$i][$key]);
    //         $size = getimagesize($path . $_FILES['files']['name'][$i][$key]);
    //         $height_img = $size[0]; // Высота картинки
    //         $width_img = $size[1];  // Ширина картинки
    //         if ($height_img == $width_img) {
    //             echo "Квадратная";
    //         } else if ($height_img > $width_img) {

    //             $img_gorizontal[] = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i][$key];
    //         } else {
    //             $img_vertical[] = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i][$key];
    //         }

    //     }
    // }

    $price_m2 = $price / $square;
    $price_m2 = round($price_m2);
    //writeToLog(format_price($price_m2, $cur), 'PRICE_M2');
    
    $cur = $currency;
    //writeToLog( $cur, 'CURR');
    if ($_POST['cards'][$i]["typeDeal"] == 'Продажа') {
        $price_temp = '<p style="padding-top:5px;color:white;font-size:22px;padding-left:25px">'.format_price($price, $cur).'</p>';
    } else if ($_POST['cards'][$i]["typeDeal"] == 'Аренда') {
        $price_temp = '<p style="padding-top:20px;color:white;font-size:22px;padding-left:25px">' . format_price($price, $cur) . ' в мес</p>';
    }
    if (!empty($img_vertical)) {
        if (count($img_vertical) % 2 == 0) {
            for ($i = 0; $i < count($img_vertical); $i++) {
                if ($theme == "enable") {
                    $slide = '
                    <div class="slid1">
                    <div class="header">
                        <div class="logo">
                            <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                        </div>
                        <div class="info">
                            <h1>+7 (985) 222 47 47</h1>
                            <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                            <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                            <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                        </div>
                    </div>
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="' . $img_vertical[$i] . '" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="' . $img_vertical[$i + 1] . '" alt="">
                    </div>
                    </div>';
                }else if ($theme == "disable"){
                    $slide = '
                    <div class="slid1">
                    <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
                    <div style="padding-top:130px;"class="content">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="'.$img_vertical[$i].'" alt="">
                        <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="'.$img_vertical[$i+1].'" alt="">
                    </div>
                    </div>';
                }
                
                $i++;
                $img_all = $img_all . $slide;
            }
        }
    }


    for ($i = 0; $i < count($img_gorizontal); $i++) {
        if ($theme == "enable") {
            $slide = '
            <div class="slid1">
            <div class="header">
                <div class="logo">
                    <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                </div>
                <div class="info">
                    <h1>+7 (985) 222 47 47</h1>
                    <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                    <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                    <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                </div>
            </div>
            <div  class="content">
                <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_gorizontal[$i] . '" alt="">
            </div>
            </div>';
        }else if ($theme == "disable") {
            $slide = '
        <div class="slid1">
        <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
        <div  class="content">
            <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="'.$img_gorizontal[$i].'" alt="">
        </div>
        </div>';
        }else if ($theme == "photo") {
            $slide = '
            <div class="slid1">
            <div class="header">
                <div class="logo">
                    <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                </div>
                <div class="info">
                    <h1>+7 (985) 222 47 47</h1>
                    <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                    <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                    <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                </div>
            </div>
            <div  class="content">
                <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_gorizontal[$i] . '" alt="">
            </div>
            </div>';
        }
        
        $img_all = $img_all . $slide;
    }
    foreach ($img_block as $block) {
        $img_all = $img_all.$block;
    }
    if ($theme == "enable") {
        $a_html = create_code($name, $adress, $price_temp, $price_m2, $cur, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id);
        $f_html = $f_html . $a_html;
    }else if ($theme == "disable") {
        $a_html = create_code_dis($name, $adress, $price_temp, $price_m2, $cur, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id);
        $f_html = $f_html . $a_html;
    }else if ($theme == "photo") {
        $a_html = create_code_photo($img_1, $img_2, $img_all);
        $f_html = $f_html . $a_html;
    }


}
//writeToLog($img_all, 'img_code');
print_r(create_presentation($f_html,$name,$square));

function create_code($name, $adress, $price_temp, $price_m2, $cur, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id)
{
//	
    $html = '
    <div class="slid1">
        <div class="header">
            <div class="logo">
                <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
            </div>
            <div class="info">
                <h1>+7 (985) 222 47 47</h1>
                <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
            </div>
        </div>
        <div class="content">
        <img style="padding-top:40px;padding-left:60px" class="img_other" src="'.$img_1.'" alt="">
        
        </div>
        <div class="box">
            <p style="padding-top: 10px;color:white;font-size:21px;padding-left:25px" >'.$name.'</p>
            <p style="padding-top:5px;color:white;font-size:16px;padding-left:25px">'.$adress.'</p>
            '.$price_temp.'
            <p style="padding-top:5px;color:white;font-size:16px;padding-left:25px">'.$square.' м2</p>
        </div>
        
    </div>
    <div class="slid1">
        <div class="header">
            <div class="logo">
                <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
            </div>
            <div class="info">
                <h1>+7 (985) 222 47 47</h1>
                <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
            </div>
        </div>
        <div class="content_2">
        <table cellspacing="0" cellpadding="0" id="maket"  style="padding-top:25px;">
        <tr> 
         <td id="leftcol"><p style="margin-left: 25px;color:white;font-size:15px;width:500px;">'.$text.'
         </p></td>
            <td>
                <img style="position:absolute;top: 200px;left: 625px;width: 400px; height:250px;"  src="'.$img_2.'" alt="">

            </td>
        </tr>
        <tr>
            <td></td>
            <td><div style="position:absolute;top: 475px;left: 625px;width:400px;background-color:white;height:200px;">
            <p style="margin-left:25px;font-size:15px;padding-top:25px"><img style="width:25px;height:26px;" src="https://spa-bitrix.ru/masthev/create_presentation/img/square.png" alt="">  Стоимость за м²: '.format_price($price_m2, $cur).'</p>
            '.$block_sleep.'
            '.$block_floor.'
            '.$block_finish.'
            </div></td>
        </tr>
       </table>
        </div>
        
    </div>
    '.$img_all.'
    <div class="slid1">
    <img style="width:1120px;height:791px;" src="https://spa-bitrix.ru/masthev/create_presentation/img/end_slide.png">
    </div>';
    return $html;
}
function create_code_photo($img_1, $img_2, $img_all)
{
//	
    $html = '
    <div class="slid1">
    <div class="header">
        <div class="logo">
            <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
        </div>
        <div class="info">
            <h1>+7 (985) 222 47 47</h1>
            <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
            <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
            <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
        </div>
    </div>
    <div  class="content">
        <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_1 . '" alt="">
    </div>
    </div>
    <div class="slid1">
    <div class="header">
        <div class="logo">
            <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
        </div>
        <div class="info">
            <h1>+7 (985) 222 47 47</h1>
            <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
            <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
            <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
        </div>
    </div>
    <div  class="content">
        <img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_2 . '" alt="">
    </div>
    </div>
    '.$img_all.'
    <div class="slid1">
    <img style="width:1120px;height:791px;" src="https://spa-bitrix.ru/masthev/create_presentation/img/end_slide.png">
    </div>';
    return $html;
}
function create_code_dis($name, $adress, $price_temp, $price_m2, $cur, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id)
{
//	
    $html = '
    <div class="slid1">
    <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
        
        <div class="content">
        <img style="padding-top:40px;padding-left:60px" class="img_other" src="'.$img_1.'" alt="">
        
        </div>
        <div class="box">
            <p style="padding-top: 10px;color:white;font-size:21px;padding-left:25px" >'.$name.'</p>
            <p style="padding-top:5px;color:white;font-size:16px;padding-left:25px">'.$adress.'</p>
            '.$price_temp.'
            <p style="padding-top:5px;color:white;font-size:16px;padding-left:25px">'.$square.' м2</p>
        </div>
        
    </div>
    <div class="slid1">
    <img style="width:1120px;height:93px;" src="http://94.198.219.190/img/header.jpg" alt="header">
        <div class="content_2">
        <table cellspacing="0" cellpadding="0" id="maket"  style="padding-top:25px;">
        <tr> 
         <td id="leftcol"><p style="margin-left: 25px;color:white!important;font-size:16px;width:500px;">'.$text.'
         </p></td>
            <td>
                <img style="position:absolute;top: 200px;left: 625px;width: 400px; height:250px;"  src="'.$img_2.'" alt="">

            </td>
        </tr>
        <tr>
            <td></td>
            <td><div style="position:absolute;top: 475px;left: 625px;width:400px;background-color:white;height:200px;">
            <p style="margin-left:25px;font-size:15px;padding-top:25px"><img style="width:25px;height:26px;" src="http://94.198.219.190/img/square.png" alt="">  Стоимость за м²: '.format_price($price_m2, $cur).'</p>
            '.$block_sleep.'
            '.$block_floor.'
            '.$block_finish.'
            </div></td>
        </tr>
       </table>
        </div>
        
    </div>
'.$img_all;
    return $html;
}

function create_presentation($html,$name,$square)
{
    $mainHtml = $basic = '<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style type="text/css">
    
            *,*{
                margin: 0px;
                padding: 0px
            }
    
            body {
                font-family: dejavu serif;
                background-color: #d4d4d4;
                
            }
    
            .slid1 {
                width: 1120px;
                height: 791px;
                background: #FFFFFF;
                border: 1 solid #a5a5a5;
            }
    
            .header{
                height: 93px;
                background: #E7E7E9;
                padding: 15px 70px;
            }
    
            h1 {
                font-size: 11px;
            }
    
            p{
                font-size: 9px;
            }
    
            .info{
                text-align: right;
                position: absolute;
                width:165px;
                height: 81px;
                left: 885px;
                top: 20px;
    
            }
    
            img {
                width: 103px;
                height: 66px;
            }
    
            .photo1 {
                width: 578px;
                height: 375px;
            }
    
            .block1{
                float: right;
            }
    
            .photo2 {
                position: absolute;
                width: 360px;
                height: 235px;
                /* left: 500px; */
            }
    
            .photo {
                padding: 20px 60px;
            }
    
            .description {
                width: 360px;
                height: 95px;
                font-size: 10px;
                color: #878787;
            }
            .fio {
                color: #878787;
                font-size: 10px;
            }
    
            .fio_text{
                bottom: 20px;
                float: right;
                position: relative;
                margin-right:10px;
            }
            .text-content {
                
                position: relative;
                top: 230px;
                
            }
    
            .block {
                width: 578px;
                height: 165px;
                background: #294651;
                margin-left:60px;
                float: left;
            }
            .info-block{
                color: #FFFFFF;
                padding: 10px 0 0 15px;
            }
    
            .info-block > h1 {
                font-size: 21px;
                margin: 0;
                padding-bottom: 10px;
            }
    
            .info-block > p {
                font-size: 12px;
                margin: 0;
                padding-bottom: 20px;
            }
    
            .info-block > h2 {
                font-size: 21px;
                margin: 0;
                padding: 15px 0 10px 0;
            }
        
            .slid2 {
                width: 842px;
                height: 595px;
                background: #FFFFFF;
                margin-top: 40px;
            }
            .logo {
                padding-top:15px;
            }
            .img_other {
                width: 1000px;
                height: 578px;
            }
            .box {
                background-color: #294651;
                width: 480px;
                height: 175px;
                position: absolute;
                left: 60px;
                top: 520px;
            }
            .content_2 {
                background-color: #294651;
                width: 1000px;
                height: 578px;
                margin-left:60px;
                margin-top: 40px;
            }
            </style>
    </head><body style="background-color:#ffffff;">' . $html . '
    </body>
    </html>
    ';
    //writeToLog($mainHtml, 'html');
    $dompdf = new Dompdf\Dompdf();
    $dompdf->set_option('isRemoteEnabled', TRUE);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->loadHtml($mainHtml, 'UTF-8');
    $dompdf->render();
    $pdf = $dompdf->output();
    //file_put_contents(__DIR__ . '/presentation/1.pdf', $pdf);
    file_put_contents(__DIR__ . '/presentation/'.$name.' - '.$square.'.pdf', $pdf);
    $url = 'https://spa-bitrix.ru/masthev/create_presentation/presentation/'.$name.' - '.$square.'.pdf';
    //writeToLog($url, 'URL');
    return $url;
}


function format_price($value, $unit)
{
    if ($value > 0) {
        $value = number_format($value, 2, ',', ' ');
        $value = str_replace(',00', '', $value);

        if (!empty($unit)) {
            $value .= ' ' . $unit;
        }
    }

    return $value;
}

function writeToLog($data, $title = '')
{
    $log = "\n------------------------\n";
    $log .= date("Y.m.d G:i:s") . "\n";
    $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n";
    $log .= print_r($data, 1);
    $log .= "\n------------------------\n";
    file_put_contents(getcwd() . '/log.log', $log, FILE_APPEND);
    return true;
}

?>