当前位置:首页 » 文件管理 » php上传视频并压缩

php上传视频并压缩

发布时间: 2022-08-19 19:23:15

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中对应的信息

② php怎么上传视频

对于PHP来说,上传视频就是当一个普通的文件来处理的.比如一个图片的上传.
可能出现错误的地方是 一般PHP的配置中都限定了上传文件最大的值.
而视频相对来说是比较大的文件,所以你需要去修改配置文件.

③ 如何实现用php上传视频文件

<?php

date_default_timezone_set('PRC');

functionuploadFile($file)
{

if($file["error"]>0){
echo"ReturnCode:".$file["error"]."<br/>";
}else{

$filename=md5(uniqid().$file["name"]).'.'.getExt($file['name']);
move_uploaded_file($file["tmp_name"],"upload/".$filename);
return$filename;
}


}


functiongetExt($file)
{
$arr=explode('.',$file);

returnend($arr);
}

$file=$_FILES['fileName'];
uploadFile($file);
?>

④ 求助:用php弄一个视频上传并转换为flv格式的功能

设计原理不难理解,如果是linux服务器,视频文件上传和普通文件上传一样,上传完成后php执行shell命令调用服务器端的视频转换软件进行转换然后保存到指定的文件夹

⑤ PHP怎么对GIF动图进行压缩和上传

单帧 gif 可以转换成 jpg/png/webp 等其他图片格式来节约体积。

多帧 gif 可以使用 gifsicle 压缩,也可以转换成 apng/webp 来节约体积,国外比较流行的一种做法是把 gif 转换成 mp4视频。

用哪种方法取决于你的用户端,压缩的效果可以通过 compression ratio/ssim/psnr 等来衡量,寻找一个合适的压缩参数。

如果用到的库没有 PHP binding,那么在条件允许的情况下可以通过 popen() 和标准流来集成。

⑥ php 怎样上传压缩包并解压到目录

1.查找一般的php上传类都可以上传 zip 文件的。 (记得设置好上传文件格式就好)

2.确认你的php扩展中 包含有 php_zip 这个扩展。
然后找 zip 的相关函数方法吧。 php手册中去看。

⑦ php上传视频并且转换成flv格式的怎么实现

视频转换还是要找专门的转换工具,PHP是网站技术是不能实现的

热点内容
python文件删除一行 发布:2025-05-14 08:06:58 浏览:721
如何下载奥特曼高级化3安卓版 发布:2025-05-14 07:47:31 浏览:346
qml文件修改后编译未生效 发布:2025-05-14 07:31:00 浏览:331
内到内算法 发布:2025-05-14 07:29:11 浏览:34
文件夹名字不显示 发布:2025-05-14 07:27:47 浏览:775
oracle的数据库驱动jar 发布:2025-05-14 07:23:20 浏览:556
我的世界电脑版服务器手机版能进吗 发布:2025-05-14 07:22:01 浏览:680
达内培训php多少钱 发布:2025-05-14 07:19:10 浏览:27
python字节转字符串 发布:2025-05-14 07:06:35 浏览:423
subplotpython 发布:2025-05-14 06:53:51 浏览:662