<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/29
 * Time: 17:32
 */

namespace app\home\controller;


use think\Lang;

class Cut extends BaseMember
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH.'home/lang/zh-cn/cut.lang.php');
    }

    public function pic_upload()
    {
        if (input('param.form_submit')=='ok') {
            //halt(44);
            //上传图片
           /* $base_url = input('param.uploadpath');
            $image_url = BASE_UPLOAD_PATH . DS . $base_url;
            halt($image_url);*/
            if (!empty($_FILES['_pic']['tmp_name'])) {
                $file_object = request()->file('_pic');
                $base_url = input('param.uploadpath');
                $image_url = BASE_UPLOAD_PATH . DS . $base_url;
                $info = $file_object->validate(['ext' => 'jpg,png,gif'])->move($image_url);
                if ($info) {
                    json_encode(array('status' => 1, 'url' => UPLOAD_SITE_URL . '/' . $base_url . '/' . $info->getFilename()));
                    exit;
                }
                else {
                   json_encode(array('status' => 0, 'msg' => $file_object->getError()));
                   exit;
                }
            }
        }
    }

    /**
     * 图片裁剪
     *
     */
    public function pic_cut()
    {
        if (request()->isPost()) {
            $thumb_width = $_POST['x'];
            $x1 = $_POST["x1"];
            $y1 = $_POST["y1"];
            $x2 = $_POST["x2"];
            $y2 = $_POST["y2"];
            $w = $_POST["w"];
            $h = $_POST["h"];
            $scale = $thumb_width / $w;
            $src = str_ireplace(config('url_domain_root'), ROOT_PATH, $_POST['url']);
            $src = str_replace('..', '', $src);

            $cropped = resize_thumb($src, $src, $w, $h, $x1, $y1, $scale);
            if (is_file($src)) {
                @unlink($src);
            }
            $pathinfo = pathinfo($src);
            exit($pathinfo['basename']);
        }
            $save_file = str_ireplace(config('url_domain_root'), ROOT_PATH, $_POST['url']);
            $_GET['x'] = (intval(input('param.x')) > 50 && input('param.x') < 400) ? input('param.x') : 200;
            $_GET['y'] = (intval(input('param.y')) > 50 && input('param.y') < 400) ? input('param.y') : 200;
            $_GET['resize'] = input('param.resize') == '0' ? '0' : '1';
            $imageinfo=getimagesize($save_file);
             $this->assign('height', $imageinfo[1]);
             $this->assign('width',  $imageinfo[0]);

            return $this->fetch($this->template_dir.'cut');

    }
    function resize_thumb($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        if (config('thumb.cut_type') == 'im'){
            $exec_str = rtrim(config('thumb.impath'),'/').'/convert -quality 100 -crop '.$width.'x'.$height.'+'.$start_width.'+'.$start_height.' -resize '.$newImageWidth.'x'.$newImageHeight.' '.$image.' '.$thumb_image_name;
            exec($exec_str);
        }else{
            list($imagewidth, $imageheight, $imageType) = getimagesize($image);
            $imageType = image_type_to_mime_type($imageType);
            $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
            $white = imagecolorallocate($newImage, 255, 255, 255);
            imagefill($newImage, 0, 0, $white);
            switch($imageType) {
                case "image/gif":
                    $source=imagecreatefromgif($image);
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    $source=imagecreatefromjpeg($image);
                    break;
                case "image/png":
                case "image/x-png":
                    $source=imagecreatefrompng($image);
                    break;
            }
            $dst_w = $dst_h = 0;
            if ($newImageWidth > $width) {
                $dst_w = ($newImageWidth - $width)/2;
            }
            if ($newImageHeight > $height) {
                $dst_h = ($newImageHeight - $height)/2;
            }
            if ($dst_w > 0) {
                imagecopyresampled($newImage,$source,$dst_w,$dst_h,$start_width,$start_height,$width,$height,$width,$height);
            } else {
                imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
            }

            switch($imageType) {
                case "image/gif":
                    imagegif($newImage,$thumb_image_name);
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    imagejpeg($newImage,$thumb_image_name,100);
                    break;
                case "image/png":
                case "image/x-png":
                    imagepng($newImage,$thumb_image_name);
                    break;
            }
        }
        return $thumb_image_name;
    }
}