如果清晰的上傳後,不清晰,,那就是設置的壓縮質量問題,,,如果是不清晰上傳了,這肯定不會清晰了。。。
找到上傳的設置里可以找到圖片壓縮比例。。。。
⑵ php上傳圖片壓縮
html里加一個表單,然後提交到處理上傳文件的upload.php
類似下面這樣
<fromaction="upload.php"enctype="multipart/from-data"method="post">
<inputtype="file"name="file3"/>
</from>
然後upload.php里先處理圖片上傳,最終保存一份原始圖片文件在伺服器上,接著就把文件路徑什麼的傳到你的這個函數里處理圖片就OK了
⑶ PHP怎麼在上傳之前壓縮圖片
遠標老師教我們用fireworks優化下圖片,用最高品質jpg就可以.優化後再上傳就可以了
⑷ PHP上傳圖片,並自動修改圖片的大小
這個是可以的,用imageresamled()處理之後就file_put_contents()保存替換原文件就可以了
⑸ 有誰做過上傳文件自動壓縮的東東 - PHP進階討論
Zip是通用的,rar不通用用ZipArchive類就行了$zip = new ZipArchive();$filename = \"./test112.zip\";if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {? ? exit(\"cannot open \\n\");}$zip->addFromString(\"testfilephp.txt\" . time(), \"#1 This is a test string added as testfilephp.txt.\\n\");$zip->addFromString(\"testfilephp2.txt\" . time(), \"#2 This is a test string added as testfilephp2.txt.\\n\");$zip->addFile(\"../test/10_while_break_continue.avi\", uniqid().\".avi\");echo \"numfiles: \" . $zip->numFiles . \"\\n\";echo \"status:\" . $zip->status . \"\\n\";$zip->close();
⑹ 如何利用php把上傳的圖片壓縮
<?php
//Thefile
$filename='test.jpg';
$percent=0.5;
//Contenttype
header('Content-Type:image/jpeg');
//Getnewdimensions
list($width,$height)=getimagesize($filename);
$new_width=$width*$percent;
$new_height=$height*$percent;
//Resample
$image_p=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromjpeg($filename);
imageresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
//Output
imagejpeg($image_p,null,100);
?>
http://php.net/manual/en/function.imageresampled.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=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",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="insertintoimg(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");
//獲取文件後綴名函數
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}
//生成隨機文件名函數
functionrandom($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'];
functionResizeImage($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、HTML5上傳圖片自動壓縮問題
給你個圖片處理的類吧,圖片剪裁處理後,也就等於將圖片壓縮了。
/**
*圖像處理類
*============================================================================
*Copyright2014大秦科技,並保留所有權利。
*網站地址:http://www.qintech.net;
*============================================================================
*/
classImage{
//生成縮略圖的方式
public$thumbType;
//縮略圖的寬度
public$thumbWidth;
//縮略圖的高度
public$thumbHeight;
//生成縮略圖文件名後綴
public$thumbEndFix;
//縮略圖文件前綴
public$thumbPreFix;
/**
*構造函數
*/
publicfunction__construct(){
$this->thumbType=1;
$this->thumbWidth=120;
$this->thumbHeight=60;
$this->thumbPreFix='';
$this->thumbEndFix='_thumb';
}
/**
*檢測是否為圖像文件
*@param$img圖像
*@returnbool
*/
privatefunctioncheck($img){
$type=array(".jpg",".jpeg",".png",".gif");
$imgType=strtolower(strrchr($img,'.'));
returnextension_loaded('gd')&&file_exists($img)&&in_array($imgType,$type);
}
/**
*獲得縮略圖的尺寸信息
*@param$imgWidth原圖寬度
*@param$imgHeight原圖高度
*@param$thumbWidth縮略圖寬度
*@param$thumbHeight縮略圖的高度
*@param$thumbType處理方式
*1固定寬度高度自增2固定高度寬度自增3固定寬度高度裁切
*4固定高度寬度裁切5縮放最大邊原圖不裁切
*@returnmixed
*/
privatefunctionthumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
//初始化縮略圖尺寸
$w=$thumbWidth;
$h=$thumbHeight;
//初始化原圖尺寸
$cuthumbWidth=$imgWidth;
$cuthumbHeight=$imgHeight;
switch($thumbType){
case1:
//固定寬度高度自增
$h=$thumbWidth/$imgWidth*$imgHeight;
break;
case2:
//固定高度寬度自增
$w=$thumbHeight/$imgHeight*$imgWidth;
break;
case3:
//固定寬度高度裁切
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
break;
case4:
//固定高度寬度裁切
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
break;
case5:
//縮放最大邊原圖不裁切
if(($imgWidth/$thumbWidth)>($imgHeight/$thumbHeight)){
$h=$thumbWidth/$imgWidth*$imgHeight;
}elseif(($imgWidth/$thumbWidth)<($imgHeight/$thumbHeight)){
$w=$thumbHeight/$imgHeight*$imgWidth;
}else{
$w=$thumbWidth;
$h=$thumbHeight;
}
break;
default:
//縮略圖尺寸不變,自動裁切圖片
if(($imgHeight/$thumbHeight)<($imgWidth/$thumbWidth)){
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
}elseif(($imgHeight/$thumbHeight)>($imgWidth/$thumbWidth)){
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
}
//}
}
$arr[0]=$w;
$arr[1]=$h;
$arr[2]=$cuthumbWidth;
$arr[3]=$cuthumbHeight;
return$arr;
}
/**
*圖片裁切處理
*@param$img原圖
*@paramstring$outFile另存文件名
*@paramstring$thumbWidth縮略圖寬度
*@paramstring$thumbHeight縮略圖高度
*@paramstring$thumbType裁切圖片的方式
*1固定寬度高度自增2固定高度寬度自增3固定寬度高度裁切
*4固定高度寬度裁切5縮放最大邊原圖不裁切6縮略圖尺寸不變,自動裁切最大邊
*@returnbool|string
*/
publicfunctionthumb($img,$outFile='',$thumbWidth='',$thumbHeight='',$thumbType=''){
if(!$this->check($img)){
returnfalse;
}
//基礎配置
$thumbType=$thumbType?$thumbType:$this->thumbType;
$thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
$thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
//獲得圖像信息
$imgInfo=getimagesize($img);
$imgWidth=$imgInfo[0];
$imgHeight=$imgInfo[1];
$imgType=image_type_to_extension($imgInfo[2]);
//獲得相關尺寸
$thumb_size=$this->thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
//原始圖像資源
$func="imagecreatefrom".substr($imgType,1);
$resImg=$func($img);
//縮略圖的資源
if($imgType=='.gif'){
$res_thumb=imagecreate($thumb_size[0],$thumb_size[1]);
$color=imagecolorallocate($res_thumb,255,0,0);
}else{
$res_thumb=imagecreatetruecolor($thumb_size[0],$thumb_size[1]);
imagealphablending($res_thumb,false);//關閉混色
imagesavealpha($res_thumb,true);//儲存透明通道
}
//繪制縮略圖X
if(function_exists("imageresampled")){
imageresampled($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}else{
imageresized($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}
//處理透明色
if($imgType=='.gif'){
imagecolortransparent($res_thumb,$color);
}
//配置輸出文件名
$imgInfo=pathinfo($img);
$outFile=$outFile?$outFile:dirname($img).'/'.$this->thumbPreFix.$imgInfo['filename'].$this->thumbEndFix.".".$imgInfo['extension'];
Files::create(dirname($outFile));
$func="image".substr($imgType,1);
$func($res_thumb,$outFile);
if(isset($resImg))
imagedestroy($resImg);
if(isset($res_thumb))
imagedestroy($res_thumb);
return$outFile;
}
}