php压缩代码(php解压压缩包)
admin 发布:2022-12-19 23:05 147
今天给各位分享php压缩代码的知识,其中也会对php解压压缩包进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、php 压缩文件夹
- 2、如何用php压缩html代码并输出
- 3、PHP等比例压缩图片的实例代码
- 4、如何在PHP开启gzip页面压缩实例
- 5、PHP网站上传图片自动压缩,怎么编程啊,求指
- 6、PHP 实现文件压缩解压zip格式
php 压缩文件夹
php将文件夹打包成zip文件,参考代码如下:
function addFileToZip($path,$zip){
$handler=opendir($path); //打开当前文件夹由$path指定。
while(($filename=readdir($handler))!==false){
if($filename != "." $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
addFileToZip($path."/".$filename, $zip);
}else{ //将文件加入zip对象
$zip-addFile($path."/".$filename);
}
}
}
@closedir($path);
}
$zip=new ZipArchive();
if($zip-open('images.zip', ZipArchive::OVERWRITE)=== TRUE){
addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip-close(); //关闭处理的zip文件
}
如何用php压缩html代码并输出
function compressHtml($string) {
$matches = array();
preg_match_all('/((?:pre|code).+?\/(?:pre|code))+/is', $string, $matches);
foreach ((array)$matches[1] as $k = $v) {
$string = str_replace($v, "#{$k}#", $string);
}
$string = str_replace("\r\n", '', $string);
//清除换行符
$string = str_replace("\n", '', $string);
//清除换行符
$string = str_replace("\t", '', $string);
//清除制表符
$pattern = array ("/ *([^ ]*) */", "/[\s]+/", "/!--[^!]*--/", "/\" /", "/ \"/", "'/\*[^*]*\*/'");
$replace = array ("\\1", " ", "", "\"", "\"", "");
$string = preg_replace($pattern, $replace, $string);
foreach ((array)$matches[1] as $k = $v) {
$string = str_replace("#{$k}#", $v, $string);
}
return $string;
}
思路就是提前替换出 pre, code 的块内容.
在处理完之后, 再替换回来.
ps . 其实那个压缩函数没多大用途. nginx 直接开 gzip 压缩就好了. 效率和性能比用php做要高.
PHP等比例压缩图片的实例代码
具体代码如下所示:
/**
*
desription
压缩图片
*
@param
sting
$imgsrc
图片路径
*
@param
string
$imgdst
压缩后保存路径
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//压缩后的图片宽
$new_height
=
$height;//压缩后的图片高
if($width
=
600){
$per
=
600
/
$width;//计算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
总结
以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP
实现等比压缩图片尺寸和大小实例代码php
gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例
如何在PHP开启gzip页面压缩实例
1、apache环境下开启gzip
找到并打开apache/conf目录中的httpd.conf文件
httpd.conf中打开deflate_Module和headers_Module模块,具体做法为将 如下两句前面的#去掉:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
在httpd.conf文件底部加入如下代码配置需要压缩的文件:
IfModule deflate_module
SetOutputFilter DEFLATE
# Don’t compress images and other
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript
/IfModule
重启apache服务端
使用工具重新检测是否开启gzip压缩。考虑到相关广告检测机制具体的gzip检测工具访问地址可以到搜索引擎中去查找。
2、iis7.0以上
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 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$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("imagecopyresampled"))
{
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);
imagecopyresized($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中对应的信息。
望采纳,谢谢。
PHP 实现文件压缩解压zip格式
在php中,有时我们需要使用到压缩文件操作,压缩文件可以节省磁盘空间;且压缩文件更小,便于网络传输,效率高,下面我们就来了解php的压缩解压相关操作
在PHP中有一个ZipArchive类,专门用于文件的压缩解压相关操作
在ZipArchive类中主要使用到了如下方法:
第一个参数:要打开的压缩包文件
第二个参数:
ZIPARCHIVE::OVERWRITE 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉
ZIPARCHIVE::CREATE 如果指定的zip文件不存在,则新建一个
ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错
ZIPARCHIVE::CHECKCONS 对指定的zip执行其他一致性测试
上面就是ZipArchive的一些常用方法,下面来一些简单示例
关于php压缩代码和php解压压缩包的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-05区块链游戏代码源(区块链游戏编程)[20240505更新]
- 05-05客服咨询代码(客服咨询代码怎么填)[20240505更新]
- 05-05一键分享到微信代码(网站分享到微信代码)[20240505更新]
- 05-05医院预约系统代码(医院预约系统代码怎么查)[20240505更新]
- 05-05jquery柱状图代码(js柱状图如何实现)[20240505更新]
- 05-05js二维码识别代码(二维码识别程序)[20240505更新]
- 05-05代码转化器(什么是代码转换)[20240505更新]
- 05-05asp.net输出html代码(html5 asp)[20240505更新]
- 05-05酒店订房页面代码(酒店房间代码)[20240505更新]
- 05-05discuz代码模式(discuz代码大全)[20240505更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接