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是網站技術是不能實現的