php生成总值固定随机数字数组

简介

php代码生成数字数组,所有数字总和固定

    private function _getRandomNumberArray($total, $times, $min, $max)
    {
        $data = array();
        if ($min * $times > $total) {
            return array();
        }
        if ($max * $times < $total) {
            return array();
        }
        while ($times >= 1) {
            $times--;
            $kmix = max($min, $total - $times * $max);
            $kmax = min($max, $total - $times * $min);
            $kAvg = $total / ($times + 1);
            //获取最大值和最小值的距离之间的最小值
            $kDis = min($kAvg - $kmix, $kmax - $kAvg);
            //获取0到1之间的随机数与距离最小值相乘得出浮动区间,这使得浮动区间不会超出范围
            $r = ((float)(rand(1, 10000) / 10000) - 0.5) * $kDis * 2;
            $k = round($kAvg + $r);
            $total -= $k;
            $data[] = $k;
        }
        return $data;
    }

php隐藏图片下载地址

简介

有时根据图片url却不能下载图片。用php是如何实现的?

方法1

    public function test()
    {

        $img = "<img src='https://helpee.club/hoges/hoge2?f=minna.png'>";

        echo $img;

        $this->autoRender = false;

    }

方法2

    public function hoge2()
    {
        $file = $_GET['f'];

        $path = WWW_ROOT."img/".$file;
        header('Content-Disposition: inline; filename="' . $path . '"');

        //必要に応じ、png , jpg , gif などに変更
        header('Content-type: image/png');
        readfile($path);




        $this->autoRender = false;
    }

判断是不是iphone x齐刘海屏幕

判断规则

由于iphone x屏幕不是传统的长方形了,而是圆角矩形。如何判断当前的屏幕是长方形还是圆角矩形呢?
可以使用windowssafeAreaInsets方法,当返回值为0时,为长方形,非0时即认为是iphone x.

func isiPhoneXScreen() -> Bool {
        guard #available(iOS 11.0, *) else {
            return false
        }

        return UIApplication.shared.windows[0].safeAreaInsets != UIEdgeInsets.zero
}

Continue reading