導航:首頁 > 文件處理 > 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相關的資料

熱點內容
澳門電影免費觀看網站大全 瀏覽:241
電腦多組命令 瀏覽:806
abkdb編譯 瀏覽:710
尺度計演算法大全 瀏覽:926
單片機開發板的作用 瀏覽:331
唯美愛情動作電影在線觀看 瀏覽:574
老電影農村片 瀏覽:303
netbeansclinux 瀏覽:181
不可能的世界小說免費閱讀 瀏覽:272
法國啄木鳥絲襪電影 瀏覽:307
動作片愛情在線免費觀看 瀏覽:1002
騰飛投資理財分紅源碼 瀏覽:854
windows打開埠命令 瀏覽:93
php獲取數組第一個元素key 瀏覽:488
重生二戰德國元首希特勒 瀏覽:135
被迫成為言情文的炮灰男小三 瀏覽:646
風月片在線觀看視頻 瀏覽:427
如何更新搶修app 瀏覽:711
aqdya愛情網 瀏覽:743
韓國倫理電影正宇 瀏覽:887