php之图标点选验证码

一个图标点选的验证码

/**
     * 随机生成图标
     *
     * @return void
     */
    private function randChars($length = 0, $max = 15)
    {
        $result = [];

        $index = array();
        for ($i = 1; $i < $max + 1; $i++) {
            $index[$i] = $i;
        }

        $startOne = current($index);
        $endOne = end($index);

        for ($i = $startOne; $i < $endOne; $i++) {
            $one = mt_rand($i, $max);
            if ($index[$i] == $i) {
                $index[$i] = $index[$one];
                $index[$one] = $i;
            }
        }

        $result = array_slice($index, 0, $length);

        return $result;
    }


    /**
     * 压缩图片
     *
     * @return void
     */
    public function gzippngs($imags, $pers = 0.9)
    {
        $imagesize = getimagesize($imags);
        $x = intval($imagesize[0] * $pers);
        $y = intval($imagesize[1] * $pers);
        $image_wp = imagecreatetruecolor($x, $y);
        $image = imagecreatefrompng($imags);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $x, $y, $imagesize[0], $imagesize[1]);
        //背景透明度
        $color = imagecolorallocate($image_wp, 255, 255, 255);
        imagecolortransparent($image_wp, $color);
        imagefill($image_wp, 0, 0, $color);
        imagealphablending($image_wp, true);

        return $image_wp;
    }


 /**
     * 生成随机坐标数
     * 
     * @return void
     */
    public function randXy($sizes = [], $imgW = 0, $imgH = 0, $i = 0, $pers = 0.9, $arrays = [])
    {
        $xW = intval(intval($sizes[1] * $pers) / ($i + 1));
        $yW = intval(intval($sizes[1] * $pers) / ($i + 1));
        //x坐标 - 用上层图-边界(40*2)-原图的缩放比例
        $x = rand(($i + 1) * 20, $imgW - 80);

        $y = mt_rand($i * 40, $imgH - 80);

        if ($this->checkPointsX($arrays, $x, $y, 60, 60)) {
            $return = $this->randXy($sizes, $imgW, $imgH, $i, $pers, $arrays);
            // var_dump(1);
        } else {
            $return = array($x, $y);
        }

        return $return;
    }


 /**
     * 计算 是否重叠
     *
     * @return void
     */
    public function checkPointsX($arrays = [], $x = 0, $y = 0, $ws = 40, $hs = 40)
    {
        if (empty($arrays)) {
            return false;
        }


        foreach ($arrays as $ks => $vs) {

            if (
                $vs['x'] + $ws  > $x &&
                $x + $ws  > $vs['x'] &&
                $vs['y'] + $hs > $y &&
                $y + $hs > $y
            ) {
                return true;
            } else {
                return false;
            }
        }
    }


 /**
     * 生成验证的图片
     *
     * @return void
     */
    public function creates()
    {
        ob_clean();
        //背景图片
    
        //获取图片资源
        $res_img = imagecreatefromjpeg($imagePath);

        $tmp_array = [];
        list($imageWidth, $imageHeight, $imageType) = getimagesize($imagePath);
        $tmp_array['width'] = $imageWidth;
        $tmp_array['height'] = $imageHeight;


        $randArray = $this->randChars($this->nums) ?? [];

        //随机生成 图片的位置
        foreach ($randArray as  $ks => $v) {

            $file_img = . 'icons/' . $v . '.png';



            //判断图片是否要进行缩放,缩放比
            if ($this->isGzip) {
                $w_img = $this->gzippngs($file_img, 0.2);
            } else {
                $w_img = imagecreatefrompng($file_img);
                //背景透明度
                $color = imagecolorallocate($w_img, 255, 255, 255);
                imagecolortransparent($w_img, $color);
                imagefill($w_img, 0, 0, $color);
                imagealphablending($w_img, true);
            }

            $w_imgSize = getimagesize($file_img);



            //计算每个图标的尺寸
          
            list($x, $y) = $this->randXy($w_imgSize, $imageWidth, $imageHeight, $ks, 0.2, $tmp_array);
            $tmp_array[$v] = array('x' => $x, 'y' => $y);

            imagecopymerge($res_img, $w_img, $x, $y, 0, 0, 40, 40, 75);
        }

        //重新建立一个画布用来描述





        //生成图片
        switch ($imageType) {
            case 1: //GIF
                header('Content-Type: image/gif');
                imagegif($res_img);
                break;
            case 2: //JPG
                header('Content-Type: image/jpeg');
                imagejpeg($res_img);
                break;
            case 3: //PNG
                header('Content-Type: image/png');
                imagepng($res_img);
                break;
            default:
                break;
        }
        imagedestroy($res_img);
    }


posted @ 2022-09-07 09:44  尘梦  阅读(85)  评论(0编辑  收藏  举报