网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

php接口传输数据压缩 php上传视频压缩

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

这里会使用到三个文件:

创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目成都网站设计、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元西藏做网站,已为上家服务,为西藏各地企业和个人服务,联系电话:13518219792

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多文件上传并进行压缩。常见的手机打开某个app,,或者其他的网站,上面多有上传图片的功能……

这个你别担心, 如果是数据流量, 用户也不会去上传!

那个用手机的不知道流量伤不起?

例外, php的运行环境,默认的情况下, 是上传不了大于2M的文件的!

php上传如何设置才能上传压缩包跟txt文本

html

head

meta http-equiv="Content-Type" content="text/html; charset=gb2312"

title网页教学网(webjx.com)文件上传实例/title

/head

body

form enctype="multipart/form-data" action=upfile.php method=post

input type="hidden" name="MAX_FILE_SIZE" value="2000000"

input type=file name=upfile size=20

input type=submit value='上传文件'

/form

/body

/html

?

function getname($exname){

$dir = "../uploadfile/";

$i=1;

if(!is_dir($dir)){

mkdir($dir,0777);

}

while(true){

if(!is_file($dir.$i.".".$exname)){

$name=$i.".".$exname;

break;

}

$i++;

}

return $dir.$name;

}

$exname=strtolower(substr($_FILES['upfile']['name'],(strrpos($_FILES['upfile']['name'],'.')+1)));

$uploadfile = getname($exname);

if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {

echo "h2font color=#ff0000文件上传成功!/font/h2brbr";

}else {

echo "h2font color=#ff0000文件上传失败!/font/h2brbr";

}

echo "下面是文件上传的一些信息:

brbr原文件名:".$_FILES['upfile']['name'] .

"brbr类型:" .$_FILES['upfile']['type'] .

"brbr临时文件名:".$_FILES['upfile']['tmp_name'].

"brbr文件大小:".$_FILES['upfile']['size'] .

"brbr错误代码:".$_FILES['upfile']['error'];

?

一般文件都是可以传的!文件太大就不行了!

PHP如何让上传的大文件变小

好的PHP上传文件大小都是在php.ini中设置的。如果做小图,就要把上传的图压缩一下就可以了。但是长宽也相应的小了。

求一个php数字压缩函数

PHP是有自带的压缩函数的

gzencode 默认使用ZLIB_ENCODING_GZIP编码,使用gzip压缩格式,实际上是使用defalte 算法压缩数据,然后加上文件头和adler32校验

gzdeflate 默认使用ZLIB_ENCODING_RAW编码方式,使用deflate数据压缩算法,实际上是先用 LZ77 压缩,然后用霍夫曼编码压缩

gzcompress ;默认使用ZLIB_ENCODING_DEFLATE编码,使用zlib压缩格式,实际上是用 deflate 压缩数据,然后加上 zlib 头和 CRC 校验


新闻标题:php接口传输数据压缩 php上传视频压缩
文章转载:http://bjjierui.cn/article/doddshi.html

其他资讯