php 裁剪图片

源代码

public function upload3()
    {
        $pid = $_SESSION['cmpid'];
        $allow_type = array('jpg', 'jpeg', 'gif', 'png'); //定义允许上传的类型
        $file = $this->loadModel('businesscard')->file($_FILES['file'], $allow_type, 'communication', 'img');
        $ramd = $this->loadModel('businesscard')->random(6, 0);
        $filename=time() . $ramd;
        if ($file) {
            $left = $this->post->left;
            $width = $this->post->width;
            $height = $this->post->height;
            $top = $this->post->top;

            $image = '../../' . $file;
            $source = imagecreatefromjpeg($image);
            $croped = imagecreatetruecolor($width, $height);
            imagecopy($croped, $source, 0, 0, $left, $top, $width, $height);
            imagejpeg($croped, "$filename.jpg");
            imagedestroy($croped);
            unlink('../../' . $file);
            $fielPath=preg_split('/\//', $file);
            if (copy("$filename.jpg", "../../$fielPath[0]/$fielPath[1]/$fielPath[2]/$fielPath[3]/$fielPath[4]/$fielPath[5]/$filename.jpg")) {
                unlink("$filename.jpg");
                $this->dao->insert(TABLE_CSLIDERIMG)
                    ->set('url')->eq("$fielPath[0]/$fielPath[1]/$fielPath[2]/$fielPath[3]/$fielPath[4]/$fielPath[5]/$filename.jpg")
                    ->set('create_time')->eq(helper::now(time()))
                    ->set('cpid')->eq($pid)
                    ->exec();
                echo 1;
            } else {
                echo 0;
            }
        } else {
            echo 0;
        }
    }

posted @ 2020-07-18 17:00  办公魔盒  阅读(124)  评论(0)    收藏  举报