<?php
class ControllerPiyersoftWriteNameCase extends Controller{

    public function image($model, $knife, $product_id)
    {

        if(!file_exists($knife))
            return '';

        if(file_exists(DIR_IMAGE . 'catalog/mask/' . $product_id . '.png'))
            return 'catalog/mask/' . $product_id . '.png';

        $pattern = DIR_IMAGE . 'patterns/' . $model;
        $mask = $knife;


        $base = new Imagick($pattern);
        $mask = new Imagick($mask);

        $base->setImageMatte(1);

        $base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0); //orignal transparent
        //$base->scaleImage(528, 960, false);
        $base->scaleImage(275, 500, false);
        $base->borderImage('rgba(0%, 0%, 0%, 0.0)', 112, 0);
        $base->writeImage(DIR_IMAGE . 'catalog/mask/' . $product_id . '.png');

        return 'catalog/mask/' . $product_id . '.png';

        //header("Content-Type: image/png");
        //echo $base;



        //header("Content-Type: image/png");
        //echo $background_img = $this->create1000x1000Background(); die();

        //$pattern = DIR_IMAGE . 'catalog/pattern.png';
        //$mask = DIR_IMAGE . 'repository/knifes/XIAOMI-MI_6_PRO-SEFFAF-SEFFAF_SAYDAM-BLACK.PNG';

        //$imagick = new \Imagick($mask);

        //header("Content-Type: image/jpg");
        //echo $imagick->getImageBlob();

        //$imagick->borderImage('rgba(0%, 0%, 0%, 0.0)', 100, 100);

        /*
        $mask = DIR_IMAGE . 'repository/knifes/APPLE-IPHONE_11_PRO_MAX-SILICON-SILICON_PINK.PNG';

        $mask = new \Imagick(realpath($mask));
        $mask->scaleImage(528, 960, false);
        header("Content-Type: image/png");
        $mask->getImageBlob();
        echo $mask; die();

        $mask->setImageMatte(1);

        $mask->compositeImage($background_img, Imagick::COMPOSITE_DSTIN, 0, 0); //orignal transparent

        */



        /*
        $pattern = DIR_IMAGE . 'catalog/pattern.png';
        $mask = DIR_IMAGE . 'repository/knifes/XIAOMI-MI_6_PRO-SEFFAF-SEFFAF_SAYDAM-BLACK.PNG';


        $base = new Imagick($pattern);
        $mask = new Imagick($mask);
        $base->setImageMatte(1);

        // Create composite of two images using DSTIN
        // See: http://www.imagemagick.org/Usage/compose/#dstin

        //$base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
        //$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
        $base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0); //orignal transparent

        // Write image to a file.
        //$base->writeImage(DIR_IMAGE . 'newimage.png');

        // And/or output image directly to browser
        header("Content-Type: image/png");
        echo $base;

        */





        /*
        $pattern = DIR_IMAGE . 'catalog/pattern.png';
        $mask = DIR_IMAGE . 'repository/knifes/XIAOMI-MI_6_PRO-SEFFAF-SEFFAF_SAYDAM-BLACK.PNG';

        // Create new objects from png's
        $base = new Imagick($pattern);
        $mask = new Imagick($mask);

        // IMPORTANT! Must activate the opacity channel
        // See: http://www.php.net/manual/en/function.imagick-setimagematte.php
        $base->setImageMatte(1);

        // Create composite of two images using DSTIN
        // See: http://www.imagemagick.org/Usage/compose/#dstin

        //$base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
        //$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
        $base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0); //orignal transparent

        // Write image to a file.
        //$base->writeImage(DIR_IMAGE . 'newimage.png');

        // And/or output image directly to browser
        header("Content-Type: image/png");
        echo $base;

        */


    }

    public function index()
    {



        die();

        ini_set('default_socket_timeout', 120);
        set_time_limit(0);

        //unset($this->session->data['patterns']);

        if(!isset($this->session->data['patterns'])){
            $this->session->data['patterns'] = $this->getPatternFiles();
        }

        //echo count($this->session->data['patterns']) . '<br>'; die();

        $image_path = DIR_IMAGE . 'repository/knifes/';
        $count = 0;
        foreach ($this->session->data['patterns'] as $key => $pattern){
            $model = explode('.', $pattern);
            $model = $model[0];
            $products = $this->db->query("SELECT product_id, image, model, model2 FROM ps_product WHERE model = '". $this->db->escape($model) ."' AND mask = '' AND ( model2 LIKE '%neon%' OR model2 LIKE '%aquarium%' ) ")->rows;
            if($products){

                foreach ($products as $product){
                    if($product['image']){
                        $item = explode('/', $product['image']);
                        if(isset($item[3])){
                            $item = $item[3];
                            $item = explode('.', $item);
                            $item = $item[0];
                            $item = explode('-', $item);

                            if(isset($item[4]) AND $item[4] == '1'){
                                $item = $image_path . $item[1] . '-' . $item[2] . '-' . $item[3] . '-' . $item[4] . '.PNG';
                            }

                            if(isset($item[5]) AND $item[5] == '1'){
                                $item = $image_path . $item[1] . '-' . $item[2] . '-' . $item[3] . '-' . $item[4] . '.PNG';
                            }

                            if(isset($item[6]) AND $item[6] == '1'){
                                $item = $image_path . $item[1] . '-' . $item[2] . '-' . $item[3] . '-' . $item[4] . '-' . $item[5] . '.PNG';
                            }

                            if(file_exists($item)){ $count++;
                                $mask_url = $this->image($pattern, $item, $product['product_id']);
                                $this->db->query("UPDATE ps_product SET mask = '". $this->db->escape($mask_url) ."' WHERE product_id = '". (int)$product['product_id'] ."' ");
                                //echo $product['product_id'] . '<br>'; //die();
                            }

                        }
                    }
                }

            }else{
                //unset($this->session->data['patterns'][$key]);
            }

            //unset($this->session->data['patterns'][$key]);

        }

        echo $count . '<br> Bitti';

        die();

        //$this->session->data['products'] = $this->db->query("SELECT product_id, image, model, model2 FROM ps_product ")->rows;

        //echo count($this->session->data['products']) . ' Adet Ürün var <br><br><br><br><br>';

        /*

        $image_path = DIR_IMAGE . 'repository/knifes/';
        $count = 0;
        foreach ($this->session->data['products'] as $product){
            if($product['image']){
                $item = explode('/', $product['image']);
                if(isset($item[3])){
                    $item = $item[3];
                    $item = explode('.', $item);
                    $item = $item[0];
                    $item = explode('-', $item);
                    if(isset($item[5]) AND $item[5] == '1'){
                        $item = $image_path . $item[1] . '-' . $item[2] . '-' . $item[3] . '-' . $item[4] . '.PNG';
                    }

                    if(isset($item[6]) AND $item[6] == '1'){
                        $item = $image_path . $item[1] . '-' . $item[2] . '-' . $item[3] . '-' . $item[4] . '-' . $item[5] . '.PNG';
                    }

                    if(file_exists($item)){ $count++;
                        //echo $item . '<br>';
                    }

                }
            }
        }

        echo "Bitti " . $count;


        */







    }

    public function getPatternFiles()
    {

        $files = array();
        if ($handle = opendir('./image/patterns')) {

            while (false !== ($entry = readdir($handle))) {

                if ($entry != "." && $entry != "..") {

                    //echo "$entry\n";
                    $files[] = $entry;
                }
            }

            closedir($handle);
        }

        return $files;

    }

}