导航:首页 > 文件处理 > php压缩json

php压缩json

发布时间:2022-06-26 02:43:41

1. php怎么把数组转换成json格式化

可以使用json_encode完成转换,

范例:

<?php
$arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echojson_encode($arr);
?>

以上例程会输出:

{"a":1,"b":2,"c":3,"d":4,"e":5}

2. PHP能压缩网络上的图片吗

PHP图片上传并压缩的实现方法具体内容如下使用到三个文件
connect.php:连接数据库
test_upload.php:执行SQL语句
upload_img.php:上传图片并压缩
三个文件代码如下:
连接数据库:connect.php
<?php
$db_host = '';
$db_user = '';
$db_psw = '';
$db_name = '';
$db_port = '';
$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);$q="set names utf8;";
$result=$sqlconn->query($q);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());exit();
}
?>
执行SQL语句:test_upload.php
<?php
require ("connect.php");
require ("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql = "insert into img (real_img,small_img) values (?,?)";$result = $sqlconn -> prepare($insert_sql);$result -> bind_param("ss", $real_img,$small_img);$result -> execute();
?>
上传图片并压缩:upload_img.php
<?php
//设置文件保存目录
$uploaddir = "upfiles/";
//设置允许上传文件的类型
$type=array("jpg","gif","bmp","jpeg","png");//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);}
//生成随机文件名函数
function random($length)
{
$hash = 'CR-';
$chars = '';$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES['filename']['name']));//判断文件类型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)){
$text=implode(",",$type);
$ret_code=3;//文件类型错误
$page_result=$text;
$retArray = array('ret_code' => $ret_code,'page_result'=>$page_result);$retJson = json_encode($retArray);
echo $retJson;
return;
}
//生成目标文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);do
{
$filename[0]=random(10); //设置随机数长度$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)){
if(is_uploaded_file($_FILES['filename']['tmp_name'])){
$ret_code=1;//上传失败
}
else
{//上传成功
$ret_code=0;
}
}
$retArray = array('ret_code' => $ret_code);$retJson = json_encode($retArray);
echo $retJson;
}
//压缩图片
$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;//$pic_width_max=120;
//$pic_height_max=90;
//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩$file_type=$_FILES["filename"]['type'];
function ResizeImage($uploadfile,$maxwidth,$maxheight,$name){
//取得当前图片大小
$width = imagesx($uploadfile);
$height = imagesy($uploadfile);
$i=0.5;
//生成缩略图的大小
if(($width > $maxwidth) || ($height > $maxheight)){
/*
$widthratio = $maxwidth/$width;
$heightratio = $maxheight/$height;
if($widthratio < $heightratio)
{
$ratio = $widthratio;
}
else
{
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
*/
$newwidth = $width * $i;
$newheight = $height * $i;
if(function_exists("imageresampled")){
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);imageresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);imageresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}
ImageJpeg ($uploaddir_resize,$name);
ImageDestroy ($uploaddir_resize);
}
else
{
ImageJpeg ($uploadfile,$name);
}
}
if($_FILES["filename"]['size'])
{
if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg"){
//$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == "image/x-png")
{
//$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == "image/gif")
{
//$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
else//默认jpg
{
$im = imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);ImageDestroy ($im);
}
}
?>
请按照现实情况更改connect.php,test_upload.php中对应的信息

3. php解析json数据

<?php
$json = '{"APPCount": 2,"data": [
{
"originalID": "991",
"APPName": "优酷",
"APPType": "APK",
"category": "视频",
"versionName": "3.8",
"versionCode": "5",
"packageName ": "cn.dsp.youku",
" APPStatus": "待审核",
"testReportURL": "",
"downloadCount": 0
},
{
"originalID": "992",
"APPName": "优酷",
"APPType": "APK",
"category": "视频",
"versionName": "3.8",
"versionCode": "5",
"packageName ": "cn.dsp.youku",
" APPStatus": "安全测评未通过",
"testReportURL": "http: //192.168.0.106: /SecurityTest/sdsd.pdf",
"downloadCount": 0
}
]
}';

$decode = json_decode($json,true);
echo $decode['data'][1]['testReportURL'];

4. php压缩js minify实现后页面中怎么用

用ajax就可以了,点击加载时候,触发事件,发送ajax请求到php,php将数据发送回ajax回调函数里面,然后用js追加到你页面的后面就可以了$.ajax({type:"post",采用post参数url:"/index.php?m=member&c=index&a=public_up",//请求的php函数data:{id:$rid,table:$table},//传递的参数dataType:'json',//返回值以json格式返回success:function(data){//回调函数,在这里将data里面的数据追加到页面就可以了}});

5. 如何将php的变量值转换为json值

当php变量为数组的时候,通常会需要转化为json数据格式。使用内置php内置函数json_encode()来完成转化。转化完成后一般会遇到另一个头疼的问题,就是中文转化成json格式后会出现乱码的情况,针对这种情况,通常会先经过urlencode处理然后再经过urldecode进行反处理。这样处理后就可以解决乱码的问题了。

6. PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

三个文件代码如下:
连接数据库:connect.php

<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",mysqli_connect_error());
exit();
}
?>

当然使用一些封装的数据库类也是可以的。

执行SQL语句:test_upload.php

<?php
require("connect.php");
require("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql="insertintoimg(real_img,small_img)values(?,?)";
$result=$sqlconn->prepare($insert_sql);
$result->bind_param("ss",$real_img,$small_img);
$result->execute();
?>

上传图片并压缩:upload_img.php

<?php
//设置文件保存目录
$uploaddir="upfiles/";
//设置允许上传文件的类型
$type=array("jpg","gif","bmp","jpeg","png");

//获取文件后缀名函数
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}

//生成随机文件名函数
functionrandom($length)
{
$hash='CR-';
$chars='';
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i=0;$i<$length;$i++)
{
$hash.=$chars[mt_rand(0,$max)];
}
return$hash;
}

$a=strtolower(fileext($_FILES['filename']['name']));

//判断文件类型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))
{
$text=implode(",",$type);
$ret_code=3;//文件类型错误
$page_result=$text;
$retArray=array('ret_code'=>$ret_code,'page_result'=>$page_result);
$retJson=json_encode($retArray);
echo$retJson;
return;
}

//生成目标文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);
do
{
$filename[0]=random(10);//设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}

while(file_exists($uploadfile));

if(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['filename']['tmp_name']))
{
$ret_code=1;//上传失败
}
else
{//上传成功
$ret_code=0;
}
}
$retArray=array('ret_code'=>$ret_code);
$retJson=json_encode($retArray);
echo$retJson;
}

//压缩图片

$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;

//$pic_width_max=120;
//$pic_height_max=90;
//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

$file_type=$_FILES["filename"]['type'];

functionResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得当前图片大小
$width=imagesx($uploadfile);
$height=imagesy($uploadfile);
$i=0.5;
//生成缩略图的大小
if(($width>$maxwidth)||($height>$maxheight))
{
/*
$widthratio=$maxwidth/$width;
$heightratio=$maxheight/$height;

if($widthratio<$heightratio)
{
$ratio=$widthratio;
}
else
{
$ratio=$heightratio;
}

$newwidth=$width*$ratio;
$newheight=$height*$ratio;
*/
$newwidth=$width*$i;
$newheight=$height*$i;
if(function_exists("imageresampled"))
{
$uploaddir_resize=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}

ImageJpeg($uploaddir_resize,$name);
ImageDestroy($uploaddir_resize);
}
else
{
ImageJpeg($uploadfile,$name);
}
}if($_FILES["filename"]['size'])
{
if($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")
{
//$im=imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/x-png")
{
//$im=imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/gif")
{
//$im=imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
else//默认jpg
{
$im=imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

ImageDestroy($im);
}
}
?>

请按照现实情况更改connect.php,test_upload.php中对应的信息。

望采纳,谢谢。

7. 请教Nginx 中对 application/json 格式进行 gzip 压缩无效的问题

用nginx+php集成包phpfind或phpstudy。纯中文绿色解压即可,你试试吧,超级简单自动配好nginx+php环境

阅读全文

与php压缩json相关的资料

热点内容
看电视剧的免费网站下载 浏览:529
直接看的网站懂得都懂 浏览:104
夸克解压在哪个位置 浏览:522
在阿里云上开发远程服务器 浏览:570
有个人叫丧清的电影 浏览:730
androidmysql驱动 浏览:687
偷袭珍珠港国语版全部 浏览:611
美国一个电影叫什么汉 浏览:673
叔嫂外遇电影 浏览:736
露点外国电影 浏览:197
镇江服务器做棋牌游戏怎么样 浏览:855
uni小游戏源码 浏览:116
母乳在线母乳中出 浏览:783
鸿蒙为什么没有安卓彩蛋 浏览:997
可乐老师创意编程 浏览:28
七日杀如何设置专用服务器 浏览:28
主机怎么打开加密文件 浏览:19
重生收母系统小说 浏览:691
韩国电影静华 浏览:415
女的参加女儿运动会,下体塞了性爱玩具的电影 浏览:249