php壓縮代碼(php gzip壓縮)
1、php將文件夾打包成zip文件,參考代碼如下function addFileToZip$path,$zip $handler=opendir$path 打開(kāi)當(dāng)前文件夾由$path指定 while$filename=readdir$handler!==false if$filename !=;要用PHP壓縮解壓文件,常用的方法是調(diào)用命令行去執(zhí)行解壓縮操作 可以用exec system等函數(shù)調(diào)用shell命令 Linux下解壓縮命令是tar cxtzjvfpPN 文件與目錄,tar命令可以壓縮解壓targztargztgzbz2;php的壓縮方式 lt?php zip = zip_openquottmptest2zipquotif $zip while $zip_entry = zip_read$zip echo quotName quot zip_entry_name$zip_entry quot\nquotecho quotActual Filesize;第二步,你需要在apache下增加如下設(shè)置AddOutputFilter DEFLATE html php js css 這樣就可以對(duì)html php js css進(jìn)行g(shù)zip壓縮了第三步,你需要使用如下php壓縮html并輸出到客戶端的函數(shù)function compress_html$string。
2、LINUX系統(tǒng)的話,可以寫(xiě)一個(gè)常駐腳本,實(shí)時(shí)監(jiān)控你的圖片,所以發(fā)現(xiàn)圖片文件超過(guò)指定大小的話,也執(zhí)行圖片大小和尺寸壓縮的,當(dāng)然也能通過(guò)調(diào)用PHP來(lái)執(zhí)行以下是一段PHP的圖片壓縮代碼縮放圖片 function mt$srcFile,$dst;php 壓縮圖片的大小lt?php$im = imagecreatefromjpeg#39Dphpplacejpeg#39resizeImage$im,#39xinde#39,#39jpg#39function resizeImage$im,$maxwidth,$maxheight,$name,$filetype$pic_width = imagesx$im$;連接數(shù)據(jù)庫(kù) test_執(zhí)行SQL語(yǔ)句 upload_上傳圖片并壓縮 三個(gè)文件代碼如下連接數(shù)據(jù)庫(kù)lt?php db_host=#39#39db_user=#39#39db_psw=#39#39db_name=#39#39db_port=#39#39sqlconn=new_ysqli$db_host,$db_user,$db_psw,$。
3、filename = quotquot date #39YmdH#39 quotzipquot 最終生成的文件名含路徑 生成文件$zip = new ZipArchive 使用本類(lèi),linux需開(kāi)啟zlib,windows需取消php_zipdll前的注釋if $zipopen;以下是一段PHP的圖片壓縮代碼縮放圖片 functionmt$srcFile,$dstFile,$max_width=200,$max_height=150,$imgQuality=90 data=@getimagesize$srcFileif$data0lt=$data1and$data1=$max_height;lt?php The file$filename = #39testjpg#39$percent = 05 Content typeheader#39ContentType imagejpeg#39 Get new dimensionslist$width, $height = getimagesize$filename$new_width = $width;lt?php set_time_limit0ifphp_sapi_name!==#39cli#39 header#39ContentTypetextplain#39 s = file_get_contents#39xxx#39data = array#39data#39=str_repeat$s,100function benchmark$function;文件超過(guò)2G需要更換64位操作系統(tǒng),或者使用NTFS分區(qū)格式;現(xiàn)在做圖片上傳,原圖上傳是成功了,但對(duì)于壓縮這塊,上網(wǎng)查了很多資料,都是對(duì)圖片的寬高進(jìn)行壓縮 當(dāng)時(shí)就覺(jué)得奇怪寬高壓縮了,圖片就不是原圖大小,再放大應(yīng)該也會(huì)失真 我當(dāng)時(shí)也懷疑是不是根本就不是用于上傳,而僅僅是。
4、當(dāng)然可以啊,親 下面代碼就是在tmp目錄下生成aaaazip文件 zip = new ZipArchive $zipopen#39tmpaaaazip#39, ZipArchiveCREATE $zipaddEmptyDir#39dir1#39 $zipclose;connectphp連接數(shù)據(jù)庫(kù) test_uploadphp執(zhí)行SQL語(yǔ)句 upload_imgphp上傳圖片并壓縮 三個(gè)文件代碼如下連接數(shù)據(jù)庫(kù)connectphp lt?php$db_host = #39#39$db_user = #39#39$db_psw = #39#39$db_name = #39#39$db_p;lt?php pic = $_REQUEST#39name#39 1打開(kāi)圖片源文件資源 im = @imagecreatefrompng$picif $im 2源文件的寬高,也可寫(xiě)為定值 fx = imagesx$im 取寬 fy = imagesy$im ??;不用寫(xiě),rar算法是有專利權(quán)的 RAR壓縮算法是RARLab的專利,其它軟件要使用或自己實(shí)現(xiàn)RAR算法的實(shí)作,都是要向RARLab申請(qǐng)?jiān)S可或交錢(qián)的 zip算法php有自帶的函數(shù) === 需求 PHP 4 The bundled PHP 4 version requires #187。
掃描二維碼推送至手機(jī)訪問(wèn)。
版權(quán)聲明:本文由飛速云SEO網(wǎng)絡(luò)優(yōu)化推廣發(fā)布,如需轉(zhuǎn)載請(qǐng)注明出處。