$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); Yii::$app->end(); } /** * 组合需要的格式,输出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 ''; } if (Yii::$app->params['appStyle'] == 'mobile') { //跳转 echo ''; } 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 * @return array|false|string */ 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; } /** * 是否移动端 */ public static function isMobileEnd() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) { return true; } // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset($_SERVER['HTTP_VIA'])) { // 找不到为flase,否则为true return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信 if (isset($_SERVER['HTTP_USER_AGENT'])) { $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'MicroMessenger'); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { return true; } } // 协议法,因为有可能不准确,放到最后判断 if (isset ($_SERVER['HTTP_ACCEPT'])) { // 如果只支持wml并且不支持html那一定是移动设备 // 如果支持wml和html但是wml在html之前则是移动设备 if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) { return true; } } return false; } /** * 获取huahuibao根目录 * @author shish * @time 2019.5.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 "
";
		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]);
	}
	
	//只输出成功状态
	public static function ok()
	{
		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;
	}
	
}