<?php
    require_once('include/functions/tile_is_available.php');

    function create_quilt_image_cache($id)
    {
        $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = '" . $id . "'");
        if ($quilts_row = mysqli_fetch_assoc($quilts))
        {
            $im_dest = imagecreatetruecolor($quilts_row['quilt_width'] * $quilts_row['tile_width'], $quilts_row['quilt_height'] * $quilts_row['tile_height']);
            $trans_color = imagecolorallocate($im_dest, 0, 0, 1);
            imagefill($im_dest, 0, 0, $trans_color);
            for ($y = 1; $y <= $quilts_row['quilt_height']; $y++)
            {
                for ($x = 1; $x <= $quilts_row['quilt_width']; $x++)
                {
                    $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE quilt_id = '" . $id . "' AND matrix_x = '" . $x . "' AND matrix_y = '" . $y . "' AND deleted = '0'");
                    if ($tiles_row = mysqli_fetch_assoc($tiles))
                    {
                        list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y'], 1, 1);
                        if ($display)
                        {
                            mysqli_query_logged("UPDATE tiles SET cache_display = '1' WHERE tile_id = '" . $tiles_row['tile_id'] . "'");
                            $im_src = imagecreatefromstring($tiles_row['data_tile']);
                            imagecopy($im_dest, $im_src, $x * $quilts_row['tile_width'] - $quilts_row['tile_width'], $y * $quilts_row['tile_height'] - $quilts_row['tile_height'], 0, 0, $quilts_row['tile_width'], $quilts_row['tile_height']);
                        }
                        else
                        {
                            mysqli_query_logged("UPDATE tiles SET cache_display = '0' WHERE tile_id = '" . $tiles_row['tile_id'] . "'");
                        }
                    }
                }
            }
            $temp = make_cache_code();
            imagejpeg($im_dest, 'temp/' . $temp, 90);
            $imgData = file_get_contents('temp/' . $temp);
            unlink('temp/' . $temp);
            mysqli_query_logged("UPDATE quilts SET data_full_jpg = '" . addslashes($imgData) . "' WHERE id = '" . $id . "'");
            $temp = make_cache_code();
            imagepng($im_dest, 'temp/' . $temp);
            $imgData = file_get_contents('temp/' . $temp);
            unlink('temp/' . $temp);
            mysqli_query_logged("UPDATE quilts SET data_full_png = '" . addslashes($imgData) . "' WHERE id = '" . $id . "'");
            $width_orig = $quilts_row['quilt_width'] * $quilts_row['tile_width'];
            $height_orig = $quilts_row['quilt_height'] * $quilts_row['tile_height'];
            $width = 250;
            $height = 250;
            if ($width > $quilts_row['quilt_width'] * $quilts_row['tile_width'])
            {
                $width = $quilts_row['quilt_width'] * $quilts_row['tile_width'];
            }
            if ($height > $quilts_row['quilt_height'] * $quilts_row['tile_height'])
            {
                $height = $quilts_row['quilt_height'] * $quilts_row['tile_height'];
            }
            if ($width && ($width_orig < $height_orig))
            {
               $width = ($height / $height_orig) * $width_orig;
            }
            else
            {
               $height = ($width / $width_orig) * $height_orig;
            }
            $image_p = imagecreatetruecolor($width, $height);
            imagecopyresampled($image_p, $im_dest, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagecolortransparent($image_p, $trans_color);
            $temp = make_cache_code();
            imagepng($image_p, 'temp/' . $temp);
            $imgData = file_get_contents('temp/' . $temp);
            unlink('temp/' . $temp);
            mysqli_query_logged("UPDATE quilts SET data_thumb = '" . addslashes($imgData) . "' WHERE id = '" . $id . "'");
        }
    }
?>