|
|
@@ -7,450 +7,450 @@ use yii\helpers\Json;
|
|
|
|
|
|
class util
|
|
|
{
|
|
|
-
|
|
|
- //判断是否微信浏览器
|
|
|
- public static function isWx()
|
|
|
- {
|
|
|
- if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- //根据两点间的经纬度计算距离
|
|
|
- public static function getDistance($lat1, $lng1, $lat2, $lng2)
|
|
|
- {
|
|
|
- $earthRadius = 6367000; //approximate radius of earth in meters
|
|
|
-
|
|
|
- /*
|
|
|
- Convert these degrees to radians
|
|
|
- to work with the formula
|
|
|
- */
|
|
|
-
|
|
|
- $lat1 = ($lat1 * pi()) / 180;
|
|
|
- $lng1 = ($lng1 * pi()) / 180;
|
|
|
-
|
|
|
- $lat2 = ($lat2 * pi()) / 180;
|
|
|
- $lng2 = ($lng2 * pi()) / 180;
|
|
|
-
|
|
|
- /*
|
|
|
- Using the
|
|
|
- Haversine formula
|
|
|
-
|
|
|
- http://en.wikipedia.org/wiki/Haversine_formula
|
|
|
-
|
|
|
- calculate the distance
|
|
|
- */
|
|
|
-
|
|
|
- $calcLongitude = $lng2 - $lng1;
|
|
|
- $calcLatitude = $lat2 - $lat1;
|
|
|
- $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
|
|
|
- $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
|
|
|
- $calculatedDistance = $earthRadius * $stepTwo;
|
|
|
-
|
|
|
- return round($calculatedDistance);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成缩略图
|
|
|
- * @author yangzhiguo0903@163.com
|
|
|
- * @param string 源图绝对完整地址{带文件名及后缀名}
|
|
|
- * @param string 目标图绝对完整地址{带文件名及后缀名}
|
|
|
- * @param int 缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
|
|
|
- * @param int 缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
|
|
|
- * @param int 是否裁切{宽,高必须非0}
|
|
|
- * @param int /float 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public static function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
|
|
|
- {
|
|
|
- if (!is_file($src_img)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- $ot = pathinfo($dst_img, PATHINFO_EXTENSION);
|
|
|
- $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
|
|
|
- $srcinfo = getimagesize($src_img);
|
|
|
- $src_w = $srcinfo[0];
|
|
|
- $src_h = $srcinfo[1];
|
|
|
- $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
|
|
|
- $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
|
|
|
-
|
|
|
- $dst_h = $height;
|
|
|
- $dst_w = $width;
|
|
|
- $x = $y = 0;
|
|
|
-
|
|
|
- /**
|
|
|
- * 缩略图不超过源图尺寸(前提是宽或高只有一个)
|
|
|
- */
|
|
|
- if (($width > $src_w && $height > $src_h) || ($height > $src_h && $width == 0) || ($width > $src_w && $height == 0)) {
|
|
|
- $proportion = 1;
|
|
|
- }
|
|
|
- if ($width > $src_w) {
|
|
|
- $dst_w = $width = $src_w;
|
|
|
- }
|
|
|
- if ($height > $src_h) {
|
|
|
- $dst_h = $height = $src_h;
|
|
|
- }
|
|
|
-
|
|
|
- if (!$width && !$height && !$proportion) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!$proportion) {
|
|
|
- if ($cut == 0) {
|
|
|
- if ($dst_w && $dst_h) {
|
|
|
- if ($dst_w / $src_w > $dst_h / $src_h) {
|
|
|
- $dst_w = $src_w * ($dst_h / $src_h);
|
|
|
- $x = 0 - ($dst_w - $width) / 2;
|
|
|
- } else {
|
|
|
- $dst_h = $src_h * ($dst_w / $src_w);
|
|
|
- $y = 0 - ($dst_h - $height) / 2;
|
|
|
- }
|
|
|
- } else if ($dst_w xor $dst_h) {
|
|
|
- if ($dst_w && !$dst_h) //有宽无高
|
|
|
- {
|
|
|
- $propor = $dst_w / $src_w;
|
|
|
- $height = $dst_h = $src_h * $propor;
|
|
|
- } else if (!$dst_w && $dst_h) //有高无宽
|
|
|
- {
|
|
|
- $propor = $dst_h / $src_h;
|
|
|
- $width = $dst_w = $src_w * $propor;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (!$dst_h) //裁剪时无高
|
|
|
- {
|
|
|
- $height = $dst_h = $dst_w;
|
|
|
- }
|
|
|
- if (!$dst_w) //裁剪时无宽
|
|
|
- {
|
|
|
- $width = $dst_w = $dst_h;
|
|
|
- }
|
|
|
- $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
|
|
|
- $dst_w = (int)round($src_w * $propor);
|
|
|
- $dst_h = (int)round($src_h * $propor);
|
|
|
- $x = ($width - $dst_w) / 2;
|
|
|
- $y = ($height - $dst_h) / 2;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $proportion = min($proportion, 1);
|
|
|
- $height = $dst_h = $src_h * $proportion;
|
|
|
- $width = $dst_w = $src_w * $proportion;
|
|
|
- }
|
|
|
-
|
|
|
- $src = $createfun($src_img);
|
|
|
- $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
|
|
|
- $white = imagecolorallocate($dst, 255, 255, 255);
|
|
|
- imagefill($dst, 0, 0, $white);
|
|
|
-
|
|
|
- if (function_exists('imagecopyresampled')) {
|
|
|
- imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
|
|
|
- } else {
|
|
|
- imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
|
|
|
- }
|
|
|
- $otfunc($dst, $dst_img);
|
|
|
- imagedestroy($dst);
|
|
|
- imagedestroy($src);
|
|
|
- //return true;
|
|
|
- return ['width' => $src_w, 'height' => $src_h];//modify sofashi
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 输出字符并结束运行
|
|
|
- */
|
|
|
- public static function stop($string = '')
|
|
|
- {
|
|
|
- echo $string;
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
-
|
|
|
- public static function end()
|
|
|
- {
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 输出json数据并结束运行
|
|
|
- */
|
|
|
- public static function encode($data)
|
|
|
- {
|
|
|
- echo Json::encode($data);
|
|
|
- exit();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 组合需要的格式,输出json数据并结束运行
|
|
|
- * code A0001 正确 其它如:A0002表示错误
|
|
|
- */
|
|
|
- public static function failInfo($msg = '操作失败', $code = 'A0002', $data = [])
|
|
|
- {
|
|
|
- $arr = ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
|
- self::encode($arr);
|
|
|
- }
|
|
|
-
|
|
|
- public static function successInfo($msg = '操作成功', $code = 'A0001', $data = [])
|
|
|
- {
|
|
|
- $arr = ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
|
- self::encode($arr);
|
|
|
- }
|
|
|
-
|
|
|
- //操作成功,只关心数据的返回
|
|
|
- public static function sendJson($data)
|
|
|
- {
|
|
|
- $arr = ['code' => 1, 'msg' => '操作成功', 'data' => $data];
|
|
|
- self::encode($arr);
|
|
|
- }
|
|
|
-
|
|
|
- //只关心返回失败状态
|
|
|
- public static function sendFail($msg)
|
|
|
- {
|
|
|
- //要输出页面
|
|
|
- if (isset(Yii::$app->params['exportStyle']) && Yii::$app->params['exportStyle'] == 'web') {
|
|
|
- //确认平台
|
|
|
- if (Yii::$app->params['appStyle'] == 'backend') {
|
|
|
- //跳转 echo '<script> window.location.href=""; </script>';
|
|
|
- }
|
|
|
- if (Yii::$app->params['appStyle'] == 'mobile') {
|
|
|
- //跳转 echo '<script> window.location.href=""; </script>';
|
|
|
- }
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
- $arr = ['code' => 0, 'msg' => $msg, 'data' => []];
|
|
|
- self::encode($arr);
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
-
|
|
|
- //只关心返回成功状态
|
|
|
- public static function sendSuccess()
|
|
|
- {
|
|
|
- $arr = ['code' => 1, 'msg' => '操作成功', 'data' => []];
|
|
|
- self::encode($arr);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 合并图片 ---lqh 2017-05-05
|
|
|
- * @param $background_img 背景原图
|
|
|
- * @param $inline_img 内嵌原图
|
|
|
- * @return mixed 合并图的路径名称
|
|
|
- */
|
|
|
- public static function mergeImg($background_img, $inline_img, $type = 'wx')
|
|
|
- {
|
|
|
- $app = Yii::getAlias("@app");
|
|
|
- $saveDir = $app . '/../images/qrcode/';
|
|
|
- $rand = stringUtil::buildOrderNo();
|
|
|
- $random = empty($unique) ? $rand : $unique . '_' . $rand;
|
|
|
- //背景框图
|
|
|
- $path_1 = $background_img;
|
|
|
- //二维码图片
|
|
|
- $path_2 = $inline_img;
|
|
|
-
|
|
|
- //将框和二维码图片分别取到两个画布中。 针对png格式
|
|
|
- $image_1 = imagecreatefrompng($path_1);
|
|
|
- $image_2 = imagecreatefrompng($path_2);
|
|
|
- //创建一个和框图片一样大小的真彩色画布(ps:只有这样才能保证后面copy二维码图片的时候不会失真)
|
|
|
- $image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
|
|
|
- //为真彩色画布创建白色背景,再设置为透明
|
|
|
- $color = imagecolorallocate($image_3, 255, 255, 255);
|
|
|
- imagefill($image_3, 0, 0, $color);
|
|
|
- imageColorTransparent($image_3, $color);
|
|
|
- //首先将框画布采样copy到真彩色画布中,不会失真
|
|
|
- imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
|
|
|
- //再将二维码图片copy到已经具有框图像的真彩色画布中,同样也不会失真
|
|
|
- //根据微信或则支付宝合并位置有所不同
|
|
|
- if ($type == 'wx') {
|
|
|
- imagecopymerge($image_3, $image_2, 220, 320, 0, 0, imagesx($image_2), imagesy($image_2), 100);
|
|
|
- } else {
|
|
|
- //支付宝
|
|
|
- imagecopymerge($image_3, $image_2, 450, 820, 0, 0, imagesx($image_2), imagesy($image_2), 100);
|
|
|
- }
|
|
|
-
|
|
|
- //将画布保存到指定的png文件
|
|
|
- imagepng($image_3, $saveDir . $random . '_merge.png');
|
|
|
- return '/qrcode/' . $random . '_merge.png';//组合图片
|
|
|
- }
|
|
|
-
|
|
|
- public static function mergeQrimg($backgroundImg, array $qrImg)
|
|
|
- {
|
|
|
- $app = Yii::getAlias("@app");
|
|
|
- $saveDir = $app . '/../images/qrcode/';
|
|
|
- $random = stringUtil::buildOrderNo();
|
|
|
-
|
|
|
- $dest = imagecreatefromjpeg($backgroundImg);
|
|
|
-
|
|
|
- // Get new sizes
|
|
|
- list($width, $height) = getimagesize($qrImg['wifi']);
|
|
|
- list($newwidth, $newheight) = getimagesize($qrImg['zhifubao']);
|
|
|
- $newwidth = $newwidth + 35;//下载的wifi二维码size太小,放大些
|
|
|
- $newheight = $newheight + 35;
|
|
|
-
|
|
|
- // Load
|
|
|
- $thumb = imagecreatetruecolor($newwidth, $newheight);
|
|
|
- $source = imagecreatefromjpeg($qrImg['wifi']);
|
|
|
-
|
|
|
- // Resize
|
|
|
- imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
|
|
|
- imagejpeg($thumb, $saveDir . "temp.jpeg");//Output image to file
|
|
|
-
|
|
|
- //微信(支付宝)二维码、WiFi二维码
|
|
|
- $zfbQrImg = imagecreatefrompng($qrImg['zhifubao']);
|
|
|
- $WXQrImg = imagecreatefrompng($qrImg['weixin']);
|
|
|
- $wifiQrImg = imagecreatefromjpeg($saveDir . "temp.jpeg");
|
|
|
-
|
|
|
- imagealphablending($dest, false);
|
|
|
- imagesavealpha($dest, true);
|
|
|
- imagecopymerge($dest, $zfbQrImg, 240, 538, 0, 0, imagesx($zfbQrImg), imagesy($zfbQrImg), 100);
|
|
|
- imagecopymerge($dest, $WXQrImg, 985, 538, 0, 0, imagesx($WXQrImg), imagesy($WXQrImg), 100);
|
|
|
- imagecopymerge($dest, $wifiQrImg, 1708, 520, 0, 0, imagesx($wifiQrImg), imagesy($wifiQrImg), 100);
|
|
|
-
|
|
|
- imagepng($dest, $saveDir . $random . '_fullQrImg.png');
|
|
|
- imagedestroy($dest);
|
|
|
- imagedestroy($zfbQrImg);
|
|
|
- imagedestroy($WXQrImg);
|
|
|
- imagedestroy($wifiQrImg);
|
|
|
-
|
|
|
- return '/qrcode/' . $random . '_fullQrImg.png';//组合图片
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- *功能:php完美实现下载远程图片保存到本地
|
|
|
- *参数:文件url,保存文件目录,保存文件名称,使用的下载方式
|
|
|
- *当保存文件名称为空时则使用远程文件原来的名称
|
|
|
- */
|
|
|
- function downLoadImage($url, $save_dir = '', $filename = '', $type = 0)
|
|
|
- {
|
|
|
- if (trim($url) == '') {
|
|
|
- return array('file_name' => '', 'save_path' => '', 'error' => 1);
|
|
|
- }
|
|
|
- if (trim($save_dir) == '') {
|
|
|
- $save_dir = './';
|
|
|
- }
|
|
|
- if (trim($filename) == '') {//保存文件名
|
|
|
- $ext = strrchr($url, '.');
|
|
|
- if ($ext != '.gif' && $ext != '.jpg') {
|
|
|
- return array('file_name' => '', 'save_path' => '', 'error' => 3);
|
|
|
- }
|
|
|
- $filename = time() . $ext;
|
|
|
- }
|
|
|
- if (0 !== strrpos($save_dir, '/')) {
|
|
|
- $save_dir .= '/';
|
|
|
- }
|
|
|
- //创建保存目录
|
|
|
- if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
|
|
|
- return array('file_name' => '', 'save_path' => '', 'error' => 5);
|
|
|
- }
|
|
|
- //获取远程文件所采用的方法
|
|
|
- if ($type) {
|
|
|
- $ch = curl_init();
|
|
|
- $timeout = 5;
|
|
|
- curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
|
- $img = curl_exec($ch);
|
|
|
- curl_close($ch);
|
|
|
- } else {
|
|
|
- ob_start();
|
|
|
- readfile($url);
|
|
|
- $img = ob_get_contents();
|
|
|
- ob_end_clean();
|
|
|
- }
|
|
|
- //$size=strlen($img);
|
|
|
- //文件大小
|
|
|
- $fp2 = @fopen($save_dir . $filename, 'a');
|
|
|
- fwrite($fp2, $img);
|
|
|
- fclose($fp2);
|
|
|
- unset($img, $url);
|
|
|
- return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);
|
|
|
- }
|
|
|
-
|
|
|
- //获取服务器IP
|
|
|
- public static function serverIp()
|
|
|
- {
|
|
|
- if (isset($_SERVER)) {
|
|
|
- if ($_SERVER['SERVER_ADDR']) {
|
|
|
- $server_ip = $_SERVER['SERVER_ADDR'];
|
|
|
- } else {
|
|
|
- $server_ip = $_SERVER['LOCAL_ADDR'];
|
|
|
- }
|
|
|
- } else {
|
|
|
- $server_ip = getenv('SERVER_ADDR');
|
|
|
- }
|
|
|
- return $server_ip;
|
|
|
- }
|
|
|
-
|
|
|
- //获取huahuibao根目录 shish 2020.3.11
|
|
|
- public static function getRootDir()
|
|
|
- {
|
|
|
- $basePath = Yii::$app->basePath;
|
|
|
- if (Yii::$app->id == 'app-console') {
|
|
|
- $root = $basePath . "/../";
|
|
|
- } else {
|
|
|
- $root = $basePath . "/../../";
|
|
|
- }
|
|
|
- return $root;
|
|
|
- }
|
|
|
-
|
|
|
- //格式化输出;用于调试时使用 shish 2019.8.20
|
|
|
- public static function print_r($data)
|
|
|
- {
|
|
|
- echo "<pre>";
|
|
|
- print_r($data);
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
-
|
|
|
- //错误输出
|
|
|
- public static function fail($msg = '操作失败')
|
|
|
- {
|
|
|
- Yii::info($msg);
|
|
|
- self::encode(['code' => 0, 'msg' => $msg, 'data' => []]);
|
|
|
- }
|
|
|
-
|
|
|
- //成功输出内容
|
|
|
- public static function success($data, $msg = '操作成功')
|
|
|
- {
|
|
|
- self::encode(['code' => 1, 'msg' => $msg, 'data' => $data]);
|
|
|
- }
|
|
|
-
|
|
|
- //退出当前登陆 shish 2020.3.8
|
|
|
- public static function logout()
|
|
|
- {
|
|
|
- self::encode(['code' => 2, 'msg' => '当前登陆信息已失效,请重新登陆', 'data' => []]);
|
|
|
- }
|
|
|
-
|
|
|
- //只输出执行完成状态 shish 2020.3.11
|
|
|
- public static function complete($msg = '')
|
|
|
- {
|
|
|
- $msg = empty($msg) ? '操作成功' : $msg;
|
|
|
- self::encode(['code' => 1, 'msg' => $msg, 'data' => []]);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static function notLogin()
|
|
|
- {
|
|
|
- self::encode(['code' => -1, 'msg' => '没有登陆', 'data' => []]);
|
|
|
- }
|
|
|
-
|
|
|
- //shisq
|
|
|
- public static function echoStatus($num, $key, $status = 'status')
|
|
|
- {
|
|
|
- $dictionary = \common\components\configDict::$dict[$key][$status];
|
|
|
- echo $dictionary[$num];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 二维数组根据字段进行排序 shisq
|
|
|
- * @params array $array 需要排序的数组
|
|
|
- * @params string $field 排序的字段
|
|
|
- * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
|
|
|
- */
|
|
|
- public static function arraySequence($array, $field, $sort = 'SORT_DESC')
|
|
|
- {
|
|
|
- $arrSort = array();
|
|
|
- foreach ($array as $uniqid => $row) {
|
|
|
- foreach ($row as $key => $value) {
|
|
|
- $arrSort[$key][$uniqid] = $value;
|
|
|
- }
|
|
|
- }
|
|
|
- array_multisort($arrSort[$field], constant($sort), $array);
|
|
|
- return $array;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ //判断是否微信浏览器
|
|
|
+ public static function isWx()
|
|
|
+ {
|
|
|
+ if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据两点间的经纬度计算距离
|
|
|
+ public static function getDistance($lat1, $lng1, $lat2, $lng2)
|
|
|
+ {
|
|
|
+ $earthRadius = 6367000; //approximate radius of earth in meters
|
|
|
+
|
|
|
+ /*
|
|
|
+ Convert these degrees to radians
|
|
|
+ to work with the formula
|
|
|
+ */
|
|
|
+
|
|
|
+ $lat1 = ($lat1 * pi()) / 180;
|
|
|
+ $lng1 = ($lng1 * pi()) / 180;
|
|
|
+
|
|
|
+ $lat2 = ($lat2 * pi()) / 180;
|
|
|
+ $lng2 = ($lng2 * pi()) / 180;
|
|
|
+
|
|
|
+ /*
|
|
|
+ Using the
|
|
|
+ Haversine formula
|
|
|
+
|
|
|
+ http://en.wikipedia.org/wiki/Haversine_formula
|
|
|
+
|
|
|
+ calculate the distance
|
|
|
+ */
|
|
|
+
|
|
|
+ $calcLongitude = $lng2 - $lng1;
|
|
|
+ $calcLatitude = $lat2 - $lat1;
|
|
|
+ $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
|
|
|
+ $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
|
|
|
+ $calculatedDistance = $earthRadius * $stepTwo;
|
|
|
+
|
|
|
+ return round($calculatedDistance);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成缩略图
|
|
|
+ * @author yangzhiguo0903@163.com
|
|
|
+ * @param string 源图绝对完整地址{带文件名及后缀名}
|
|
|
+ * @param string 目标图绝对完整地址{带文件名及后缀名}
|
|
|
+ * @param int 缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
|
|
|
+ * @param int 缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
|
|
|
+ * @param int 是否裁切{宽,高必须非0}
|
|
|
+ * @param int /float 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
|
|
|
+ {
|
|
|
+ if (!is_file($src_img)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $ot = pathinfo($dst_img, PATHINFO_EXTENSION);
|
|
|
+ $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
|
|
|
+ $srcinfo = getimagesize($src_img);
|
|
|
+ $src_w = $srcinfo[0];
|
|
|
+ $src_h = $srcinfo[1];
|
|
|
+ $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
|
|
|
+ $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
|
|
|
+
|
|
|
+ $dst_h = $height;
|
|
|
+ $dst_w = $width;
|
|
|
+ $x = $y = 0;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缩略图不超过源图尺寸(前提是宽或高只有一个)
|
|
|
+ */
|
|
|
+ if (($width > $src_w && $height > $src_h) || ($height > $src_h && $width == 0) || ($width > $src_w && $height == 0)) {
|
|
|
+ $proportion = 1;
|
|
|
+ }
|
|
|
+ if ($width > $src_w) {
|
|
|
+ $dst_w = $width = $src_w;
|
|
|
+ }
|
|
|
+ if ($height > $src_h) {
|
|
|
+ $dst_h = $height = $src_h;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$width && !$height && !$proportion) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!$proportion) {
|
|
|
+ if ($cut == 0) {
|
|
|
+ if ($dst_w && $dst_h) {
|
|
|
+ if ($dst_w / $src_w > $dst_h / $src_h) {
|
|
|
+ $dst_w = $src_w * ($dst_h / $src_h);
|
|
|
+ $x = 0 - ($dst_w - $width) / 2;
|
|
|
+ } else {
|
|
|
+ $dst_h = $src_h * ($dst_w / $src_w);
|
|
|
+ $y = 0 - ($dst_h - $height) / 2;
|
|
|
+ }
|
|
|
+ } else if ($dst_w xor $dst_h) {
|
|
|
+ if ($dst_w && !$dst_h) //有宽无高
|
|
|
+ {
|
|
|
+ $propor = $dst_w / $src_w;
|
|
|
+ $height = $dst_h = $src_h * $propor;
|
|
|
+ } else if (!$dst_w && $dst_h) //有高无宽
|
|
|
+ {
|
|
|
+ $propor = $dst_h / $src_h;
|
|
|
+ $width = $dst_w = $src_w * $propor;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!$dst_h) //裁剪时无高
|
|
|
+ {
|
|
|
+ $height = $dst_h = $dst_w;
|
|
|
+ }
|
|
|
+ if (!$dst_w) //裁剪时无宽
|
|
|
+ {
|
|
|
+ $width = $dst_w = $dst_h;
|
|
|
+ }
|
|
|
+ $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
|
|
|
+ $dst_w = (int)round($src_w * $propor);
|
|
|
+ $dst_h = (int)round($src_h * $propor);
|
|
|
+ $x = ($width - $dst_w) / 2;
|
|
|
+ $y = ($height - $dst_h) / 2;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $proportion = min($proportion, 1);
|
|
|
+ $height = $dst_h = $src_h * $proportion;
|
|
|
+ $width = $dst_w = $src_w * $proportion;
|
|
|
+ }
|
|
|
+
|
|
|
+ $src = $createfun($src_img);
|
|
|
+ $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
|
|
|
+ $white = imagecolorallocate($dst, 255, 255, 255);
|
|
|
+ imagefill($dst, 0, 0, $white);
|
|
|
+
|
|
|
+ if (function_exists('imagecopyresampled')) {
|
|
|
+ imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
|
|
|
+ } else {
|
|
|
+ imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
|
|
|
+ }
|
|
|
+ $otfunc($dst, $dst_img);
|
|
|
+ imagedestroy($dst);
|
|
|
+ imagedestroy($src);
|
|
|
+ //return true;
|
|
|
+ return ['width' => $src_w, 'height' => $src_h];//modify sofashi
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出字符并结束运行
|
|
|
+ */
|
|
|
+ public static function stop($string = '')
|
|
|
+ {
|
|
|
+ echo $string;
|
|
|
+ Yii::$app->end();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function end()
|
|
|
+ {
|
|
|
+ Yii::$app->end();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出json数据并结束运行
|
|
|
+ */
|
|
|
+ public static function encode($data)
|
|
|
+ {
|
|
|
+ echo Json::encode($data);
|
|
|
+ exit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组合需要的格式,输出json数据并结束运行
|
|
|
+ * code A0001 正确 其它如:A0002表示错误
|
|
|
+ */
|
|
|
+ public static function failInfo($msg = '操作失败', $code = 'A0002', $data = [])
|
|
|
+ {
|
|
|
+ $arr = ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
|
+ self::encode($arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function successInfo($msg = '操作成功', $code = 'A0001', $data = [])
|
|
|
+ {
|
|
|
+ $arr = ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
|
+ self::encode($arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //操作成功,只关心数据的返回
|
|
|
+ public static function sendJson($data)
|
|
|
+ {
|
|
|
+ $arr = ['code' => 1, 'msg' => '操作成功', 'data' => $data];
|
|
|
+ self::encode($arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //只关心返回失败状态
|
|
|
+ public static function sendFail($msg)
|
|
|
+ {
|
|
|
+ //要输出页面
|
|
|
+ if (isset(Yii::$app->params['exportStyle']) && Yii::$app->params['exportStyle'] == 'web') {
|
|
|
+ //确认平台
|
|
|
+ if (Yii::$app->params['appStyle'] == 'backend') {
|
|
|
+ //跳转 echo '<script> window.location.href=""; </script>';
|
|
|
+ }
|
|
|
+ if (Yii::$app->params['appStyle'] == 'mobile') {
|
|
|
+ //跳转 echo '<script> window.location.href=""; </script>';
|
|
|
+ }
|
|
|
+ Yii::$app->end();
|
|
|
+ }
|
|
|
+ $arr = ['code' => 0, 'msg' => $msg, 'data' => []];
|
|
|
+ self::encode($arr);
|
|
|
+ Yii::$app->end();
|
|
|
+ }
|
|
|
+
|
|
|
+ //只关心返回成功状态
|
|
|
+ public static function sendSuccess()
|
|
|
+ {
|
|
|
+ $arr = ['code' => 1, 'msg' => '操作成功', 'data' => []];
|
|
|
+ self::encode($arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并图片 ---lqh 2017-05-05
|
|
|
+ * @param $background_img 背景原图
|
|
|
+ * @param $inline_img 内嵌原图
|
|
|
+ * @return mixed 合并图的路径名称
|
|
|
+ */
|
|
|
+ public static function mergeImg($background_img, $inline_img, $type = 'wx')
|
|
|
+ {
|
|
|
+ $app = Yii::getAlias("@app");
|
|
|
+ $saveDir = $app . '/../images/qrcode/';
|
|
|
+ $rand = stringUtil::buildOrderNo();
|
|
|
+ $random = empty($unique) ? $rand : $unique . '_' . $rand;
|
|
|
+ //背景框图
|
|
|
+ $path_1 = $background_img;
|
|
|
+ //二维码图片
|
|
|
+ $path_2 = $inline_img;
|
|
|
+
|
|
|
+ //将框和二维码图片分别取到两个画布中。 针对png格式
|
|
|
+ $image_1 = imagecreatefrompng($path_1);
|
|
|
+ $image_2 = imagecreatefrompng($path_2);
|
|
|
+ //创建一个和框图片一样大小的真彩色画布(ps:只有这样才能保证后面copy二维码图片的时候不会失真)
|
|
|
+ $image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
|
|
|
+ //为真彩色画布创建白色背景,再设置为透明
|
|
|
+ $color = imagecolorallocate($image_3, 255, 255, 255);
|
|
|
+ imagefill($image_3, 0, 0, $color);
|
|
|
+ imageColorTransparent($image_3, $color);
|
|
|
+ //首先将框画布采样copy到真彩色画布中,不会失真
|
|
|
+ imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
|
|
|
+ //再将二维码图片copy到已经具有框图像的真彩色画布中,同样也不会失真
|
|
|
+ //根据微信或则支付宝合并位置有所不同
|
|
|
+ if ($type == 'wx') {
|
|
|
+ imagecopymerge($image_3, $image_2, 220, 320, 0, 0, imagesx($image_2), imagesy($image_2), 100);
|
|
|
+ } else {
|
|
|
+ //支付宝
|
|
|
+ imagecopymerge($image_3, $image_2, 450, 820, 0, 0, imagesx($image_2), imagesy($image_2), 100);
|
|
|
+ }
|
|
|
+
|
|
|
+ //将画布保存到指定的png文件
|
|
|
+ imagepng($image_3, $saveDir . $random . '_merge.png');
|
|
|
+ return '/qrcode/' . $random . '_merge.png';//组合图片
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function mergeQrimg($backgroundImg, array $qrImg)
|
|
|
+ {
|
|
|
+ $app = Yii::getAlias("@app");
|
|
|
+ $saveDir = $app . '/../images/qrcode/';
|
|
|
+ $random = stringUtil::buildOrderNo();
|
|
|
+
|
|
|
+ $dest = imagecreatefromjpeg($backgroundImg);
|
|
|
+
|
|
|
+ // Get new sizes
|
|
|
+ list($width, $height) = getimagesize($qrImg['wifi']);
|
|
|
+ list($newwidth, $newheight) = getimagesize($qrImg['zhifubao']);
|
|
|
+ $newwidth = $newwidth + 35;//下载的wifi二维码size太小,放大些
|
|
|
+ $newheight = $newheight + 35;
|
|
|
+
|
|
|
+ // Load
|
|
|
+ $thumb = imagecreatetruecolor($newwidth, $newheight);
|
|
|
+ $source = imagecreatefromjpeg($qrImg['wifi']);
|
|
|
+
|
|
|
+ // Resize
|
|
|
+ imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
|
|
|
+ imagejpeg($thumb, $saveDir . "temp.jpeg");//Output image to file
|
|
|
+
|
|
|
+ //微信(支付宝)二维码、WiFi二维码
|
|
|
+ $zfbQrImg = imagecreatefrompng($qrImg['zhifubao']);
|
|
|
+ $WXQrImg = imagecreatefrompng($qrImg['weixin']);
|
|
|
+ $wifiQrImg = imagecreatefromjpeg($saveDir . "temp.jpeg");
|
|
|
+
|
|
|
+ imagealphablending($dest, false);
|
|
|
+ imagesavealpha($dest, true);
|
|
|
+ imagecopymerge($dest, $zfbQrImg, 240, 538, 0, 0, imagesx($zfbQrImg), imagesy($zfbQrImg), 100);
|
|
|
+ imagecopymerge($dest, $WXQrImg, 985, 538, 0, 0, imagesx($WXQrImg), imagesy($WXQrImg), 100);
|
|
|
+ imagecopymerge($dest, $wifiQrImg, 1708, 520, 0, 0, imagesx($wifiQrImg), imagesy($wifiQrImg), 100);
|
|
|
+
|
|
|
+ imagepng($dest, $saveDir . $random . '_fullQrImg.png');
|
|
|
+ imagedestroy($dest);
|
|
|
+ imagedestroy($zfbQrImg);
|
|
|
+ imagedestroy($WXQrImg);
|
|
|
+ imagedestroy($wifiQrImg);
|
|
|
+
|
|
|
+ return '/qrcode/' . $random . '_fullQrImg.png';//组合图片
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ *功能:php完美实现下载远程图片保存到本地
|
|
|
+ *参数:文件url,保存文件目录,保存文件名称,使用的下载方式
|
|
|
+ *当保存文件名称为空时则使用远程文件原来的名称
|
|
|
+ */
|
|
|
+ function downLoadImage($url, $save_dir = '', $filename = '', $type = 0)
|
|
|
+ {
|
|
|
+ if (trim($url) == '') {
|
|
|
+ return array('file_name' => '', 'save_path' => '', 'error' => 1);
|
|
|
+ }
|
|
|
+ if (trim($save_dir) == '') {
|
|
|
+ $save_dir = './';
|
|
|
+ }
|
|
|
+ if (trim($filename) == '') {//保存文件名
|
|
|
+ $ext = strrchr($url, '.');
|
|
|
+ if ($ext != '.gif' && $ext != '.jpg') {
|
|
|
+ return array('file_name' => '', 'save_path' => '', 'error' => 3);
|
|
|
+ }
|
|
|
+ $filename = time() . $ext;
|
|
|
+ }
|
|
|
+ if (0 !== strrpos($save_dir, '/')) {
|
|
|
+ $save_dir .= '/';
|
|
|
+ }
|
|
|
+ //创建保存目录
|
|
|
+ if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
|
|
|
+ return array('file_name' => '', 'save_path' => '', 'error' => 5);
|
|
|
+ }
|
|
|
+ //获取远程文件所采用的方法
|
|
|
+ if ($type) {
|
|
|
+ $ch = curl_init();
|
|
|
+ $timeout = 5;
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
|
+ $img = curl_exec($ch);
|
|
|
+ curl_close($ch);
|
|
|
+ } else {
|
|
|
+ ob_start();
|
|
|
+ readfile($url);
|
|
|
+ $img = ob_get_contents();
|
|
|
+ ob_end_clean();
|
|
|
+ }
|
|
|
+ //$size=strlen($img);
|
|
|
+ //文件大小
|
|
|
+ $fp2 = @fopen($save_dir . $filename, 'a');
|
|
|
+ fwrite($fp2, $img);
|
|
|
+ fclose($fp2);
|
|
|
+ unset($img, $url);
|
|
|
+ return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取服务器IP
|
|
|
+ public static function serverIp()
|
|
|
+ {
|
|
|
+ if (isset($_SERVER)) {
|
|
|
+ if ($_SERVER['SERVER_ADDR']) {
|
|
|
+ $server_ip = $_SERVER['SERVER_ADDR'];
|
|
|
+ } else {
|
|
|
+ $server_ip = $_SERVER['LOCAL_ADDR'];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $server_ip = getenv('SERVER_ADDR');
|
|
|
+ }
|
|
|
+ return $server_ip;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取huahuibao根目录 shish 2020.3.11
|
|
|
+ public static function getRootDir()
|
|
|
+ {
|
|
|
+ $basePath = Yii::$app->basePath;
|
|
|
+ if (Yii::$app->id == 'app-console') {
|
|
|
+ $root = $basePath . "/../";
|
|
|
+ } else {
|
|
|
+ $root = $basePath . "/../../";
|
|
|
+ }
|
|
|
+ return $root;
|
|
|
+ }
|
|
|
+
|
|
|
+ //格式化输出;用于调试时使用 shish 2019.8.20
|
|
|
+ public static function print_r($data)
|
|
|
+ {
|
|
|
+ echo "<pre>";
|
|
|
+ print_r($data);
|
|
|
+ Yii::$app->end();
|
|
|
+ }
|
|
|
+
|
|
|
+ //错误输出
|
|
|
+ public static function fail($msg = '操作失败')
|
|
|
+ {
|
|
|
+ Yii::info($msg);
|
|
|
+ self::encode(['code' => 0, 'msg' => $msg, 'data' => []]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //成功输出内容
|
|
|
+ public static function success($data, $msg = '操作成功')
|
|
|
+ {
|
|
|
+ self::encode(['code' => 1, 'msg' => $msg, 'data' => $data]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //退出当前登陆 shish 2020.3.8
|
|
|
+ public static function logout()
|
|
|
+ {
|
|
|
+ self::encode(['code' => 2, 'msg' => '当前登陆信息已失效,请重新登陆', 'data' => []]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //只输出执行完成状态 shish 2020.3.11
|
|
|
+ public static function complete($msg = '')
|
|
|
+ {
|
|
|
+ $msg = empty($msg) ? '操作成功' : $msg;
|
|
|
+ self::encode(['code' => 1, 'msg' => $msg, 'data' => []]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static function notLogin($msg = '没有登陆')
|
|
|
+ {
|
|
|
+ self::encode(['code' => -1, 'msg' => $msg, 'data' => []]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //shisq
|
|
|
+ public static function echoStatus($num, $key, $status = 'status')
|
|
|
+ {
|
|
|
+ $dictionary = \common\components\configDict::$dict[$key][$status];
|
|
|
+ echo $dictionary[$num];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 二维数组根据字段进行排序 shisq
|
|
|
+ * @params array $array 需要排序的数组
|
|
|
+ * @params string $field 排序的字段
|
|
|
+ * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
|
|
|
+ */
|
|
|
+ public static function arraySequence($array, $field, $sort = 'SORT_DESC')
|
|
|
+ {
|
|
|
+ $arrSort = array();
|
|
|
+ foreach ($array as $uniqid => $row) {
|
|
|
+ foreach ($row as $key => $value) {
|
|
|
+ $arrSort[$key][$uniqid] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ array_multisort($arrSort[$field], constant($sort), $array);
|
|
|
+ return $array;
|
|
|
+ }
|
|
|
+
|
|
|
}
|